blob: fc9b04941739f62e77f586645b8c77710aeb8329 [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 *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
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
Trond Myklebust26d36302016-09-22 13:39:05 -0400331static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
334{
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
336
337 ops->test_and_free_expired(server, stateid, cred);
338}
339
340static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
343{
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
346}
347
348static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
351{
352 nfs4_stateid tmp;
353
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
356}
357
NeilBrown8478eaa2014-09-18 16:09:27 +1000358static long nfs4_update_delay(long *timeout)
359{
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
370}
371
Trond Myklebust65de8722008-12-23 15:21:44 -0500372static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
373{
374 int res = 0;
375
376 might_sleep();
377
NeilBrown8478eaa2014-09-18 16:09:27 +1000378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 return res;
383}
384
385/* This is the error handling routine for processes that are allowed
386 * to sleep.
387 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400388static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500390{
391 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500392 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400393 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500394 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 int ret = errorcode;
396
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400397 exception->delay = 0;
398 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400400
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
403
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 switch(errorcode) {
405 case 0:
406 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
415 }
416 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400417 if (inode) {
418 int err;
419
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
427 }
428 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500429 if (state == NULL)
430 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500436 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500447#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400458 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500459#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
464 */
465 ret = -EBUSY;
466 break;
467 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400471 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400472 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400473 exception->delay = 1;
474 return 0;
475
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400476 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
491 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500492 }
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500495wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400496 exception->recovering = 1;
497 return 0;
498}
499
500/* This is the error handling routine for processes that are allowed
501 * to sleep.
502 */
503int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
504{
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
507
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
512 }
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
518 }
519 return ret;
520out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500524}
525
Trond Myklebust037fc982015-09-20 15:51:00 -0400526static int
527nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
529{
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
532
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
537 }
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
543 }
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
551}
552
553static int
554nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
556{
557 struct nfs4_exception exception = {
558 .state = state,
559 };
560
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
573}
574
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400575/*
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
578 */
579static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
580{
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
582
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
586
587 return false;
588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Peng Taocb04ad22014-06-11 05:24:15 +0800627int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400631{
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 struct nfs4_slot *slot;
633
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
637
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
641
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust0a014a42016-09-22 13:38:51 -0400650 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400651 args->sa_slot = slot;
652 res->sr_slot = slot;
653
654out_start:
655 rpc_call_start(task);
656 return 0;
657
658out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
666}
Peng Taocb04ad22014-06-11 05:24:15 +0800667EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400668
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400669static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400670{
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
673
Chuck Lever3bd23842013-08-09 12:49:19 -0400674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
679
680 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400681}
682
683static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
685{
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400688 return 1;
689}
690
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691#if defined(CONFIG_NFS_V4_1)
692
Trond Myklebustd185a332010-06-16 09:52:25 -0400693static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400694{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500695 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400696 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500697 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500698 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400699
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500702
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
707
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500708 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
711 */
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
714
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500716 send_new_highest_used_slotid = false;
717 goto out_unlock;
718 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500719 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500720
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500723out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500724 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400725 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500726 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400727 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400730}
731
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400732static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400734{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500738 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500739 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400740
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500741 if (slot == NULL)
742 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400745 goto out;
746
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500747 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500748
Trond Myklebustac20d162012-12-15 15:36:07 -0500749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
752 }
753
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500755 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400756 switch (res->sr_status) {
757 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400758 /* If previous op on slot was interrupted and we reused
759 * the seq# and got a reply from the cache, then retry
760 */
761 if (task->tk_status == -EREMOTEIO && interrupted) {
762 ++slot->seq_nr;
763 goto retry_nowait;
764 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400765 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400766 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500767 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500769 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400770 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
771 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500772 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400773 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500774 case 1:
775 /*
776 * sr_status remains 1 if an RPC level error occurred.
777 * The server may or may not have processed the sequence
778 * operation..
779 * Mark the slot as having hosted an interrupted RPC call.
780 */
781 slot->interrupted = 1;
782 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400783 case -NFS4ERR_DELAY:
784 /* The server detected a resend of the RPC call and
785 * returned NFS4ERR_DELAY as per Section 2.10.6.2
786 * of RFC5661.
787 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500788 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400789 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500790 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500791 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400792 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500793 case -NFS4ERR_BADSLOT:
794 /*
795 * The slot id we used was probably retired. Try again
796 * using a different slot id.
797 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500798 goto retry_nowait;
799 case -NFS4ERR_SEQ_MISORDERED:
800 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500801 * Was the last operation on this sequence interrupted?
802 * If so, retry after bumping the sequence number.
803 */
804 if (interrupted) {
805 ++slot->seq_nr;
806 goto retry_nowait;
807 }
808 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500809 * Could this slot have been previously retired?
810 * If so, then the server may be expecting seq_nr = 1!
811 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500812 if (slot->seq_nr != 1) {
813 slot->seq_nr = 1;
814 goto retry_nowait;
815 }
816 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500817 case -NFS4ERR_SEQ_FALSE_RETRY:
818 ++slot->seq_nr;
819 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400820 default:
821 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400822 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400823 }
824out:
825 /* The session may be reset by one of the error handlers. */
826 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500827out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500828 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500829retry_nowait:
830 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400831 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500832 task->tk_status = 0;
833 ret = 0;
834 }
835 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400836out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400837 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 goto out;
839 rpc_delay(task, NFS4_POLL_RETRY_MAX);
840 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400841}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400842
843int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
844{
845 if (!nfs41_sequence_process(task, res))
846 return 0;
847 if (res->sr_slot != NULL)
848 nfs41_sequence_free_slot(res);
849 return 1;
850
851}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500852EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400853
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400854static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
855{
856 if (res->sr_slot == NULL)
857 return 1;
858 if (res->sr_slot->table->session != NULL)
859 return nfs41_sequence_process(task, res);
860 return nfs40_sequence_done(task, res);
861}
862
863static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
864{
865 if (res->sr_slot != NULL) {
866 if (res->sr_slot->table->session != NULL)
867 nfs41_sequence_free_slot(res);
868 else
869 nfs40_sequence_free_slot(res);
870 }
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400874{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500875 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400876 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400877 if (!res->sr_slot->table->session)
878 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400880}
Peng Tao2c4b1312014-06-11 05:24:16 +0800881EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400882
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000883int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400884 struct nfs4_sequence_args *args,
885 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886 struct rpc_task *task)
887{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400888 struct nfs4_slot *slot;
889 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400890
891 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400892 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400893 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400894 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400896 tbl = &session->fc_slot_table;
897
Trond Myklebust69d206b2012-11-22 13:21:02 -0500898 task->tk_timeout = 0;
899
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400900 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400901 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400902 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500903 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500904 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400905 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400906 }
907
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500908 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500909 if (IS_ERR(slot)) {
910 /* If out of memory, try again in 1/4 second */
911 if (slot == ERR_PTR(-ENOMEM))
912 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400913 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400914 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400915 }
916 spin_unlock(&tbl->slot_tbl_lock);
917
Trond Myklebust0a014a42016-09-22 13:38:51 -0400918 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500919 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400920
Chuck Levere8d92382013-08-09 12:47:51 -0400921 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500922 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Benny Halevydfb4f3092010-09-24 09:17:01 -0400924 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500925 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400926 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400927 /*
928 * sr_status is only set in decode_sequence, and so will remain
929 * set to 1 if an rpc level failure occurs.
930 */
931 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400932 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400933out_success:
934 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400935 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400936out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400937 /* Privileged tasks are queued with top priority */
938 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400939 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
940 NULL, RPC_PRIORITY_PRIVILEGED);
941 else
942 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400943 spin_unlock(&tbl->slot_tbl_lock);
944 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000946EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947
Chuck Lever5a580e02013-08-09 12:48:09 -0400948static int nfs4_setup_sequence(const struct nfs_server *server,
949 struct nfs4_sequence_args *args,
950 struct nfs4_sequence_res *res,
951 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400952{
Trond Myklebust035168a2010-06-16 09:52:26 -0400953 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400954 int ret = 0;
955
Chuck Lever3bd23842013-08-09 12:49:19 -0400956 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800957 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
958 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400959
Chuck Levere8d92382013-08-09 12:47:51 -0400960 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400961 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400962 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400963
Trond Myklebust9d12b212012-01-17 22:04:25 -0500964 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400965
Andy Adamsonce5039c2009-04-01 09:22:13 -0400966 dprintk("<-- %s status=%d\n", __func__, ret);
967 return ret;
968}
969
Andy Adamsonce5039c2009-04-01 09:22:13 -0400970static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
971{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400972 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400973 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400974
Trond Myklebust035168a2010-06-16 09:52:26 -0400975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
976
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400977 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978}
979
Andy Adamson69ab40c2009-04-01 09:22:19 -0400980static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
981{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400982 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983
Trond Myklebust14516c32010-07-31 14:29:06 -0400984 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985}
986
Trond Myklebust17280172012-03-11 13:11:00 -0400987static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400988 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400989 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990};
991
Chuck Lever3bd23842013-08-09 12:49:19 -0400992#else /* !CONFIG_NFS_V4_1 */
993
Chuck Lever5a580e02013-08-09 12:48:09 -0400994static int nfs4_setup_sequence(const struct nfs_server *server,
995 struct nfs4_sequence_args *args,
996 struct nfs4_sequence_res *res,
997 struct rpc_task *task)
998{
Peng Taocb04ad22014-06-11 05:24:15 +0800999 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1000 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001001}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001002
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001003static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1004{
1005 return nfs40_sequence_done(task, res);
1006}
1007
1008static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1009{
1010 if (res->sr_slot != NULL)
1011 nfs40_sequence_free_slot(res);
1012}
1013
Peng Tao2c4b1312014-06-11 05:24:16 +08001014int nfs4_sequence_done(struct rpc_task *task,
1015 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001016{
Chuck Lever3bd23842013-08-09 12:49:19 -04001017 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001018}
Peng Tao2c4b1312014-06-11 05:24:16 +08001019EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001020
1021#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001022
Chuck Lever9915ea72013-08-09 12:48:27 -04001023static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1024{
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_setup_sequence(data->seq_server,
1027 data->seq_args, data->seq_res, task);
1028}
1029
1030static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1031{
1032 struct nfs4_call_sync_data *data = calldata;
1033 nfs4_sequence_done(task, data->seq_res);
1034}
1035
1036static const struct rpc_call_ops nfs40_call_sync_ops = {
1037 .rpc_call_prepare = nfs40_call_sync_prepare,
1038 .rpc_call_done = nfs40_call_sync_done,
1039};
1040
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001041static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001042 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001044 struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001046{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 int ret;
1048 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001049 struct nfs_client *clp = server->nfs_client;
1050 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001051 .seq_server = server,
1052 .seq_args = args,
1053 .seq_res = res,
1054 };
1055 struct rpc_task_setup task_setup = {
1056 .rpc_client = clnt,
1057 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001058 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001059 .callback_data = &data
1060 };
1061
1062 task = rpc_run_task(&task_setup);
1063 if (IS_ERR(task))
1064 ret = PTR_ERR(task);
1065 else {
1066 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001067 rpc_put_task(task);
1068 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001069 return ret;
1070}
1071
Bryan Schumaker7c513052011-03-24 17:12:24 +00001072int nfs4_call_sync(struct rpc_clnt *clnt,
1073 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001074 struct rpc_message *msg,
1075 struct nfs4_sequence_args *args,
1076 struct nfs4_sequence_res *res,
1077 int cache_reply)
1078{
Chuck Levera9c92d62013-08-09 12:48:18 -04001079 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001080 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1084{
1085 struct nfs_inode *nfsi = NFS_I(dir);
1086
1087 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001088 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001089 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001091 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001092 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001093 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 spin_unlock(&dir->i_lock);
1095}
1096
1097struct nfs4_opendata {
1098 struct kref kref;
1099 struct nfs_openargs o_arg;
1100 struct nfs_openres o_res;
1101 struct nfs_open_confirmargs c_arg;
1102 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001103 struct nfs4_string owner_name;
1104 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001105 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001107 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001109 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct nfs4_state_owner *owner;
1111 struct nfs4_state *state;
1112 struct iattr attrs;
1113 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001114 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001115 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001116 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int cancelled;
1119};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001121static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1122 int err, struct nfs4_exception *exception)
1123{
1124 if (err != -EINVAL)
1125 return false;
1126 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1127 return false;
1128 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1129 exception->retry = 1;
1130 return true;
1131}
1132
Trond Myklebust6ae37332015-01-30 14:21:14 -05001133static u32
1134nfs4_map_atomic_open_share(struct nfs_server *server,
1135 fmode_t fmode, int openflags)
1136{
1137 u32 res = 0;
1138
1139 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1140 case FMODE_READ:
1141 res = NFS4_SHARE_ACCESS_READ;
1142 break;
1143 case FMODE_WRITE:
1144 res = NFS4_SHARE_ACCESS_WRITE;
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_BOTH;
1148 }
1149 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1150 goto out;
1151 /* Want no delegation if we're using O_DIRECT */
1152 if (openflags & O_DIRECT)
1153 res |= NFS4_SHARE_WANT_NO_DELEG;
1154out:
1155 return res;
1156}
1157
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001158static enum open_claim_type4
1159nfs4_map_atomic_open_claim(struct nfs_server *server,
1160 enum open_claim_type4 claim)
1161{
1162 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1163 return claim;
1164 switch (claim) {
1165 default:
1166 return claim;
1167 case NFS4_OPEN_CLAIM_FH:
1168 return NFS4_OPEN_CLAIM_NULL;
1169 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1170 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1171 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1172 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1173 }
1174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177{
1178 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001179 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 p->o_res.seqid = p->o_arg.seqid;
1181 p->c_res.seqid = p->c_arg.seqid;
1182 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001183 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001184 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001185 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186}
1187
Al Viro82a2c1b2011-06-22 18:30:55 -04001188static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001190 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001191 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001192 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194{
Al Viro82a2c1b2011-06-22 18:30:55 -04001195 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001196 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001198 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 struct nfs4_opendata *p;
1200
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 if (p == NULL)
1203 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001204
1205 p->f_label = nfs4_label_alloc(server, gfp_mask);
1206 if (IS_ERR(p->f_label))
1207 goto err_free_p;
1208
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 p->a_label = nfs4_label_alloc(server, gfp_mask);
1210 if (IS_ERR(p->a_label))
1211 goto err_free_f;
1212
Trond Myklebust63f5f792015-01-23 19:19:25 -05001213 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1214 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001215 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001216 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001217 nfs_sb_active(dentry->d_sb);
1218 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219 p->dir = parent;
1220 p->owner = sp;
1221 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 p->o_arg.open_flags = flags;
1223 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1225 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags & O_EXCL)) {
1229 /* ask server to check for all possible rights as results
1230 * are cached */
1231 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1232 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001233 }
David Howells7539bba2006-08-22 20:06:09 -04001234 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001235 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1236 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001237 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001238 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001239 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001240 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001241 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001242 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1243 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001244 case NFS4_OPEN_CLAIM_NULL:
1245 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1246 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1247 p->o_arg.fh = NFS_FH(dir);
1248 break;
1249 case NFS4_OPEN_CLAIM_PREVIOUS:
1250 case NFS4_OPEN_CLAIM_FH:
1251 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1252 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001253 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001254 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001255 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001256 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001257
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 p->o_arg.u.attrs = &p->attrs;
1259 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001260
1261 verf[0] = jiffies;
1262 verf[1] = current->pid;
1263 memcpy(p->o_arg.u.verifier.data, verf,
1264 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001266 p->c_arg.fh = &p->o_res.fh;
1267 p->c_arg.stateid = &p->o_res.stateid;
1268 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001269 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001272
1273err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001274 nfs4_label_free(p->a_label);
1275err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001276 nfs4_label_free(p->f_label);
1277err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 kfree(p);
1279err:
1280 dput(parent);
1281 return NULL;
1282}
1283
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 struct nfs4_opendata *p = container_of(kref,
1287 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001288 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289
1290 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001291 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 if (p->state != NULL)
1293 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001295
Kinglong Meea49c2692015-07-27 15:31:38 +08001296 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001297 nfs4_label_free(p->f_label);
1298
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001300 dput(p->dentry);
1301 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001302 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001303 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 kfree(p);
1305}
1306
1307static void nfs4_opendata_put(struct nfs4_opendata *p)
1308{
1309 if (p != NULL)
1310 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311}
1312
Trond Myklebust06f814a2006-01-03 09:55:07 +01001313static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1314{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001315 int ret;
1316
Trond Myklebust06f814a2006-01-03 09:55:07 +01001317 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318 return ret;
1319}
1320
Trond Myklebust24311f82015-09-20 10:50:17 -04001321static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1322 fmode_t fmode)
1323{
1324 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1325 case FMODE_READ|FMODE_WRITE:
1326 return state->n_rdwr != 0;
1327 case FMODE_WRITE:
1328 return state->n_wronly != 0;
1329 case FMODE_READ:
1330 return state->n_rdonly != 0;
1331 }
1332 WARN_ON_ONCE(1);
1333 return false;
1334}
1335
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001337{
1338 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339
Trond Myklebust536e43d2012-01-17 22:04:26 -05001340 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 goto out;
1342 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001344 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1345 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 break;
1347 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001348 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1349 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 break;
1351 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001352 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1353 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001356 return ret;
1357}
1358
Trond Myklebust2a606182015-08-19 22:30:00 -05001359static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1360 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001362 if (delegation == NULL)
1363 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001366 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1367 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001368 switch (claim) {
1369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_FH:
1371 break;
1372 case NFS4_OPEN_CLAIM_PREVIOUS:
1373 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1374 break;
1375 default:
1376 return 0;
1377 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001378 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 return 1;
1380}
1381
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001383{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001384 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001385 case FMODE_WRITE:
1386 state->n_wronly++;
1387 break;
1388 case FMODE_READ:
1389 state->n_rdonly++;
1390 break;
1391 case FMODE_READ|FMODE_WRITE:
1392 state->n_rdwr++;
1393 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001394 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001395}
1396
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001397#ifdef CONFIG_NFS_V4_1
1398static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1399{
1400 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1401 return true;
1402 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1403 return true;
1404 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1405 return true;
1406 return false;
1407}
1408#endif /* CONFIG_NFS_V4_1 */
1409
Trond Myklebust4f14c192014-02-12 19:15:06 -05001410static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001411{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001412 struct nfs_client *clp = state->owner->so_server->nfs_client;
1413 bool need_recover = false;
1414
1415 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1416 need_recover = true;
1417 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1418 need_recover = true;
1419 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1420 need_recover = true;
1421 if (need_recover)
1422 nfs4_state_mark_reclaim_nograce(clp, state);
1423}
1424
Trond Myklebuste999e802014-02-10 18:20:47 -05001425static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001426 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001427{
1428 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1429 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001430 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001431 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001432 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001434 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001435 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1436 return true;
1437 return false;
1438}
1439
Trond Myklebustf95549c2015-01-23 18:06:09 -05001440static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1441{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001442 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1443 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444 if (state->n_wronly)
1445 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1446 if (state->n_rdonly)
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 if (state->n_rdwr)
1449 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001450 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451}
1452
Trond Myklebust226056c2014-02-11 10:41:07 -05001453static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1454 nfs4_stateid *stateid, fmode_t fmode)
1455{
1456 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1457 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1458 case FMODE_WRITE:
1459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 break;
1461 case FMODE_READ:
1462 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1463 break;
1464 case 0:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 clear_bit(NFS_OPEN_STATE, &state->flags);
1468 }
1469 if (stateid == NULL)
1470 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001471 /* Handle OPEN+OPEN_DOWNGRADE races */
1472 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1473 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001474 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001475 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001476 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001477 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001478 nfs4_stateid_copy(&state->stateid, stateid);
1479 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001480}
1481
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001482static void nfs_clear_open_stateid(struct nfs4_state *state,
1483 nfs4_stateid *arg_stateid,
1484 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001485{
1486 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001487 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1488 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1489 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001490 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001491 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1492 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001493}
1494
Trond Myklebust1393d962016-09-22 13:39:13 -04001495static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1496 const nfs4_stateid *stateid, fmode_t fmode,
1497 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001498{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001500 case FMODE_READ:
1501 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1502 break;
1503 case FMODE_WRITE:
1504 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1505 break;
1506 case FMODE_READ|FMODE_WRITE:
1507 set_bit(NFS_O_RDWR_STATE, &state->flags);
1508 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001509 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001510 return;
1511 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1512 nfs4_stateid_copy(&state->stateid, stateid);
1513 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001514}
1515
Trond Myklebust1393d962016-09-22 13:39:13 -04001516static void __update_open_stateid(struct nfs4_state *state,
1517 const nfs4_stateid *open_stateid,
1518 const nfs4_stateid *deleg_stateid,
1519 fmode_t fmode,
1520 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001522 /*
1523 * Protect the call to nfs4_state_set_mode_locked and
1524 * serialise the stateid update
1525 */
Andrew Elble361cad32015-12-02 09:20:57 -05001526 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001528 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001529 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001530 set_bit(NFS_DELEGATED_STATE, &state->flags);
1531 }
1532 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001533 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001534 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001535 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001536 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Trond Myklebust1393d962016-09-22 13:39:13 -04001539static int update_open_stateid(struct nfs4_state *state,
1540 const nfs4_stateid *open_stateid,
1541 const nfs4_stateid *delegation,
1542 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001543{
Trond Myklebust1393d962016-09-22 13:39:13 -04001544 struct nfs_server *server = NFS_SERVER(state->inode);
1545 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001546 struct nfs_inode *nfsi = NFS_I(state->inode);
1547 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001548 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001549 int ret = 0;
1550
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001551 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001552
1553 rcu_read_lock();
1554 deleg_cur = rcu_dereference(nfsi->delegation);
1555 if (deleg_cur == NULL)
1556 goto no_delegation;
1557
1558 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001559 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001560 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001561 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001562 goto no_delegation_unlock;
1563
1564 if (delegation == NULL)
1565 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001566 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001567 goto no_delegation_unlock;
1568
Trond Myklebustb7391f42008-12-23 15:21:52 -05001569 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001570 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1571 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001572 ret = 1;
1573no_delegation_unlock:
1574 spin_unlock(&deleg_cur->lock);
1575no_delegation:
1576 rcu_read_unlock();
1577
1578 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001579 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001580 ret = 1;
1581 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001582 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001583 nfs4_schedule_state_manager(clp);
1584 if (freeme.type != 0)
1585 nfs4_test_and_free_stateid(server, &freeme,
1586 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001587
1588 return ret;
1589}
1590
Trond Myklebust39071e62015-01-24 15:07:56 -05001591static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1592 const nfs4_stateid *stateid)
1593{
1594 struct nfs4_state *state = lsp->ls_state;
1595 bool ret = false;
1596
1597 spin_lock(&state->state_lock);
1598 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1599 goto out_noupdate;
1600 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1601 goto out_noupdate;
1602 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1603 ret = true;
1604out_noupdate:
1605 spin_unlock(&state->state_lock);
1606 return ret;
1607}
Trond Myklebust34310432008-12-23 15:21:38 -05001608
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610{
1611 struct nfs_delegation *delegation;
1612
1613 rcu_read_lock();
1614 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001615 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 rcu_read_unlock();
1617 return;
1618 }
1619 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001620 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621}
1622
Trond Myklebust6ee41262007-07-08 14:11:36 -04001623static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624{
1625 struct nfs4_state *state = opendata->state;
1626 struct nfs_inode *nfsi = NFS_I(state->inode);
1627 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001628 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001630 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 nfs4_stateid stateid;
1632 int ret = -EAGAIN;
1633
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001635 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001636 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001637 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001638 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001639 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001640 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001641 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001642 rcu_read_lock();
1643 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001644 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001645 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001646 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001647 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001648 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001649 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001651 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001652 if (!opendata->is_recover) {
1653 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1654 if (ret != 0)
1655 goto out;
1656 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001657 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001658
1659 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001660 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001661 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001663out:
1664 return ERR_PTR(ret);
1665out_return_state:
1666 atomic_inc(&state->count);
1667 return state;
1668}
1669
Andy Adamsone23008e2012-10-02 21:07:32 -04001670static void
1671nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1672{
1673 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1674 struct nfs_delegation *delegation;
1675 int delegation_flags = 0;
1676
1677 rcu_read_lock();
1678 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1679 if (delegation)
1680 delegation_flags = delegation->flags;
1681 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001682 switch (data->o_arg.claim) {
1683 default:
1684 break;
1685 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1686 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001687 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1688 "returning a delegation for "
1689 "OPEN(CLAIM_DELEGATE_CUR)\n",
1690 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001691 return;
1692 }
1693 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001694 nfs_inode_set_delegation(state->inode,
1695 data->owner->so_cred,
1696 &data->o_res);
1697 else
1698 nfs_inode_reclaim_delegation(state->inode,
1699 data->owner->so_cred,
1700 &data->o_res);
1701}
1702
1703/*
1704 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1705 * and update the nfs4_state.
1706 */
1707static struct nfs4_state *
1708_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1709{
1710 struct inode *inode = data->state->inode;
1711 struct nfs4_state *state = data->state;
1712 int ret;
1713
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001714 if (!data->rpc_done) {
1715 if (data->rpc_status) {
1716 ret = data->rpc_status;
1717 goto err;
1718 }
1719 /* cached opens have already been processed */
1720 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001721 }
1722
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 ret = nfs_refresh_inode(inode, &data->f_attr);
1724 if (ret)
1725 goto err;
1726
1727 if (data->o_res.delegation_type != 0)
1728 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001729update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001730 update_open_stateid(state, &data->o_res.stateid, NULL,
1731 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001732 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001733
1734 return state;
1735err:
1736 return ERR_PTR(ret);
1737
1738}
1739
1740static struct nfs4_state *
1741_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742{
1743 struct inode *inode;
1744 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001745 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001746
Trond Myklebustaac00a82007-07-05 19:02:21 -04001747 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001748 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001749 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 goto out;
1751 }
1752
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001753 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001755 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001756 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001757 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001758 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001759 goto err;
1760 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 state = nfs4_get_open_state(inode, data->owner);
1762 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001763 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001764 if (data->o_res.delegation_type != 0)
1765 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001766 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001767 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001770 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001772err_put_inode:
1773 iput(inode);
1774err:
1775 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776}
1777
Andy Adamsone23008e2012-10-02 21:07:32 -04001778static struct nfs4_state *
1779nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1780{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001781 struct nfs4_state *ret;
1782
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001784 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1785 else
1786 ret = _nfs4_opendata_to_nfs4_state(data);
1787 nfs4_sequence_free_slot(&data->o_res.seq_res);
1788 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001789}
1790
Trond Myklebust864472e2006-01-03 09:55:15 +01001791static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1792{
1793 struct nfs_inode *nfsi = NFS_I(state->inode);
1794 struct nfs_open_context *ctx;
1795
1796 spin_lock(&state->inode->i_lock);
1797 list_for_each_entry(ctx, &nfsi->open_files, list) {
1798 if (ctx->state != state)
1799 continue;
1800 get_nfs_open_context(ctx);
1801 spin_unlock(&state->inode->i_lock);
1802 return ctx;
1803 }
1804 spin_unlock(&state->inode->i_lock);
1805 return ERR_PTR(-ENOENT);
1806}
1807
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001808static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1809 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001810{
1811 struct nfs4_opendata *opendata;
1812
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001813 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001814 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001815 if (opendata == NULL)
1816 return ERR_PTR(-ENOMEM);
1817 opendata->state = state;
1818 atomic_inc(&state->count);
1819 return opendata;
1820}
1821
Trond Myklebust24311f82015-09-20 10:50:17 -04001822static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1823 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001824{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001825 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001826 int ret;
1827
Trond Myklebust24311f82015-09-20 10:50:17 -04001828 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001829 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001830 opendata->o_arg.open_flags = 0;
1831 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001832 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1833 NFS_SB(opendata->dentry->d_sb),
1834 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001835 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1836 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1837 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001838 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001839 if (ret != 0)
1840 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001841 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001842 if (IS_ERR(newstate))
1843 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001844 if (newstate != opendata->state)
1845 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001846 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001848}
1849
1850static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1851{
Trond Myklebust864472e2006-01-03 09:55:15 +01001852 int ret;
1853
Trond Myklebust4f14c192014-02-12 19:15:06 -05001854 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1855 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1856 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1857 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001859 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001860 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001862 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1863 if (ret != 0)
1864 return ret;
1865 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1866 if (ret != 0)
1867 return ret;
1868 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1869 if (ret != 0)
1870 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001871 /*
1872 * We may have performed cached opens for all three recoveries.
1873 * Check if we need to update the current stateid.
1874 */
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001876 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001877 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001879 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001880 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001881 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001882 return 0;
1883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/*
1886 * OPEN_RECLAIM:
1887 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001889static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001891 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001893 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int status;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1897 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (IS_ERR(opendata))
1899 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001900 rcu_read_lock();
1901 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001902 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001903 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001904 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001905 opendata->o_arg.u.delegation_type = delegation_type;
1906 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001907 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return status;
1909}
1910
Trond Myklebust539cd032007-06-05 11:46:42 -04001911static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 struct nfs_server *server = NFS_SERVER(state->inode);
1914 struct nfs4_exception exception = { };
1915 int err;
1916 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001917 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001918 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001919 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1920 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001921 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001922 break;
1923 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 } while (exception.retry);
1925 return err;
1926}
1927
Trond Myklebust864472e2006-01-03 09:55:15 +01001928static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1929{
1930 struct nfs_open_context *ctx;
1931 int ret;
1932
1933 ctx = nfs4_state_find_open_context(state);
1934 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001935 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001936 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001937 put_nfs_open_context(ctx);
1938 return ret;
1939}
1940
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001941static 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 -07001942{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001943 switch (err) {
1944 default:
1945 printk(KERN_ERR "NFS: %s: unhandled error "
1946 "%d.\n", __func__, err);
1947 case 0:
1948 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001949 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001950 case -ESTALE:
1951 break;
1952 case -NFS4ERR_BADSESSION:
1953 case -NFS4ERR_BADSLOT:
1954 case -NFS4ERR_BAD_HIGH_SLOT:
1955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1956 case -NFS4ERR_DEADSESSION:
1957 set_bit(NFS_DELEGATED_STATE, &state->flags);
1958 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1959 return -EAGAIN;
1960 case -NFS4ERR_STALE_CLIENTID:
1961 case -NFS4ERR_STALE_STATEID:
1962 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001963 /* Don't recall a delegation if it was lost */
1964 nfs4_schedule_lease_recovery(server->nfs_client);
1965 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001966 case -NFS4ERR_MOVED:
1967 nfs4_schedule_migration_recovery(server);
1968 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001969 case -NFS4ERR_LEASE_MOVED:
1970 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1971 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001972 case -NFS4ERR_DELEG_REVOKED:
1973 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001974 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001976 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 nfs_inode_find_state_and_recover(state->inode,
1978 stateid);
1979 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001980 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001981 case -NFS4ERR_DELAY:
1982 case -NFS4ERR_GRACE:
1983 set_bit(NFS_DELEGATED_STATE, &state->flags);
1984 ssleep(1);
1985 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001986 case -ENOMEM:
1987 case -NFS4ERR_DENIED:
1988 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1989 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return err;
1992}
1993
Trond Myklebust24311f82015-09-20 10:50:17 -04001994int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1995 struct nfs4_state *state, const nfs4_stateid *stateid,
1996 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001997{
1998 struct nfs_server *server = NFS_SERVER(state->inode);
1999 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002000 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002001
2002 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2003 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2004 if (IS_ERR(opendata))
2005 return PTR_ERR(opendata);
2006 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002007 write_seqlock(&state->seqlock);
2008 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2009 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002010 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2011 switch (type & (FMODE_READ|FMODE_WRITE)) {
2012 case FMODE_READ|FMODE_WRITE:
2013 case FMODE_WRITE:
2014 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2015 if (err)
2016 break;
2017 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2018 if (err)
2019 break;
2020 case FMODE_READ:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2022 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002023 nfs4_opendata_put(opendata);
2024 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2025}
2026
Chuck Leverbe05c862013-08-09 12:49:47 -04002027static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2028{
2029 struct nfs4_opendata *data = calldata;
2030
Peng Taocb04ad22014-06-11 05:24:15 +08002031 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2032 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002033}
2034
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2036{
2037 struct nfs4_opendata *data = calldata;
2038
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002039 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002040
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002042 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002043 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002044 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002045 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002046 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002047 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048}
2049
2050static void nfs4_open_confirm_release(void *calldata)
2051{
2052 struct nfs4_opendata *data = calldata;
2053 struct nfs4_state *state = NULL;
2054
2055 /* If this request hasn't been cancelled, do nothing */
2056 if (data->cancelled == 0)
2057 goto out_free;
2058 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002059 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002061 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002062 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002063 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002064out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002065 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066}
2067
2068static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002069 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002070 .rpc_call_done = nfs4_open_confirm_done,
2071 .rpc_release = nfs4_open_confirm_release,
2072};
2073
2074/*
2075 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2076 */
2077static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2078{
David Howells2b0143b2015-03-17 22:25:59 +00002079 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002080 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002081 struct rpc_message msg = {
2082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2083 .rpc_argp = &data->c_arg,
2084 .rpc_resp = &data->c_res,
2085 .rpc_cred = data->owner->so_cred,
2086 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002087 struct rpc_task_setup task_setup_data = {
2088 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002089 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002090 .callback_ops = &nfs4_open_confirm_ops,
2091 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002092 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002093 .flags = RPC_TASK_ASYNC,
2094 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int status;
2096
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002097 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002098 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002099 data->rpc_done = 0;
2100 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002101 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002102 if (data->is_recover)
2103 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002104 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002105 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002106 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002107 status = nfs4_wait_for_completion_rpc_task(task);
2108 if (status != 0) {
2109 data->cancelled = 1;
2110 smp_wmb();
2111 } else
2112 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002113 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return status;
2115}
2116
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002119 struct nfs4_opendata *data = calldata;
2120 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002121 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002122 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002125 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002126 /*
2127 * Check if we still need to send an OPEN call, or if we can use
2128 * a delegation instead.
2129 */
2130 if (data->state != NULL) {
2131 struct nfs_delegation *delegation;
2132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002133 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002134 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002135 rcu_read_lock();
2136 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002137 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002138 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002139 rcu_read_unlock();
2140 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002141 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002142 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002143 switch (claim) {
2144 default:
2145 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002146 case NFS4_OPEN_CLAIM_PREVIOUS:
2147 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2148 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002149 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002150 case NFS4_OPEN_CLAIM_FH:
2151 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002152 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2153 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002154 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002155 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002156 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002157 &data->o_res.seq_res,
2158 task) != 0)
2159 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002160
2161 /* Set the create mode (note dependency on the session type) */
2162 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2163 if (data->o_arg.open_flags & O_EXCL) {
2164 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2165 if (nfs4_has_persistent_session(clp))
2166 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2167 else if (clp->cl_mvops->minor_version > 0)
2168 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2169 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002170 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002171unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002172 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002173 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002174out_no_action:
2175 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002176out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002177 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002178}
2179
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002180static void nfs4_open_done(struct rpc_task *task, void *calldata)
2181{
2182 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002184 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002185
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002186 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002187 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002188
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002190 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2191 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 case S_IFREG:
2193 break;
2194 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002196 break;
2197 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 break;
2200 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002202 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002204 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002205 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2206 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002207 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002208 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002209}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002211static void nfs4_open_release(void *calldata)
2212{
2213 struct nfs4_opendata *data = calldata;
2214 struct nfs4_state *state = NULL;
2215
2216 /* If this request hasn't been cancelled, do nothing */
2217 if (data->cancelled == 0)
2218 goto out_free;
2219 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002220 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 goto out_free;
2222 /* In case we need an open_confirm, no cleanup! */
2223 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2224 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002225 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002226 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002227 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002229 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230}
2231
2232static const struct rpc_call_ops nfs4_open_ops = {
2233 .rpc_call_prepare = nfs4_open_prepare,
2234 .rpc_call_done = nfs4_open_done,
2235 .rpc_release = nfs4_open_release,
2236};
2237
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002238static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239{
David Howells2b0143b2015-03-17 22:25:59 +00002240 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002241 struct nfs_server *server = NFS_SERVER(dir);
2242 struct nfs_openargs *o_arg = &data->o_arg;
2243 struct nfs_openres *o_res = &data->o_res;
2244 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002245 struct rpc_message msg = {
2246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2247 .rpc_argp = o_arg,
2248 .rpc_resp = o_res,
2249 .rpc_cred = data->owner->so_cred,
2250 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 struct rpc_task_setup task_setup_data = {
2252 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002253 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002254 .callback_ops = &nfs4_open_ops,
2255 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002256 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002257 .flags = RPC_TASK_ASYNC,
2258 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002259 int status;
2260
Chuck Levera9c92d62013-08-09 12:48:18 -04002261 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002262 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002263 data->rpc_done = 0;
2264 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002265 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002266 data->is_recover = 0;
2267 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002268 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002269 data->is_recover = 1;
2270 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002271 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002272 if (IS_ERR(task))
2273 return PTR_ERR(task);
2274 status = nfs4_wait_for_completion_rpc_task(task);
2275 if (status != 0) {
2276 data->cancelled = 1;
2277 smp_wmb();
2278 } else
2279 status = data->rpc_status;
2280 rpc_put_task(task);
2281
2282 return status;
2283}
2284
2285static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2286{
David Howells2b0143b2015-03-17 22:25:59 +00002287 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002288 struct nfs_openres *o_res = &data->o_res;
2289 int status;
2290
2291 status = nfs4_run_open_task(data, 1);
2292 if (status != 0 || !data->rpc_done)
2293 return status;
2294
Trond Myklebust6926afd2012-01-07 13:22:46 -05002295 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2296
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002297 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2298 status = _nfs4_proc_open_confirm(data);
2299 if (status != 0)
2300 return status;
2301 }
2302
2303 return status;
2304}
2305
Trond Myklebustf3792d62014-07-10 08:54:32 -04002306/*
2307 * Additional permission checks in order to distinguish between an
2308 * open for read, and an open for execute. This works around the
2309 * fact that NFSv4 OPEN treats read and execute permissions as being
2310 * the same.
2311 * Note that in the non-execute case, we want to turn off permission
2312 * checking if we just created a new file (POSIX open() semantics).
2313 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002314static int nfs4_opendata_access(struct rpc_cred *cred,
2315 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002316 struct nfs4_state *state, fmode_t fmode,
2317 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002318{
2319 struct nfs_access_entry cache;
2320 u32 mask;
2321
2322 /* access call failed or for some reason the server doesn't
2323 * support any access modes -- defer access call until later */
2324 if (opendata->o_res.access_supported == 0)
2325 return 0;
2326
2327 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002328 /*
2329 * Use openflags to check for exec, because fmode won't
2330 * always have FMODE_EXEC set when file open for exec.
2331 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002332 if (openflags & __FMODE_EXEC) {
2333 /* ONLY check for exec rights */
2334 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002335 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002336 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337
2338 cache.cred = cred;
2339 cache.jiffies = jiffies;
2340 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2341 nfs_access_add_cache(state->inode, &cache);
2342
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002343 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344 return 0;
2345
2346 /* even though OPEN succeeded, access is denied. Close the file */
2347 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002348 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002349}
2350
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002351/*
2352 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2353 */
2354static int _nfs4_proc_open(struct nfs4_opendata *data)
2355{
David Howells2b0143b2015-03-17 22:25:59 +00002356 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002357 struct nfs_server *server = NFS_SERVER(dir);
2358 struct nfs_openargs *o_arg = &data->o_arg;
2359 struct nfs_openres *o_res = &data->o_res;
2360 int status;
2361
2362 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002363 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002364 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002365 if (status != 0) {
2366 if (status == -NFS4ERR_BADNAME &&
2367 !(o_arg->open_flags & O_CREAT))
2368 return -ENOENT;
2369 return status;
2370 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371
Trond Myklebust6926afd2012-01-07 13:22:46 -05002372 nfs_fattr_map_and_free_names(server, &data->f_attr);
2373
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002374 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002375 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002376 if (o_arg->open_flags & O_EXCL)
2377 data->file_created = 1;
2378 else if (o_res->cinfo.before != o_res->cinfo.after)
2379 data->file_created = 1;
2380 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002381 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2382 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002384 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002388 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2389 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002390 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002391 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Andy Adamsond83217c2011-03-01 01:34:17 +00002395static int nfs4_recover_expired_lease(struct nfs_server *server)
2396{
2397 return nfs4_client_recover_expired_lease(server->nfs_client);
2398}
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400/*
2401 * OPEN_EXPIRED:
2402 * reclaim state on the server after a network partition.
2403 * Assumes caller holds the appropriate lock
2404 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002405static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002407 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002408 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002410 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002411 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002412 if (IS_ERR(opendata))
2413 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002414 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002415 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002416 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002417 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002418 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002421static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002422{
Trond Myklebust539cd032007-06-05 11:46:42 -04002423 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002424 struct nfs4_exception exception = { };
2425 int err;
2426
2427 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002428 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002429 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002430 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2431 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002432 switch (err) {
2433 default:
2434 goto out;
2435 case -NFS4ERR_GRACE:
2436 case -NFS4ERR_DELAY:
2437 nfs4_handle_exception(server, err, &exception);
2438 err = 0;
2439 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002440 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002441out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002442 return err;
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002448 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Trond Myklebust864472e2006-01-03 09:55:15 +01002450 ctx = nfs4_state_find_open_context(state);
2451 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002452 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002453 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002454 put_nfs_open_context(ctx);
2455 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Trond Myklebust41020b62016-09-22 13:38:58 -04002458static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2459 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002460{
Trond Myklebust41020b62016-09-22 13:38:58 -04002461 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002462 write_seqlock(&state->seqlock);
2463 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2464 write_sequnlock(&state->seqlock);
2465 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2466}
2467
2468static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2469{
2470 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002471 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002472}
2473
2474static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2475{
2476 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2477 nfs40_clear_delegation_stateid(state);
2478 return nfs4_open_expired(sp, state);
2479}
2480
Trond Myklebust45870d62016-09-22 13:38:59 -04002481static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2482 nfs4_stateid *stateid,
2483 struct rpc_cred *cred)
2484{
2485 return -NFS4ERR_BAD_STATEID;
2486}
2487
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002488#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002489static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2490 nfs4_stateid *stateid,
2491 struct rpc_cred *cred)
2492{
2493 int status;
2494
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002495 switch (stateid->type) {
2496 default:
2497 break;
2498 case NFS4_INVALID_STATEID_TYPE:
2499 case NFS4_SPECIAL_STATEID_TYPE:
2500 return -NFS4ERR_BAD_STATEID;
2501 case NFS4_REVOKED_STATEID_TYPE:
2502 goto out_free;
2503 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002504
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002505 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002506 switch (status) {
2507 case -NFS4ERR_EXPIRED:
2508 case -NFS4ERR_ADMIN_REVOKED:
2509 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002510 break;
2511 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002512 return status;
2513 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002514out_free:
2515 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002516 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002517 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002518}
2519
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002520static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002521{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002522 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002523 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002524 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525 struct rpc_cred *cred;
2526 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002527
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002528 /* Get the delegation credential for use by test/free_stateid */
2529 rcu_read_lock();
2530 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002531 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002532 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533 return;
2534 }
2535
2536 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia461bbb92017-03-30 13:49:03 -04002537 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2538 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2539 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002540 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002541 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002542 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002543 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002544
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002545 cred = get_rpccred(delegation->cred);
2546 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002547 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002548 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002549 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002550 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002551
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002552 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002553}
2554
2555/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002556 * nfs41_check_expired_locks - possibly free a lock stateid
2557 *
2558 * @state: NFSv4 state for an inode
2559 *
2560 * Returns NFS_OK if recovery for this stateid is now finished.
2561 * Otherwise a negative NFS4ERR value is returned.
2562 */
2563static int nfs41_check_expired_locks(struct nfs4_state *state)
2564{
2565 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002566 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002567 struct nfs_server *server = NFS_SERVER(state->inode);
2568
2569 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2570 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002571
2572 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002573 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2574 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2575 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2576
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002577 atomic_inc(&lsp->ls_count);
2578 spin_unlock(&state->state_lock);
2579
2580 nfs4_put_lock_state(prev);
2581 prev = lsp;
2582
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002583 status = nfs41_test_and_free_expired_stateid(server,
2584 &lsp->ls_stateid,
2585 cred);
2586 trace_nfs4_test_lock_stateid(state, lsp, status);
2587 if (status == -NFS4ERR_EXPIRED ||
2588 status == -NFS4ERR_BAD_STATEID) {
2589 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002590 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 if (!recover_lost_locks)
2592 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2593 } else if (status != NFS_OK) {
2594 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002595 nfs4_put_lock_state(prev);
2596 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002597 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002598 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002599 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002600 }
2601 spin_unlock(&state->state_lock);
2602 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603out:
2604 return ret;
2605}
2606
2607/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002608 * nfs41_check_open_stateid - possibly free an open stateid
2609 *
2610 * @state: NFSv4 state for an inode
2611 *
2612 * Returns NFS_OK if recovery for this stateid is now finished.
2613 * Otherwise a negative NFS4ERR value is returned.
2614 */
2615static int nfs41_check_open_stateid(struct nfs4_state *state)
2616{
2617 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002618 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002619 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 int status;
2621
Trond Myklebustb134fc42016-09-22 13:39:16 -04002622 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002623 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2624 if (nfs4_have_delegation(state->inode, state->state))
2625 return NFS_OK;
2626 return -NFS4ERR_OPENMODE;
2627 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002629 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002630 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002631 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002632 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002633 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2634 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2635 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002636 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002637 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002638 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002639 if (status != NFS_OK)
2640 return status;
2641 if (nfs_open_stateid_recover_openmode(state))
2642 return -NFS4ERR_OPENMODE;
2643 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002644}
2645
2646static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2647{
Chuck Levereb64cf92012-07-11 16:30:05 -04002648 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002649
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002650 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002651 status = nfs41_check_expired_locks(state);
2652 if (status != NFS_OK)
2653 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002654 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002655 if (status != NFS_OK)
2656 status = nfs4_open_expired(sp, state);
2657 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002658}
2659#endif
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002662 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2663 * fields corresponding to attributes that were used to store the verifier.
2664 * Make sure we clobber those fields in the later setattr call
2665 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002666static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2667 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002668{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002669 const u32 *attrset = opendata->o_res.attrset;
2670
2671 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002672 !(sattr->ia_valid & ATTR_ATIME_SET))
2673 sattr->ia_valid |= ATTR_ATIME;
2674
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002675 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002676 !(sattr->ia_valid & ATTR_MTIME_SET))
2677 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002678
2679 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002680 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2681 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002682 sattr->ia_valid &= ~ATTR_MODE;
2683
2684 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2685 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002686}
2687
Trond Myklebustc21443c2013-02-07 14:26:21 -05002688static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2689 fmode_t fmode,
2690 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002691 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002692{
2693 struct nfs4_state_owner *sp = opendata->owner;
2694 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002695 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002696 struct nfs4_state *state;
2697 unsigned int seq;
2698 int ret;
2699
2700 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2701
2702 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002703 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002704 goto out;
2705
2706 state = nfs4_opendata_to_nfs4_state(opendata);
2707 ret = PTR_ERR(state);
2708 if (IS_ERR(state))
2709 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002710 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711 if (server->caps & NFS_CAP_POSIX_LOCK)
2712 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002713 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2714 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715
Trond Myklebust275bb302013-05-29 13:11:28 -04002716 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002717 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002718 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002719 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002720 alias = d_exact_alias(dentry, state->inode);
2721 if (!alias)
2722 alias = d_splice_alias(igrab(state->inode), dentry);
2723 /* d_splice_alias() can't fail here - it's a non-directory */
2724 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002725 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002726 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002727 }
2728 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002729 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 }
2731
Trond Myklebustc21443c2013-02-07 14:26:21 -05002732 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2733 if (ret != 0)
2734 goto out;
2735
David Howells2b0143b2015-03-17 22:25:59 +00002736 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002737 nfs_inode_attach_open_context(ctx);
2738 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2739 nfs4_schedule_stateid_recovery(server, state);
2740 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002741out:
2742 return ret;
2743}
2744
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002745/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002746 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 */
Andy Adamson82be4172012-05-23 05:02:35 -04002748static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002749 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002750 int flags,
2751 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002752 struct nfs4_label *label,
2753 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 struct nfs4_state_owner *sp;
2756 struct nfs4_state *state = NULL;
2757 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002758 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002759 struct dentry *dentry = ctx->dentry;
2760 struct rpc_cred *cred = ctx->cred;
2761 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2762 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002763 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002764 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002765 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002769 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2770 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2772 goto out_err;
2773 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002774 status = nfs4_recover_expired_lease(server);
2775 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002776 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002777 if (d_really_is_positive(dentry))
2778 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002779 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002780 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002781 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002782 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002783 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002784 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002785 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
David Quigley14c43f72013-05-22 12:50:43 -04002787 if (label) {
2788 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2789 if (IS_ERR(olabel)) {
2790 status = PTR_ERR(olabel);
2791 goto err_opendata_put;
2792 }
2793 }
2794
Trond Myklebuste911b812014-03-26 13:24:37 -07002795 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2796 if (!opendata->f_attr.mdsthreshold) {
2797 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2798 if (!opendata->f_attr.mdsthreshold)
2799 goto err_free_label;
2800 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002801 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002802 }
David Howells2b0143b2015-03-17 22:25:59 +00002803 if (d_really_is_positive(dentry))
2804 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002805
Trond Myklebust3efb9722013-05-29 13:17:04 -04002806 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002807 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002808 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002809 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002810
NeilBrownefcbc042015-07-30 13:00:56 +10002811 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002812 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002813 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002814 /*
2815 * send create attributes which was not set by open
2816 * with an extra setattr.
2817 */
2818 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2819 nfs_fattr_init(opendata->o_res.f_attr);
2820 status = nfs4_do_setattr(state->inode, cred,
2821 opendata->o_res.f_attr, sattr,
2822 state, label, olabel);
2823 if (status == 0) {
2824 nfs_setattr_update_inode(state->inode, sattr,
2825 opendata->o_res.f_attr);
2826 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2827 }
David Quigley1775fd32013-05-22 12:50:42 -04002828 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002829 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002830 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002831 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002832
Trond Myklebuste911b812014-03-26 13:24:37 -07002833 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002834 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002835 opendata->f_attr.mdsthreshold = NULL;
2836 }
Andy Adamson82be4172012-05-23 05:02:35 -04002837
David Quigley14c43f72013-05-22 12:50:43 -04002838 nfs4_label_free(olabel);
2839
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002840 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002843err_free_label:
2844 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002845err_opendata_put:
2846 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002847err_put_state_owner:
2848 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return status;
2851}
2852
2853
Andy Adamson82be4172012-05-23 05:02:35 -04002854static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002855 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002856 int flags,
2857 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002858 struct nfs4_label *label,
2859 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002861 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct nfs4_exception exception = { };
2863 struct nfs4_state *res;
2864 int status;
2865
2866 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002867 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002868 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002869 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (status == 0)
2871 break;
2872 /* NOTE: BAD_SEQID means the server and client disagree about the
2873 * book-keeping w.r.t. state-changing operations
2874 * (OPEN/CLOSE/LOCK/LOCKU...)
2875 * It is actually a sign of a bug on the client or on the server.
2876 *
2877 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002878 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 * have unhashed the old state_owner for us, and that we can
2880 * therefore safely retry using a new one. We should still warn
2881 * the user though...
2882 */
2883 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002884 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002885 " returned a bad sequence-id error!\n",
2886 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 exception.retry = 1;
2888 continue;
2889 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002890 /*
2891 * BAD_STATEID on OPEN means that the server cancelled our
2892 * state before it received the OPEN_CONFIRM.
2893 * Recover by retrying the request as per the discussion
2894 * on Page 181 of RFC3530.
2895 */
2896 if (status == -NFS4ERR_BAD_STATEID) {
2897 exception.retry = 1;
2898 continue;
2899 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002900 if (status == -EAGAIN) {
2901 /* We must have found a delegation */
2902 exception.retry = 1;
2903 continue;
2904 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002905 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2906 continue;
2907 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 status, &exception));
2909 } while (exception.retry);
2910 return res;
2911}
2912
Trond Myklebust8487c472016-06-26 08:44:35 -04002913static int _nfs4_do_setattr(struct inode *inode,
2914 struct nfs_setattrargs *arg,
2915 struct nfs_setattrres *res,
2916 struct rpc_cred *cred,
2917 struct nfs4_state *state)
2918{
2919 struct nfs_server *server = NFS_SERVER(inode);
2920 struct rpc_message msg = {
2921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2922 .rpc_argp = arg,
2923 .rpc_resp = res,
2924 .rpc_cred = cred,
2925 };
2926 struct rpc_cred *delegation_cred = NULL;
2927 unsigned long timestamp = jiffies;
2928 fmode_t fmode;
2929 bool truncate;
2930 int status;
2931
2932 nfs_fattr_init(res->fattr);
2933
2934 /* Servers should only apply open mode checks for file size changes */
2935 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2936 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2937
2938 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2939 /* Use that stateid */
2940 } else if (truncate && state != NULL) {
2941 struct nfs_lockowner lockowner = {
2942 .l_owner = current->files,
2943 .l_pid = current->tgid,
2944 };
2945 if (!nfs4_valid_open_stateid(state))
2946 return -EBADF;
2947 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2948 &arg->stateid, &delegation_cred) == -EIO)
2949 return -EBADF;
2950 } else
2951 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2952 if (delegation_cred)
2953 msg.rpc_cred = delegation_cred;
2954
2955 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2956
2957 put_rpccred(delegation_cred);
2958 if (status == 0 && state != NULL)
2959 renew_lease(server, timestamp);
2960 trace_nfs4_setattr(inode, &arg->stateid, status);
2961 return status;
2962}
2963
2964static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2965 struct nfs_fattr *fattr, struct iattr *sattr,
2966 struct nfs4_state *state, struct nfs4_label *ilabel,
2967 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002969 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002971 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 .iap = sattr,
2973 .server = server,
2974 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002975 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 };
2977 struct nfs_setattrres res = {
2978 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002979 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 .server = server,
2981 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002982 struct nfs4_exception exception = {
2983 .state = state,
2984 .inode = inode,
2985 .stateid = &arg.stateid,
2986 };
2987 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
David Quigleyaa9c2662013-05-22 12:50:44 -04002989 arg.bitmask = nfs4_bitmask(server, ilabel);
2990 if (ilabel)
2991 arg.bitmask = nfs4_bitmask(server, olabel);
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002994 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002995 switch (err) {
2996 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002997 if (!(sattr->ia_valid & ATTR_SIZE)) {
2998 pr_warn_once("NFSv4: server %s is incorrectly "
2999 "applying open mode checks to "
3000 "a SETATTR that is not "
3001 "changing file size.\n",
3002 server->nfs_client->cl_hostname);
3003 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003004 if (state && !(state->state & FMODE_WRITE)) {
3005 err = -EBADF;
3006 if (sattr->ia_valid & ATTR_OPEN)
3007 err = -EACCES;
3008 goto out;
3009 }
3010 }
3011 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return err;
3015}
3016
Peng Tao500d7012015-09-22 11:35:22 +08003017static bool
3018nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3019{
3020 if (inode == NULL || !nfs_have_layout(inode))
3021 return false;
3022
3023 return pnfs_wait_on_layoutreturn(inode, task);
3024}
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026struct nfs4_closedata {
3027 struct inode *inode;
3028 struct nfs4_state *state;
3029 struct nfs_closeargs arg;
3030 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003031 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003032 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003033 bool roc;
3034 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035};
3036
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003037static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003038{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003039 struct nfs4_closedata *calldata = data;
3040 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003041 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003042
Fred Isamanf7e89172011-01-06 11:36:32 +00003043 if (calldata->roc)
3044 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003045 nfs4_put_open_state(calldata->state);
3046 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003047 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003048 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003049 kfree(calldata);
3050}
3051
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003052static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003054 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003057 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Chuck Levera3ca5652012-03-01 17:00:40 -05003059 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003060 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3061 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003062 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 /* hmm. we are done with the inode, and in the process of freeing
3064 * the state_owner. we keep this around to process errors
3065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 switch (task->tk_status) {
3067 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003068 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003069 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003070 pnfs_roc_set_barrier(state->inode,
3071 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003072 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003073 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003074 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003076 case -NFS4ERR_EXPIRED:
3077 nfs4_free_revoked_stateid(server,
3078 &calldata->arg.stateid,
3079 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003080 case -NFS4ERR_OLD_STATEID:
3081 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003082 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003083 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003084 rpc_restart_call_prepare(task);
3085 goto out_release;
3086 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003087 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003090 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003091 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003092 goto out_release;
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003095 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3096 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003097out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003098 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003099 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003100 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003103static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003105 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003106 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003107 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003108 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003109 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003110
Chuck Levera3ca5652012-03-01 17:00:40 -05003111 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003112 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003113 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003114
Trond Myklebust88069f72009-12-08 08:33:16 -05003115 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003116 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003117 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3118 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3119 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003120 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003121 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003122 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003123 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003124 if (state->n_rdonly == 0)
3125 call_close |= is_rdonly;
3126 else if (is_rdonly)
3127 calldata->arg.fmode |= FMODE_READ;
3128 if (state->n_wronly == 0)
3129 call_close |= is_wronly;
3130 else if (is_wronly)
3131 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003132 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3133 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003134 } else if (is_rdwr)
3135 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3136
Trond Myklebust5cc78612016-11-14 11:19:56 -05003137 if (!nfs4_valid_open_stateid(state) ||
3138 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003139 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003140 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003141
3142 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003143 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003144 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003145 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003146
Peng Tao500d7012015-09-22 11:35:22 +08003147 if (nfs4_wait_on_layoutreturn(inode, task)) {
3148 nfs_release_seqid(calldata->arg.seqid);
3149 goto out_wait;
3150 }
3151
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003152 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003153 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003154 if (calldata->roc)
3155 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3156
Trond Myklebust6ae37332015-01-30 14:21:14 -05003157 calldata->arg.share_access =
3158 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3159 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003160
Trond Myklebust516a6af2005-10-27 22:12:41 -04003161 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003162 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003163 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003164 &calldata->arg.seq_args,
3165 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003166 task) != 0)
3167 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003168 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003169 return;
3170out_no_action:
3171 task->tk_action = NULL;
3172out_wait:
3173 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003177 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 .rpc_call_done = nfs4_close_done,
3179 .rpc_release = nfs4_free_closedata,
3180};
3181
Peng Taofe08c542014-07-03 13:05:01 +08003182static bool nfs4_roc(struct inode *inode)
3183{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003184 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003185 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003186 return pnfs_roc(inode);
3187}
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/*
3190 * It is possible for data to be read/written from a mem-mapped file
3191 * after the sys_close call (which hits the vfs layer as a flush).
3192 * This means that we can't safely call nfsv4 close on a file until
3193 * the inode is cleared. This in turn means that we are not good
3194 * NFSv4 citizens - we do not indicate to the server to update the file's
3195 * share state even when we are done with one of the three share
3196 * stateid's in the inode.
3197 *
3198 * NOTE: Caller must be holding the sp->so_owner semaphore!
3199 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003200int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003202 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003203 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003205 struct nfs4_state_owner *sp = state->owner;
3206 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003207 struct rpc_message msg = {
3208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3209 .rpc_cred = state->owner->so_cred,
3210 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003211 struct rpc_task_setup task_setup_data = {
3212 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003213 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003214 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003215 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003216 .flags = RPC_TASK_ASYNC,
3217 };
Trond Myklebust95121352005-10-18 14:20:12 -07003218 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003220 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3221 &task_setup_data.rpc_client, &msg);
3222
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003223 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003225 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003226 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003227 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003229 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3232 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003233 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003234 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003235 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003236 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003237 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003238 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003239 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003240 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003241 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003242
Trond Myklebust1174dd12010-12-21 10:52:24 -05003243 msg.rpc_argp = &calldata->arg;
3244 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003245 task_setup_data.callback_data = calldata;
3246 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003247 if (IS_ERR(task))
3248 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003249 status = 0;
3250 if (wait)
3251 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003252 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003253 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003254out_free_calldata:
3255 kfree(calldata);
3256out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003257 nfs4_put_open_state(state);
3258 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Trond Myklebust2b484292010-09-17 10:56:51 -04003262static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003263nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3264 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003267 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3268
3269 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebust565277f2007-10-15 18:17:53 -04003271 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003272 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003273
3274 nfs4_label_release_security(label);
3275
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003276 if (IS_ERR(state))
3277 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003278 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Trond Myklebust1185a552009-12-03 15:54:02 -05003281static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003282{
3283 if (ctx->state == NULL)
3284 return;
3285 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003286 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003287 else
Al Viro643168c2011-06-22 18:20:23 -04003288 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Trond Myklebustb944dba2013-11-04 15:20:20 -05003291#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3292#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003293#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3296{
Kinglong Mee8c612822015-08-26 21:12:58 +08003297 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003298 struct nfs4_server_caps_arg args = {
3299 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003300 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 struct nfs4_server_caps_res res = {};
3303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003305 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 .rpc_resp = &res,
3307 };
3308 int status;
3309
Kinglong Mee8c612822015-08-26 21:12:58 +08003310 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3311 FATTR4_WORD0_FH_EXPIRE_TYPE |
3312 FATTR4_WORD0_LINK_SUPPORT |
3313 FATTR4_WORD0_SYMLINK_SUPPORT |
3314 FATTR4_WORD0_ACLSUPPORT;
3315 if (minorversion)
3316 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3317
Bryan Schumaker7c513052011-03-24 17:12:24 +00003318 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003320 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003321 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003322 case 0:
3323 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3324 res.attr_bitmask[2] = 0;
3325 break;
3326 case 1:
3327 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3328 break;
3329 case 2:
3330 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003333 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3334 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3335 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3336 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003337 NFS_CAP_CTIME|NFS_CAP_MTIME|
3338 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003339 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3340 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 server->caps |= NFS_CAP_ACLS;
3342 if (res.has_links != 0)
3343 server->caps |= NFS_CAP_HARDLINKS;
3344 if (res.has_symlinks != 0)
3345 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003346 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3347 server->caps |= NFS_CAP_FILEID;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3349 server->caps |= NFS_CAP_MODE;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3351 server->caps |= NFS_CAP_NLINK;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3353 server->caps |= NFS_CAP_OWNER;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3355 server->caps |= NFS_CAP_OWNER_GROUP;
3356 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3357 server->caps |= NFS_CAP_ATIME;
3358 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3359 server->caps |= NFS_CAP_CTIME;
3360 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3361 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003362#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3363 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3364 server->caps |= NFS_CAP_SECURITY_LABEL;
3365#endif
3366 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3367 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003368 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003369
Trond Myklebusta65318b2009-03-11 14:10:28 -04003370 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3371 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3372 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003373 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003374 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3375 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003377 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return status;
3381}
3382
Trond Myklebust55a97592006-06-09 09:34:19 -04003383int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 struct nfs4_exception exception = { };
3386 int err;
3387 do {
3388 err = nfs4_handle_exception(server,
3389 _nfs4_server_capabilities(server, fhandle),
3390 &exception);
3391 } while (exception.retry);
3392 return err;
3393}
3394
3395static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3396 struct nfs_fsinfo *info)
3397{
David Quigleyaa9c2662013-05-22 12:50:44 -04003398 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003400 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 };
3402 struct nfs4_lookup_res res = {
3403 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003404 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 .fh = fhandle,
3406 };
3407 struct rpc_message msg = {
3408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3409 .rpc_argp = &args,
3410 .rpc_resp = &res,
3411 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003412
David Quigleyaa9c2662013-05-22 12:50:44 -04003413 bitmask[0] = nfs4_fattr_bitmap[0];
3414 bitmask[1] = nfs4_fattr_bitmap[1];
3415 /*
3416 * Process the label in the upcoming getfattr
3417 */
3418 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3419
Trond Myklebust0e574af2005-10-27 22:12:38 -04003420 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003421 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
3424static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *info)
3426{
3427 struct nfs4_exception exception = { };
3428 int err;
3429 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003430 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003431 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003432 switch (err) {
3433 case 0:
3434 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003435 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003436 default:
3437 err = nfs4_handle_exception(server, err, &exception);
3438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003440out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return err;
3442}
3443
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003444static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3445 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3446{
Trond Myklebustc2190662013-08-26 19:23:04 -04003447 struct rpc_auth_create_args auth_args = {
3448 .pseudoflavor = flavor,
3449 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003450 struct rpc_auth *auth;
3451 int ret;
3452
Trond Myklebustc2190662013-08-26 19:23:04 -04003453 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003454 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003455 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003456 goto out;
3457 }
3458 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003459out:
3460 return ret;
3461}
3462
Chuck Lever9a744ba2013-03-16 15:56:02 -04003463/*
3464 * Retry pseudoroot lookup with various security flavors. We do this when:
3465 *
3466 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3467 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3468 *
3469 * Returns zero on success, or a negative NFS4ERR value, or a
3470 * negative errno value.
3471 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003472static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003473 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003475 /* Per 3530bis 15.33.5 */
3476 static const rpc_authflavor_t flav_array[] = {
3477 RPC_AUTH_GSS_KRB5P,
3478 RPC_AUTH_GSS_KRB5I,
3479 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003480 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003481 RPC_AUTH_NULL,
3482 };
3483 int status = -EPERM;
3484 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003486 if (server->auth_info.flavor_len > 0) {
3487 /* try each flavor specified by user */
3488 for (i = 0; i < server->auth_info.flavor_len; i++) {
3489 status = nfs4_lookup_root_sec(server, fhandle, info,
3490 server->auth_info.flavors[i]);
3491 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3492 continue;
3493 break;
3494 }
3495 } else {
3496 /* no flavors specified by user, try default list */
3497 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3498 status = nfs4_lookup_root_sec(server, fhandle, info,
3499 flav_array[i]);
3500 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3501 continue;
3502 break;
3503 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003504 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003505
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003506 /*
3507 * -EACCESS could mean that the user doesn't have correct permissions
3508 * to access the mount. It could also mean that we tried to mount
3509 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3510 * existing mount programs don't handle -EACCES very well so it should
3511 * be mapped to -EPERM instead.
3512 */
3513 if (status == -EACCES)
3514 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003515 return status;
3516}
3517
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003518/**
3519 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3520 * @server: initialized nfs_server handle
3521 * @fhandle: we fill in the pseudo-fs root file handle
3522 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003523 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003524 *
3525 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003526 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003527int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003528 struct nfs_fsinfo *info,
3529 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003530{
Andre Przywarac7757072015-04-23 17:17:40 +01003531 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003532
Andre Przywarac7757072015-04-23 17:17:40 +01003533 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003534 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003535
3536 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003537 status = server->nfs_client->cl_mvops->find_root_sec(server,
3538 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 if (status == 0)
3541 status = nfs4_server_capabilities(server, fhandle);
3542 if (status == 0)
3543 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003544
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003545 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Bryan Schumakerbae36242012-05-10 15:07:31 -04003548static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3549 struct nfs_fsinfo *info)
3550{
3551 int error;
3552 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003553 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003554
3555 error = nfs4_server_capabilities(server, mntfh);
3556 if (error < 0) {
3557 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3558 return error;
3559 }
3560
David Quigley14c43f72013-05-22 12:50:43 -04003561 label = nfs4_label_alloc(server, GFP_KERNEL);
3562 if (IS_ERR(label))
3563 return PTR_ERR(label);
3564
David Quigley1775fd32013-05-22 12:50:42 -04003565 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003566 if (error < 0) {
3567 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003568 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003569 }
3570
3571 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3572 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3573 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3574
David Quigley14c43f72013-05-22 12:50:43 -04003575err_free_label:
3576 nfs4_label_free(label);
3577
Bryan Schumakerbae36242012-05-10 15:07:31 -04003578 return error;
3579}
3580
Manoj Naik6b97fd32006-06-09 09:34:29 -04003581/*
3582 * Get locations and (maybe) other attributes of a referral.
3583 * Note that we'll actually follow the referral later when
3584 * we detect fsid mismatch in inode revalidation
3585 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003586static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3587 const struct qstr *name, struct nfs_fattr *fattr,
3588 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589{
3590 int status = -ENOMEM;
3591 struct page *page = NULL;
3592 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003593
3594 page = alloc_page(GFP_KERNEL);
3595 if (page == NULL)
3596 goto out;
3597 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3598 if (locations == NULL)
3599 goto out;
3600
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003601 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003602 if (status != 0)
3603 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003604
3605 /*
3606 * If the fsid didn't change, this is a migration event, not a
3607 * referral. Cause us to drop into the exception handler, which
3608 * will kick off migration recovery.
3609 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003610 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003611 dprintk("%s: server did not return a different fsid for"
3612 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003613 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614 goto out;
3615 }
Andy Adamson533eb462011-06-13 18:25:56 -04003616 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3617 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003618
Andy Adamson533eb462011-06-13 18:25:56 -04003619 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003620 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003621 memset(fhandle, 0, sizeof(struct nfs_fh));
3622out:
3623 if (page)
3624 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003625 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003626 return status;
3627}
3628
David Quigley1775fd32013-05-22 12:50:42 -04003629static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3630 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 struct nfs4_getattr_arg args = {
3633 .fh = fhandle,
3634 .bitmask = server->attr_bitmask,
3635 };
3636 struct nfs4_getattr_res res = {
3637 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003638 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 .server = server,
3640 };
3641 struct rpc_message msg = {
3642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3643 .rpc_argp = &args,
3644 .rpc_resp = &res,
3645 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003646
3647 args.bitmask = nfs4_bitmask(server, label);
3648
Trond Myklebust0e574af2005-10-27 22:12:38 -04003649 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003650 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
David Quigley1775fd32013-05-22 12:50:42 -04003653static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3654 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 struct nfs4_exception exception = { };
3657 int err;
3658 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003659 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3660 trace_nfs4_getattr(server, fhandle, fattr, err);
3661 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 &exception);
3663 } while (exception.retry);
3664 return err;
3665}
3666
3667/*
3668 * The file is not closed if it is opened due to the a request to change
3669 * the size of the file. The open call will not be needed once the
3670 * VFS layer lookup-intents are implemented.
3671 *
3672 * Close is called when the inode is destroyed.
3673 * If we haven't opened the file for O_WRONLY, we
3674 * need to in the size_change case to obtain a stateid.
3675 *
3676 * Got race?
3677 * Because OPEN is always done by name in nfsv4, it is
3678 * possible that we opened a different file by the same
3679 * name. We can recognize this race condition, but we
3680 * can't do anything about it besides returning an error.
3681 *
3682 * This will be fixed with VFS changes (lookup-intent).
3683 */
3684static int
3685nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3686 struct iattr *sattr)
3687{
David Howells2b0143b2015-03-17 22:25:59 +00003688 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003689 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003690 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003691 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 int status;
3693
Peng Tao88ac8152014-09-12 11:04:10 +08003694 if (pnfs_ld_layoutret_on_setattr(inode) &&
3695 sattr->ia_valid & ATTR_SIZE &&
3696 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003697 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003698
Trond Myklebust0e574af2005-10-27 22:12:38 -04003699 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Andy Adamson26699402012-05-30 16:12:24 -04003701 /* Deal with open(O_TRUNC) */
3702 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003703 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003704
3705 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003706 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003707 return 0;
3708
Trond Myklebustd5308382005-11-04 15:33:38 -05003709 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003710 if (sattr->ia_valid & ATTR_FILE) {
3711 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003712
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003713 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003714 if (ctx) {
3715 cred = ctx->cred;
3716 state = ctx->state;
3717 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003718 }
3719
David Quigley14c43f72013-05-22 12:50:43 -04003720 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3721 if (IS_ERR(label))
3722 return PTR_ERR(label);
3723
3724 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003725 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003726 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003727 nfs_setsecurity(inode, fattr, label);
3728 }
David Quigley14c43f72013-05-22 12:50:43 -04003729 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return status;
3731}
3732
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003733static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3734 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003735 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003736{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003737 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003738 int status;
3739 struct nfs4_lookup_arg args = {
3740 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003741 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003742 .name = name,
3743 };
3744 struct nfs4_lookup_res res = {
3745 .server = server,
3746 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003747 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003748 .fh = fhandle,
3749 };
3750 struct rpc_message msg = {
3751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3752 .rpc_argp = &args,
3753 .rpc_resp = &res,
3754 };
3755
David Quigleyaa9c2662013-05-22 12:50:44 -04003756 args.bitmask = nfs4_bitmask(server, label);
3757
David Howells2b3de442006-08-22 20:06:09 -04003758 nfs_fattr_init(fattr);
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003761 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 dprintk("NFS reply lookup: %d\n", status);
3763 return status;
3764}
3765
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003766static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003767{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003768 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003769 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003770 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3771 fattr->nlink = 2;
3772}
3773
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003774static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003775 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003776 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003777{
3778 struct nfs4_exception exception = { };
3779 struct rpc_clnt *client = *clnt;
3780 int err;
3781 do {
David Quigley1775fd32013-05-22 12:50:42 -04003782 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003783 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003784 switch (err) {
3785 case -NFS4ERR_BADNAME:
3786 err = -ENOENT;
3787 goto out;
3788 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003789 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003790 if (err == -NFS4ERR_MOVED)
3791 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003792 goto out;
3793 case -NFS4ERR_WRONGSEC:
3794 err = -EPERM;
3795 if (client != *clnt)
3796 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003797 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003798 if (IS_ERR(client))
3799 return PTR_ERR(client);
3800
3801 exception.retry = 1;
3802 break;
3803 default:
3804 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3805 }
3806 } while (exception.retry);
3807
3808out:
3809 if (err == 0)
3810 *clnt = client;
3811 else if (client != *clnt)
3812 rpc_shutdown_client(client);
3813
3814 return err;
3815}
3816
Al Virobeffb8f2016-07-20 16:34:42 -04003817static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003818 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3819 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003821 int status;
3822 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003823
David Quigley1775fd32013-05-22 12:50:42 -04003824 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003825 if (client != NFS_CLIENT(dir)) {
3826 rpc_shutdown_client(client);
3827 nfs_fixup_secinfo_attributes(fattr);
3828 }
3829 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003832struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003833nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3835{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003836 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003837 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003838
David Quigley1775fd32013-05-22 12:50:42 -04003839 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003840 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003841 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003842 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3846{
Trond Myklebust76b32992007-08-10 17:45:11 -04003847 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 struct nfs4_accessargs args = {
3849 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003850 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003852 struct nfs4_accessres res = {
3853 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 struct rpc_message msg = {
3856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3857 .rpc_argp = &args,
3858 .rpc_resp = &res,
3859 .rpc_cred = entry->cred,
3860 };
3861 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003862 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 /*
3865 * Determine which access bits we want to ask for...
3866 */
3867 if (mode & MAY_READ)
3868 args.access |= NFS4_ACCESS_READ;
3869 if (S_ISDIR(inode->i_mode)) {
3870 if (mode & MAY_WRITE)
3871 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3872 if (mode & MAY_EXEC)
3873 args.access |= NFS4_ACCESS_LOOKUP;
3874 } else {
3875 if (mode & MAY_WRITE)
3876 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3877 if (mode & MAY_EXEC)
3878 args.access |= NFS4_ACCESS_EXECUTE;
3879 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003880
3881 res.fattr = nfs_alloc_fattr();
3882 if (res.fattr == NULL)
3883 return -ENOMEM;
3884
Bryan Schumaker7c513052011-03-24 17:12:24 +00003885 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003887 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003888 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003890 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return status;
3892}
3893
3894static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3895{
3896 struct nfs4_exception exception = { };
3897 int err;
3898 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003899 err = _nfs4_proc_access(inode, entry);
3900 trace_nfs4_access(inode, err);
3901 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 &exception);
3903 } while (exception.retry);
3904 return err;
3905}
3906
3907/*
3908 * TODO: For the time being, we don't try to get any attributes
3909 * along with any of the zero-copy operations READ, READDIR,
3910 * READLINK, WRITE.
3911 *
3912 * In the case of the first three, we want to put the GETATTR
3913 * after the read-type operation -- this is because it is hard
3914 * to predict the length of a GETATTR response in v4, and thus
3915 * align the READ data correctly. This means that the GETATTR
3916 * may end up partially falling into the page cache, and we should
3917 * shift it into the 'tail' of the xdr_buf before processing.
3918 * To do this efficiently, we need to know the total length
3919 * of data received, which doesn't seem to be available outside
3920 * of the RPC layer.
3921 *
3922 * In the case of WRITE, we also want to put the GETATTR after
3923 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003924 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 *
3926 * Both of these changes to the XDR layer would in fact be quite
3927 * minor, but I decided to leave them for a subsequent patch.
3928 */
3929static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3930 unsigned int pgbase, unsigned int pglen)
3931{
3932 struct nfs4_readlink args = {
3933 .fh = NFS_FH(inode),
3934 .pgbase = pgbase,
3935 .pglen = pglen,
3936 .pages = &page,
3937 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003938 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 struct rpc_message msg = {
3940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3941 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003942 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 };
3944
Bryan Schumaker7c513052011-03-24 17:12:24 +00003945 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 -07003946}
3947
3948static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3949 unsigned int pgbase, unsigned int pglen)
3950{
3951 struct nfs4_exception exception = { };
3952 int err;
3953 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003954 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3955 trace_nfs4_readlink(inode, err);
3956 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 &exception);
3958 } while (exception.retry);
3959 return err;
3960}
3961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003963 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965static int
3966nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003967 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
David Quigleyaa9c2662013-05-22 12:50:44 -04003969 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003970 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 int status = 0;
3973
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003974 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3975 if (IS_ERR(ctx))
3976 return PTR_ERR(ctx);
3977
David Quigleyaa9c2662013-05-22 12:50:44 -04003978 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3979
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003980 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003981 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (IS_ERR(state)) {
3983 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003987 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003988 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return status;
3990}
3991
Al Virobeffb8f2016-07-20 16:34:42 -04003992static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Trond Myklebust16e42952005-10-27 22:12:44 -04003994 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003995 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003997 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003999 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004000 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4004 .rpc_argp = &args,
4005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004007 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004008
Bryan Schumaker7c513052011-03-24 17:12:24 +00004009 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004010 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004011 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return status;
4013}
4014
Al Virobeffb8f2016-07-20 16:34:42 -04004015static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
4017 struct nfs4_exception exception = { };
4018 int err;
4019 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004020 err = _nfs4_proc_remove(dir, name);
4021 trace_nfs4_remove(dir, name, err);
4022 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 &exception);
4024 } while (exception.retry);
4025 return err;
4026}
4027
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004028static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004030 struct nfs_server *server = NFS_SERVER(dir);
4031 struct nfs_removeargs *args = msg->rpc_argp;
4032 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004034 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004036 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004037
4038 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004041static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4042{
Al Viro884be172016-04-28 23:56:31 -04004043 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004044 &data->args.seq_args,
4045 &data->res.seq_res,
4046 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004049static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004051 struct nfs_unlinkdata *data = task->tk_calldata;
4052 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004053
Trond Myklebust14516c32010-07-31 14:29:06 -04004054 if (!nfs4_sequence_done(task, &res->seq_res))
4055 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004056 if (nfs4_async_handle_error(task, res->server, NULL,
4057 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004058 return 0;
4059 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004060 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Jeff Laytond3d41522010-09-17 17:31:57 -04004063static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4064{
4065 struct nfs_server *server = NFS_SERVER(dir);
4066 struct nfs_renameargs *arg = msg->rpc_argp;
4067 struct nfs_renameres *res = msg->rpc_resp;
4068
4069 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004070 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004071 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004072}
4073
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004074static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4075{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004076 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4077 &data->args.seq_args,
4078 &data->res.seq_res,
4079 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004080}
4081
4082static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4083 struct inode *new_dir)
4084{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004085 struct nfs_renamedata *data = task->tk_calldata;
4086 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004087
4088 if (!nfs4_sequence_done(task, &res->seq_res))
4089 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004090 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004091 return 0;
4092
4093 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004094 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004095 return 1;
4096}
4097
Al Virobeffb8f2016-07-20 16:34:42 -04004098static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004100 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 struct nfs4_link_arg arg = {
4102 .fh = NFS_FH(inode),
4103 .dir_fh = NFS_FH(dir),
4104 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004105 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004107 struct nfs4_link_res res = {
4108 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004109 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4113 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004114 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004116 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Trond Myklebust136f2622010-04-16 16:22:49 -04004118 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004119 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004120 goto out;
4121
David Quigley14c43f72013-05-22 12:50:43 -04004122 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4123 if (IS_ERR(res.label)) {
4124 status = PTR_ERR(res.label);
4125 goto out;
4126 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004127 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004128
Bryan Schumaker7c513052011-03-24 17:12:24 +00004129 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 if (!status) {
4131 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004132 status = nfs_post_op_update_inode(inode, res.fattr);
4133 if (!status)
4134 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 }
David Quigley14c43f72013-05-22 12:50:43 -04004136
4137
4138 nfs4_label_free(res.label);
4139
Trond Myklebust136f2622010-04-16 16:22:49 -04004140out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004141 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return status;
4143}
4144
Al Virobeffb8f2016-07-20 16:34:42 -04004145static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 struct nfs4_exception exception = { };
4148 int err;
4149 do {
4150 err = nfs4_handle_exception(NFS_SERVER(inode),
4151 _nfs4_proc_link(inode, dir, name),
4152 &exception);
4153 } while (exception.retry);
4154 return err;
4155}
4156
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157struct nfs4_createdata {
4158 struct rpc_message msg;
4159 struct nfs4_create_arg arg;
4160 struct nfs4_create_res res;
4161 struct nfs_fh fh;
4162 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004163 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004164};
4165
4166static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004167 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168{
4169 struct nfs4_createdata *data;
4170
4171 data = kzalloc(sizeof(*data), GFP_KERNEL);
4172 if (data != NULL) {
4173 struct nfs_server *server = NFS_SERVER(dir);
4174
David Quigley14c43f72013-05-22 12:50:43 -04004175 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4176 if (IS_ERR(data->label))
4177 goto out_free;
4178
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4180 data->msg.rpc_argp = &data->arg;
4181 data->msg.rpc_resp = &data->res;
4182 data->arg.dir_fh = NFS_FH(dir);
4183 data->arg.server = server;
4184 data->arg.name = name;
4185 data->arg.attrs = sattr;
4186 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004187 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004188 data->res.server = server;
4189 data->res.fh = &data->fh;
4190 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004191 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193 }
4194 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004195out_free:
4196 kfree(data);
4197 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004198}
4199
4200static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4201{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004202 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004203 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 if (status == 0) {
4205 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004206 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 }
4208 return status;
4209}
4210
4211static void nfs4_free_createdata(struct nfs4_createdata *data)
4212{
David Quigley14c43f72013-05-22 12:50:43 -04004213 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004214 kfree(data);
4215}
4216
Chuck Lever4f390c12006-08-22 20:06:22 -04004217static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004218 struct page *page, unsigned int len, struct iattr *sattr,
4219 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 struct nfs4_createdata *data;
4222 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Chuck Lever94a6d752006-08-22 20:06:23 -04004224 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004225 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004226
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 status = -ENOMEM;
4228 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4229 if (data == NULL)
4230 goto out;
4231
4232 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4233 data->arg.u.symlink.pages = &page;
4234 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004235 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 status = nfs4_do_create(dir, dentry, data);
4238
4239 nfs4_free_createdata(data);
4240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return status;
4242}
4243
Chuck Lever4f390c12006-08-22 20:06:22 -04004244static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004245 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004248 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004250
4251 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004254 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4255 trace_nfs4_symlink(dir, &dentry->d_name, err);
4256 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 &exception);
4258 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004259
4260 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 return err;
4262}
4263
4264static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004265 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 struct nfs4_createdata *data;
4268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4271 if (data == NULL)
4272 goto out;
4273
David Quigley1775fd32013-05-22 12:50:42 -04004274 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 status = nfs4_do_create(dir, dentry, data);
4276
4277 nfs4_free_createdata(data);
4278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return status;
4280}
4281
4282static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4283 struct iattr *sattr)
4284{
4285 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004286 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004288
David Quigleyaa9c2662013-05-22 12:50:44 -04004289 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4290
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004291 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004293 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4294 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4295 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 &exception);
4297 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004298 nfs4_label_release_security(label);
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return err;
4301}
4302
4303static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004304 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
David Howells2b0143b2015-03-17 22:25:59 +00004306 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 struct nfs4_readdir_arg args = {
4308 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004309 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 .pgbase = 0,
4311 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004312 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004313 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 };
4315 struct nfs4_readdir_res res;
4316 struct rpc_message msg = {
4317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4318 .rpc_argp = &args,
4319 .rpc_resp = &res,
4320 .rpc_cred = cred,
4321 };
4322 int status;
4323
Al Viro6de14722013-09-16 10:53:17 -04004324 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4325 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004326 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004327 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004329 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 -05004330 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004331 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004332 status += args.pgbase;
4333 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004334
4335 nfs_invalidate_atime(dir);
4336
Harvey Harrison3110ff82008-05-02 13:42:44 -07004337 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return status;
4339}
4340
4341static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004342 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
4344 struct nfs4_exception exception = { };
4345 int err;
4346 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004347 err = _nfs4_proc_readdir(dentry, cred, cookie,
4348 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004349 trace_nfs4_readdir(d_inode(dentry), err);
4350 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 &exception);
4352 } while (exception.retry);
4353 return err;
4354}
4355
4356static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004357 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004359 struct nfs4_createdata *data;
4360 int mode = sattr->ia_mode;
4361 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004363 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4364 if (data == NULL)
4365 goto out;
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004368 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004370 data->arg.ftype = NF4BLK;
4371 data->arg.u.device.specdata1 = MAJOR(rdev);
4372 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 }
4374 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 data->arg.ftype = NF4CHR;
4376 data->arg.u.device.specdata1 = MAJOR(rdev);
4377 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004378 } else if (!S_ISSOCK(mode)) {
4379 status = -EINVAL;
4380 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
David Quigley1775fd32013-05-22 12:50:42 -04004382
David Quigleyaa9c2662013-05-22 12:50:44 -04004383 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004384 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004385out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 nfs4_free_createdata(data);
4387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return status;
4389}
4390
4391static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4392 struct iattr *sattr, dev_t rdev)
4393{
4394 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004395 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004397
David Quigleyaa9c2662013-05-22 12:50:44 -04004398 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4399
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004400 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004402 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4403 trace_nfs4_mknod(dir, &dentry->d_name, err);
4404 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 &exception);
4406 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004407
4408 nfs4_label_release_security(label);
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 return err;
4411}
4412
4413static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4414 struct nfs_fsstat *fsstat)
4415{
4416 struct nfs4_statfs_arg args = {
4417 .fh = fhandle,
4418 .bitmask = server->attr_bitmask,
4419 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004420 struct nfs4_statfs_res res = {
4421 .fsstat = fsstat,
4422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 struct rpc_message msg = {
4424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4425 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004426 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 };
4428
Trond Myklebust0e574af2005-10-27 22:12:38 -04004429 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004430 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
4433static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4434{
4435 struct nfs4_exception exception = { };
4436 int err;
4437 do {
4438 err = nfs4_handle_exception(server,
4439 _nfs4_proc_statfs(server, fhandle, fsstat),
4440 &exception);
4441 } while (exception.retry);
4442 return err;
4443}
4444
4445static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4446 struct nfs_fsinfo *fsinfo)
4447{
4448 struct nfs4_fsinfo_arg args = {
4449 .fh = fhandle,
4450 .bitmask = server->attr_bitmask,
4451 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004452 struct nfs4_fsinfo_res res = {
4453 .fsinfo = fsinfo,
4454 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 struct rpc_message msg = {
4456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4457 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004458 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 };
4460
Bryan Schumaker7c513052011-03-24 17:12:24 +00004461 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
4464static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4465{
4466 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004467 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
4469
4470 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004471 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004472 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004473 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004474 nfs4_set_lease_period(server->nfs_client,
4475 fsinfo->lease_time * HZ,
4476 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004477 break;
4478 }
4479 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 } while (exception.retry);
4481 return err;
4482}
4483
4484static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4485{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004486 int error;
4487
Trond Myklebust0e574af2005-10-27 22:12:38 -04004488 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004489 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004490 if (error == 0) {
4491 /* block layout checks this! */
4492 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004493 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004494 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004495
4496 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497}
4498
4499static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4500 struct nfs_pathconf *pathconf)
4501{
4502 struct nfs4_pathconf_arg args = {
4503 .fh = fhandle,
4504 .bitmask = server->attr_bitmask,
4505 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004506 struct nfs4_pathconf_res res = {
4507 .pathconf = pathconf,
4508 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 struct rpc_message msg = {
4510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4511 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004512 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 };
4514
4515 /* None of the pathconf attributes are mandatory to implement */
4516 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4517 memset(pathconf, 0, sizeof(*pathconf));
4518 return 0;
4519 }
4520
Trond Myklebust0e574af2005-10-27 22:12:38 -04004521 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004522 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523}
4524
4525static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4526 struct nfs_pathconf *pathconf)
4527{
4528 struct nfs4_exception exception = { };
4529 int err;
4530
4531 do {
4532 err = nfs4_handle_exception(server,
4533 _nfs4_proc_pathconf(server, fhandle, pathconf),
4534 &exception);
4535 } while (exception.retry);
4536 return err;
4537}
4538
Trond Myklebust5521abf2013-03-16 20:54:34 -04004539int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004540 const struct nfs_open_context *ctx,
4541 const struct nfs_lock_context *l_ctx,
4542 fmode_t fmode)
4543{
4544 const struct nfs_lockowner *lockowner = NULL;
4545
4546 if (l_ctx != NULL)
4547 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004548 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004549}
4550EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4551
Trond Myklebust5521abf2013-03-16 20:54:34 -04004552static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4553 const struct nfs_open_context *ctx,
4554 const struct nfs_lock_context *l_ctx,
4555 fmode_t fmode)
4556{
4557 nfs4_stateid current_stateid;
4558
Trond Myklebuste1253be2014-03-05 08:44:23 -05004559 /* If the current stateid represents a lost lock, then exit */
4560 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4561 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004562 return nfs4_stateid_match(stateid, &current_stateid);
4563}
4564
4565static bool nfs4_error_stateid_expired(int err)
4566{
4567 switch (err) {
4568 case -NFS4ERR_DELEG_REVOKED:
4569 case -NFS4ERR_ADMIN_REVOKED:
4570 case -NFS4ERR_BAD_STATEID:
4571 case -NFS4ERR_STALE_STATEID:
4572 case -NFS4ERR_OLD_STATEID:
4573 case -NFS4ERR_OPENMODE:
4574 case -NFS4ERR_EXPIRED:
4575 return true;
4576 }
4577 return false;
4578}
4579
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004580static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004582 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004584 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004585 if (task->tk_status < 0) {
4586 struct nfs4_exception exception = {
4587 .inode = hdr->inode,
4588 .state = hdr->args.context->state,
4589 .stateid = &hdr->args.stateid,
4590 };
4591 task->tk_status = nfs4_async_handle_exception(task,
4592 server, task->tk_status, &exception);
4593 if (exception.retry) {
4594 rpc_restart_call_prepare(task);
4595 return -EAGAIN;
4596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004600 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
Trond Myklebust5521abf2013-03-16 20:54:34 -04004604static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004605 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004606{
4607
4608 if (!nfs4_error_stateid_expired(task->tk_status) ||
4609 nfs4_stateid_is_current(&args->stateid,
4610 args->context,
4611 args->lock_context,
4612 FMODE_READ))
4613 return false;
4614 rpc_restart_call_prepare(task);
4615 return true;
4616}
4617
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004618static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004619{
4620
4621 dprintk("--> %s\n", __func__);
4622
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004624 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004626 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004627 if (task->tk_status > 0)
4628 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004629 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4630 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004631}
4632
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4634 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004636 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004637 if (!hdr->pgio_done_cb)
4638 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004639 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4644 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004645{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4647 &hdr->args.seq_args,
4648 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004649 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004650 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4652 hdr->args.lock_context,
4653 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004654 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004656 return -EIO;
4657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660static int nfs4_write_done_cb(struct rpc_task *task,
4661 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004664
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004666 if (task->tk_status < 0) {
4667 struct nfs4_exception exception = {
4668 .inode = hdr->inode,
4669 .state = hdr->args.context->state,
4670 .stateid = &hdr->args.stateid,
4671 };
4672 task->tk_status = nfs4_async_handle_exception(task,
4673 NFS_SERVER(inode), task->tk_status,
4674 &exception);
4675 if (exception.retry) {
4676 rpc_restart_call_prepare(task);
4677 return -EAGAIN;
4678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004680 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004682 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004683 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
Trond Myklebust5521abf2013-03-16 20:54:34 -04004687static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004688 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004689{
4690
4691 if (!nfs4_error_stateid_expired(task->tk_status) ||
4692 nfs4_stateid_is_current(&args->stateid,
4693 args->context,
4694 args->lock_context,
4695 FMODE_WRITE))
4696 return false;
4697 rpc_restart_call_prepare(task);
4698 return true;
4699}
4700
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004702{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004704 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004706 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4708 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004709}
4710
Trond Myklebust5a37f852012-04-28 14:55:16 -04004711static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004713{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004714 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004716 return false;
4717 /* Otherwise, request attributes if and only if we don't hold
4718 * a delegation
4719 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004720 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004721}
Fred Isamana69aef12011-03-03 15:13:47 +00004722
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004723static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4724 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004727
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4729 hdr->args.bitmask = NULL;
4730 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004731 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004733
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 if (!hdr->pgio_done_cb)
4735 hdr->pgio_done_cb = nfs4_write_done_cb;
4736 hdr->res.server = server;
4737 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Fred Isaman0b7c0152012-04-20 14:47:39 -04004743static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4744{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004745 nfs4_setup_sequence(NFS_SERVER(data->inode),
4746 &data->args.seq_args,
4747 &data->res.seq_res,
4748 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
Fred Isaman0b7c0152012-04-20 14:47:39 -04004751static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004754
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004755 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004756 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4757 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004758 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004759 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Fred Isaman0b7c0152012-04-20 14:47:39 -04004764static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004765{
4766 if (!nfs4_sequence_done(task, &data->res.seq_res))
4767 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004768 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004769}
4770
Fred Isaman0b7c0152012-04-20 14:47:39 -04004771static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004773 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004774
Fred Isaman0b7c0152012-04-20 14:47:39 -04004775 if (data->commit_done_cb == NULL)
4776 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004777 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004779 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004782struct nfs4_renewdata {
4783 struct nfs_client *client;
4784 unsigned long timestamp;
4785};
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787/*
4788 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4789 * standalone procedure for queueing an asynchronous RENEW.
4790 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004791static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004792{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004793 struct nfs4_renewdata *data = calldata;
4794 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004795
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004796 if (atomic_read(&clp->cl_count) > 1)
4797 nfs4_schedule_state_renewal(clp);
4798 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004799 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004800}
4801
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004802static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004804 struct nfs4_renewdata *data = calldata;
4805 struct nfs_client *clp = data->client;
4806 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004808 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004809 switch (task->tk_status) {
4810 case 0:
4811 break;
4812 case -NFS4ERR_LEASE_MOVED:
4813 nfs4_schedule_lease_moved_recovery(clp);
4814 break;
4815 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004816 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004817 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4818 return;
4819 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004820 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004821 return;
4822 }
4823 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004825 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004828static const struct rpc_call_ops nfs4_renew_ops = {
4829 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004830 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004831};
4832
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004833static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835 struct rpc_message msg = {
4836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4837 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004840 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004842 if (renew_flags == 0)
4843 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004844 if (!atomic_inc_not_zero(&clp->cl_count))
4845 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004846 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004847 if (data == NULL)
4848 return -ENOMEM;
4849 data->client = clp;
4850 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004851 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004855static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
4857 struct rpc_message msg = {
4858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4859 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004860 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 };
4862 unsigned long now = jiffies;
4863 int status;
4864
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004865 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (status < 0)
4867 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004868 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return 0;
4870}
4871
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004872static inline int nfs4_server_supports_acls(struct nfs_server *server)
4873{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004874 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004875}
4876
Trond Myklebust21f498c2012-08-24 10:59:25 -04004877/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4878 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004879 * the stack.
4880 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004881#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004882
Neil Hormane9e3d722011-03-04 19:26:03 -05004883static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004884 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004885{
4886 struct page *newpage, **spages;
4887 int rc = 0;
4888 size_t len;
4889 spages = pages;
4890
4891 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004892 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004893 newpage = alloc_page(GFP_KERNEL);
4894
4895 if (newpage == NULL)
4896 goto unwind;
4897 memcpy(page_address(newpage), buf, len);
4898 buf += len;
4899 buflen -= len;
4900 *pages++ = newpage;
4901 rc++;
4902 } while (buflen != 0);
4903
4904 return rc;
4905
4906unwind:
4907 for(; rc > 0; rc--)
4908 __free_page(spages[rc-1]);
4909 return -ENOMEM;
4910}
4911
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004912struct nfs4_cached_acl {
4913 int cached;
4914 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004915 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004916};
4917
4918static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004919{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004920 struct nfs_inode *nfsi = NFS_I(inode);
4921
4922 spin_lock(&inode->i_lock);
4923 kfree(nfsi->nfs4_acl);
4924 nfsi->nfs4_acl = acl;
4925 spin_unlock(&inode->i_lock);
4926}
4927
4928static void nfs4_zap_acl_attr(struct inode *inode)
4929{
4930 nfs4_set_cached_acl(inode, NULL);
4931}
4932
4933static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4934{
4935 struct nfs_inode *nfsi = NFS_I(inode);
4936 struct nfs4_cached_acl *acl;
4937 int ret = -ENOENT;
4938
4939 spin_lock(&inode->i_lock);
4940 acl = nfsi->nfs4_acl;
4941 if (acl == NULL)
4942 goto out;
4943 if (buf == NULL) /* user is just asking for length */
4944 goto out_len;
4945 if (acl->cached == 0)
4946 goto out;
4947 ret = -ERANGE; /* see getxattr(2) man page */
4948 if (acl->len > buflen)
4949 goto out;
4950 memcpy(buf, acl->data, acl->len);
4951out_len:
4952 ret = acl->len;
4953out:
4954 spin_unlock(&inode->i_lock);
4955 return ret;
4956}
4957
Sachin Prabhu5794d212012-04-17 14:36:40 +01004958static 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 +00004959{
4960 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004961 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004962
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004963 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004964 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004965 if (acl == NULL)
4966 goto out;
4967 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004968 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004969 } else {
4970 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4971 if (acl == NULL)
4972 goto out;
4973 acl->cached = 0;
4974 }
4975 acl->len = acl_len;
4976out:
4977 nfs4_set_cached_acl(inode, acl);
4978}
4979
Andy Adamsonbf118a32011-12-07 11:55:27 -05004980/*
4981 * The getxattr API returns the required buffer length when called with a
4982 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4983 * the required buf. On a NULL buf, we send a page of data to the server
4984 * guessing that the ACL request can be serviced by a page. If so, we cache
4985 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4986 * the cache. If not so, we throw away the page, and cache the required
4987 * length. The next getxattr call will then produce another round trip to
4988 * the server, this time with the input buf of the required size.
4989 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004990static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004991{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05004992 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004993 struct nfs_getaclargs args = {
4994 .fh = NFS_FH(inode),
4995 .acl_pages = pages,
4996 .acl_len = buflen,
4997 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004998 struct nfs_getaclres res = {
4999 .acl_len = buflen,
5000 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5003 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005004 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005005 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005006 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005007 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005008
Trond Myklebust21f498c2012-08-24 10:59:25 -04005009 if (npages > ARRAY_SIZE(pages))
5010 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005011
Andy Adamsonbf118a32011-12-07 11:55:27 -05005012 for (i = 0; i < npages; i++) {
5013 pages[i] = alloc_page(GFP_KERNEL);
5014 if (!pages[i])
5015 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005016 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005017
5018 /* for decoding across pages */
5019 res.acl_scratch = alloc_page(GFP_KERNEL);
5020 if (!res.acl_scratch)
5021 goto out_free;
5022
Andy Adamsonbf118a32011-12-07 11:55:27 -05005023 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005024
Peng Taode040be2012-01-10 22:42:47 +08005025 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005026 __func__, buf, buflen, npages, args.acl_len);
5027 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5028 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005029 if (ret)
5030 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005031
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005032 /* Handle the case where the passed-in buffer is too short */
5033 if (res.acl_flags & NFS4_ACL_TRUNC) {
5034 /* Did the user only issue a request for the acl length? */
5035 if (buf == NULL)
5036 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005037 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005038 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005039 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005040 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005041 if (buf) {
5042 if (res.acl_len > buflen) {
5043 ret = -ERANGE;
5044 goto out_free;
5045 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005046 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005047 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005048out_ok:
5049 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005050out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005051 for (i = 0; i < npages; i++)
5052 if (pages[i])
5053 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005054 if (res.acl_scratch)
5055 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056 return ret;
5057}
5058
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005059static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5060{
5061 struct nfs4_exception exception = { };
5062 ssize_t ret;
5063 do {
5064 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005065 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005066 if (ret >= 0)
5067 break;
5068 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5069 } while (exception.retry);
5070 return ret;
5071}
5072
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5074{
5075 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005076 int ret;
5077
5078 if (!nfs4_server_supports_acls(server))
5079 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005080 ret = nfs_revalidate_inode(server, inode);
5081 if (ret < 0)
5082 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005083 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5084 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005085 ret = nfs4_read_cached_acl(inode, buf, buflen);
5086 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005087 /* -ENOENT is returned if there is no ACL or if there is an ACL
5088 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 return ret;
5090 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005091}
5092
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005093static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005094{
5095 struct nfs_server *server = NFS_SERVER(inode);
5096 struct page *pages[NFS4ACL_MAXPAGES];
5097 struct nfs_setaclargs arg = {
5098 .fh = NFS_FH(inode),
5099 .acl_pages = pages,
5100 .acl_len = buflen,
5101 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005102 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005103 struct rpc_message msg = {
5104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5105 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005106 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005107 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005108 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005109 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005110
5111 if (!nfs4_server_supports_acls(server))
5112 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005113 if (npages > ARRAY_SIZE(pages))
5114 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005115 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005116 if (i < 0)
5117 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005118 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005119 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005120
5121 /*
5122 * Free each page after tx, so the only ref left is
5123 * held by the network stack
5124 */
5125 for (; i > 0; i--)
5126 put_page(pages[i-1]);
5127
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005128 /*
5129 * Acl update can result in inode attribute update.
5130 * so mark the attribute cache invalid.
5131 */
5132 spin_lock(&inode->i_lock);
5133 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5134 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005135 nfs_access_zap_cache(inode);
5136 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005137 return ret;
5138}
5139
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005140static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5141{
5142 struct nfs4_exception exception = { };
5143 int err;
5144 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005145 err = __nfs4_proc_set_acl(inode, buf, buflen);
5146 trace_nfs4_set_acl(inode, err);
5147 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005148 &exception);
5149 } while (exception.retry);
5150 return err;
5151}
5152
David Quigleyaa9c2662013-05-22 12:50:44 -04005153#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5154static int _nfs4_get_security_label(struct inode *inode, void *buf,
5155 size_t buflen)
5156{
5157 struct nfs_server *server = NFS_SERVER(inode);
5158 struct nfs_fattr fattr;
5159 struct nfs4_label label = {0, 0, buflen, buf};
5160
5161 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005162 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005163 .fh = NFS_FH(inode),
5164 .bitmask = bitmask,
5165 };
5166 struct nfs4_getattr_res res = {
5167 .fattr = &fattr,
5168 .label = &label,
5169 .server = server,
5170 };
5171 struct rpc_message msg = {
5172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005173 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005174 .rpc_resp = &res,
5175 };
5176 int ret;
5177
5178 nfs_fattr_init(&fattr);
5179
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005180 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005181 if (ret)
5182 return ret;
5183 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5184 return -ENOENT;
5185 if (buflen < label.len)
5186 return -ERANGE;
5187 return 0;
5188}
5189
5190static int nfs4_get_security_label(struct inode *inode, void *buf,
5191 size_t buflen)
5192{
5193 struct nfs4_exception exception = { };
5194 int err;
5195
5196 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5197 return -EOPNOTSUPP;
5198
5199 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005200 err = _nfs4_get_security_label(inode, buf, buflen);
5201 trace_nfs4_get_security_label(inode, err);
5202 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005203 &exception);
5204 } while (exception.retry);
5205 return err;
5206}
5207
5208static int _nfs4_do_set_security_label(struct inode *inode,
5209 struct nfs4_label *ilabel,
5210 struct nfs_fattr *fattr,
5211 struct nfs4_label *olabel)
5212{
5213
5214 struct iattr sattr = {0};
5215 struct nfs_server *server = NFS_SERVER(inode);
5216 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005217 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005218 .fh = NFS_FH(inode),
5219 .iap = &sattr,
5220 .server = server,
5221 .bitmask = bitmask,
5222 .label = ilabel,
5223 };
5224 struct nfs_setattrres res = {
5225 .fattr = fattr,
5226 .label = olabel,
5227 .server = server,
5228 };
5229 struct rpc_message msg = {
5230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005231 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005232 .rpc_resp = &res,
5233 };
5234 int status;
5235
Jeff Layton12207f62013-11-01 10:49:32 -04005236 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005237
Jeff Layton12207f62013-11-01 10:49:32 -04005238 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005239 if (status)
5240 dprintk("%s failed: %d\n", __func__, status);
5241
5242 return status;
5243}
5244
5245static int nfs4_do_set_security_label(struct inode *inode,
5246 struct nfs4_label *ilabel,
5247 struct nfs_fattr *fattr,
5248 struct nfs4_label *olabel)
5249{
5250 struct nfs4_exception exception = { };
5251 int err;
5252
5253 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005254 err = _nfs4_do_set_security_label(inode, ilabel,
5255 fattr, olabel);
5256 trace_nfs4_set_security_label(inode, err);
5257 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005258 &exception);
5259 } while (exception.retry);
5260 return err;
5261}
5262
5263static int
Al Viro59301222016-05-27 10:19:30 -04005264nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005265{
5266 struct nfs4_label ilabel, *olabel = NULL;
5267 struct nfs_fattr fattr;
5268 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005269 int status;
5270
5271 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5272 return -EOPNOTSUPP;
5273
5274 nfs_fattr_init(&fattr);
5275
5276 ilabel.pi = 0;
5277 ilabel.lfs = 0;
5278 ilabel.label = (char *)buf;
5279 ilabel.len = buflen;
5280
5281 cred = rpc_lookup_cred();
5282 if (IS_ERR(cred))
5283 return PTR_ERR(cred);
5284
5285 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5286 if (IS_ERR(olabel)) {
5287 status = -PTR_ERR(olabel);
5288 goto out;
5289 }
5290
5291 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5292 if (status == 0)
5293 nfs_setsecurity(inode, &fattr, olabel);
5294
5295 nfs4_label_free(olabel);
5296out:
5297 put_rpccred(cred);
5298 return status;
5299}
5300#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5301
5302
Chuck Leverf0920752012-05-21 22:45:41 -04005303static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5304 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005305{
5306 __be32 verf[2];
5307
Chuck Lever2c820d92012-05-21 22:45:33 -04005308 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5309 /* An impossible timestamp guarantees this value
5310 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005311 verf[0] = cpu_to_be32(U32_MAX);
5312 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005313 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005314 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005315 u64 ns = ktime_to_ns(nn->boot_time);
5316
5317 verf[0] = cpu_to_be32(ns >> 32);
5318 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005319 }
Chuck Levercd937102012-03-02 17:14:31 -05005320 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5321}
5322
Jeff Laytona3192682015-06-09 19:43:59 -04005323static int
5324nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005325{
Jeff Laytona3192682015-06-09 19:43:59 -04005326 size_t len;
5327 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005328
Trond Myklebustceb3a162015-01-03 15:16:04 -05005329 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005330 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005331
Jeff Laytona3192682015-06-09 19:43:59 -04005332 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005333 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005334 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5335 1 +
5336 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5337 1;
5338 rcu_read_unlock();
5339
5340 if (len > NFS4_OPAQUE_LIMIT + 1)
5341 return -EINVAL;
5342
5343 /*
5344 * Since this string is allocated at mount time, and held until the
5345 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5346 * about a memory-reclaim deadlock.
5347 */
5348 str = kmalloc(len, GFP_KERNEL);
5349 if (!str)
5350 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005351
Chuck Levere984a552012-09-14 17:24:21 -04005352 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005353 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005354 clp->cl_ipaddr,
5355 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5356 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005357 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005358
Jeff Laytona3192682015-06-09 19:43:59 -04005359 clp->cl_owner_id = str;
5360 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005361}
5362
Jeff Layton873e3852015-06-09 19:44:00 -04005363static int
5364nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005365{
Jeff Layton873e3852015-06-09 19:44:00 -04005366 size_t len;
5367 char *str;
5368
5369 len = 10 + 10 + 1 + 10 + 1 +
5370 strlen(nfs4_client_id_uniquifier) + 1 +
5371 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5372
5373 if (len > NFS4_OPAQUE_LIMIT + 1)
5374 return -EINVAL;
5375
5376 /*
5377 * Since this string is allocated at mount time, and held until the
5378 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5379 * about a memory-reclaim deadlock.
5380 */
5381 str = kmalloc(len, GFP_KERNEL);
5382 if (!str)
5383 return -ENOMEM;
5384
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005385 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005386 clp->rpc_ops->version, clp->cl_minorversion,
5387 nfs4_client_id_uniquifier,
5388 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005389 clp->cl_owner_id = str;
5390 return 0;
5391}
5392
5393static int
5394nfs4_init_uniform_client_string(struct nfs_client *clp)
5395{
Jeff Layton873e3852015-06-09 19:44:00 -04005396 size_t len;
5397 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005398
5399 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005400 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005401
5402 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005403 return nfs4_init_uniquifier_client_string(clp);
5404
5405 len = 10 + 10 + 1 + 10 + 1 +
5406 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5407
5408 if (len > NFS4_OPAQUE_LIMIT + 1)
5409 return -EINVAL;
5410
5411 /*
5412 * Since this string is allocated at mount time, and held until the
5413 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5414 * about a memory-reclaim deadlock.
5415 */
5416 str = kmalloc(len, GFP_KERNEL);
5417 if (!str)
5418 return -ENOMEM;
5419
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005420 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005421 clp->rpc_ops->version, clp->cl_minorversion,
5422 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005423 clp->cl_owner_id = str;
5424 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005425}
5426
Chuck Lever706cb8d2014-03-12 12:51:47 -04005427/*
5428 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5429 * services. Advertise one based on the address family of the
5430 * clientaddr.
5431 */
5432static unsigned int
5433nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5434{
5435 if (strchr(clp->cl_ipaddr, ':') != NULL)
5436 return scnprintf(buf, len, "tcp6");
5437 else
5438 return scnprintf(buf, len, "tcp");
5439}
5440
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005441static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5442{
5443 struct nfs4_setclientid *sc = calldata;
5444
5445 if (task->tk_status == 0)
5446 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5447}
5448
5449static const struct rpc_call_ops nfs4_setclientid_ops = {
5450 .rpc_call_done = nfs4_setclientid_done,
5451};
5452
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005453/**
5454 * nfs4_proc_setclientid - Negotiate client ID
5455 * @clp: state data structure
5456 * @program: RPC program for NFSv4 callback service
5457 * @port: IP port number for NFS4 callback service
5458 * @cred: RPC credential to use for this call
5459 * @res: where to place the result
5460 *
5461 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5462 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005463int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5464 unsigned short port, struct rpc_cred *cred,
5465 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466{
5467 nfs4_verifier sc_verifier;
5468 struct nfs4_setclientid setclientid = {
5469 .sc_verifier = &sc_verifier,
5470 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005471 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 };
5473 struct rpc_message msg = {
5474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5475 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005476 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005477 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005479 struct rpc_task *task;
5480 struct rpc_task_setup task_setup_data = {
5481 .rpc_client = clp->cl_rpcclient,
5482 .rpc_message = &msg,
5483 .callback_ops = &nfs4_setclientid_ops,
5484 .callback_data = &setclientid,
5485 .flags = RPC_TASK_TIMEOUT,
5486 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005487 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Chuck Leverde734832012-07-11 16:30:50 -04005489 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005490 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005491
5492 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5493 status = nfs4_init_uniform_client_string(clp);
5494 else
Jeff Laytona3192682015-06-09 19:43:59 -04005495 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005496
5497 if (status)
5498 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005499
Chuck Leverde734832012-07-11 16:30:50 -04005500 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005501 setclientid.sc_netid_len =
5502 nfs4_init_callback_netid(clp,
5503 setclientid.sc_netid,
5504 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005505 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005506 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 clp->cl_ipaddr, port >> 8, port & 255);
5508
Jeff Layton3a6bb732015-06-09 19:43:57 -04005509 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005510 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005511 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005512 task = rpc_run_task(&task_setup_data);
5513 if (IS_ERR(task)) {
5514 status = PTR_ERR(task);
5515 goto out;
5516 }
5517 status = task->tk_status;
5518 if (setclientid.sc_cred) {
5519 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5520 put_rpccred(setclientid.sc_cred);
5521 }
5522 rpc_put_task(task);
5523out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005524 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005525 dprintk("NFS reply setclientid: %d\n", status);
5526 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005529/**
5530 * nfs4_proc_setclientid_confirm - Confirm client ID
5531 * @clp: state data structure
5532 * @res: result of a previous SETCLIENTID
5533 * @cred: RPC credential to use for this call
5534 *
5535 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5536 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005537int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005538 struct nfs4_setclientid_res *arg,
5539 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 struct rpc_message msg = {
5542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005543 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005544 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 int status;
5547
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005548 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5549 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5550 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005551 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005552 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005553 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return status;
5555}
5556
Trond Myklebustfe650402006-01-03 09:55:18 +01005557struct nfs4_delegreturndata {
5558 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005559 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005560 struct nfs_fh fh;
5561 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005562 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005563 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005564 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005565 struct inode *inode;
5566 bool roc;
5567 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005568};
5569
Trond Myklebustfe650402006-01-03 09:55:18 +01005570static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5571{
5572 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005573
Trond Myklebust14516c32010-07-31 14:29:06 -04005574 if (!nfs4_sequence_done(task, &data->res.seq_res))
5575 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005576
Trond Myklebustca8acf82013-08-13 10:36:56 -04005577 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005578 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005579 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005580 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005581 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005582 case -NFS4ERR_ADMIN_REVOKED:
5583 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005584 case -NFS4ERR_EXPIRED:
5585 nfs4_free_revoked_stateid(data->res.server,
5586 data->args.stateid,
5587 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005588 case -NFS4ERR_BAD_STATEID:
5589 case -NFS4ERR_OLD_STATEID:
5590 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005591 task->tk_status = 0;
5592 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005593 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005594 if (nfs4_async_handle_error(task, data->res.server,
5595 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005596 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005597 return;
5598 }
5599 }
5600 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005601 if (data->roc && data->rpc_status == 0)
5602 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005603}
5604
5605static void nfs4_delegreturn_release(void *calldata)
5606{
Peng Tao039b7562014-07-03 13:05:02 +08005607 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005608 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005609
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005610 if (inode) {
5611 if (data->roc)
5612 pnfs_roc_release(inode);
5613 nfs_iput_and_deactive(inode);
5614 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005615 kfree(calldata);
5616}
5617
Andy Adamson938e1012009-04-01 09:22:28 -04005618static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5619{
5620 struct nfs4_delegreturndata *d_data;
5621
5622 d_data = (struct nfs4_delegreturndata *)data;
5623
Peng Tao500d7012015-09-22 11:35:22 +08005624 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5625 return;
5626
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005627 if (d_data->roc)
5628 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005629
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005630 nfs4_setup_sequence(d_data->res.server,
5631 &d_data->args.seq_args,
5632 &d_data->res.seq_res,
5633 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005634}
Andy Adamson938e1012009-04-01 09:22:28 -04005635
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005636static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005637 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005638 .rpc_call_done = nfs4_delegreturn_done,
5639 .rpc_release = nfs4_delegreturn_release,
5640};
5641
Trond Myklebuste6f81072008-01-24 18:14:34 -05005642static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005643{
5644 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005645 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005646 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005647 struct rpc_message msg = {
5648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5649 .rpc_cred = cred,
5650 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005651 struct rpc_task_setup task_setup_data = {
5652 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005653 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005654 .callback_ops = &nfs4_delegreturn_ops,
5655 .flags = RPC_TASK_ASYNC,
5656 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005657 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005658
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005659 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005660 if (data == NULL)
5661 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005662 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005663
5664 nfs4_state_protect(server->nfs_client,
5665 NFS_SP4_MACH_CRED_CLEANUP,
5666 &task_setup_data.rpc_client, &msg);
5667
Trond Myklebustfe650402006-01-03 09:55:18 +01005668 data->args.fhandle = &data->fh;
5669 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005670 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005671 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005672 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005673 data->res.fattr = &data->fattr;
5674 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005675 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005676 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005677 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005678 data->inode = nfs_igrab_and_active(inode);
5679 if (data->inode)
5680 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005681
Trond Myklebustc970aa82007-07-14 15:39:59 -04005682 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005683 msg.rpc_argp = &data->args;
5684 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005685 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005686 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005687 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005688 if (!issync)
5689 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005690 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005691 if (status != 0)
5692 goto out;
5693 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005694 if (status == 0)
5695 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5696 else
5697 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005698out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005699 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701}
5702
Trond Myklebuste6f81072008-01-24 18:14:34 -05005703int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
5705 struct nfs_server *server = NFS_SERVER(inode);
5706 struct nfs4_exception exception = { };
5707 int err;
5708 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005709 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005710 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 switch (err) {
5712 case -NFS4ERR_STALE_STATEID:
5713 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 case 0:
5715 return 0;
5716 }
5717 err = nfs4_handle_exception(server, err, &exception);
5718 } while (exception.retry);
5719 return err;
5720}
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5723{
5724 struct inode *inode = state->inode;
5725 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005726 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005727 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005729 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005731 struct nfs_lockt_res res = {
5732 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 };
5734 struct rpc_message msg = {
5735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5736 .rpc_argp = &arg,
5737 .rpc_resp = &res,
5738 .rpc_cred = state->owner->so_cred,
5739 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 struct nfs4_lock_state *lsp;
5741 int status;
5742
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005744 status = nfs4_set_lock_state(state, request);
5745 if (status != 0)
5746 goto out;
5747 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005748 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005749 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005750 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005751 switch (status) {
5752 case 0:
5753 request->fl_type = F_UNLCK;
5754 break;
5755 case -NFS4ERR_DENIED:
5756 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005758 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005759 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005760out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 return status;
5762}
5763
5764static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5765{
5766 struct nfs4_exception exception = { };
5767 int err;
5768
5769 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005770 err = _nfs4_proc_getlk(state, cmd, request);
5771 trace_nfs4_get_lock(request, state, cmd, err);
5772 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 &exception);
5774 } while (exception.retry);
5775 return err;
5776}
5777
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005778struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005779 struct nfs_locku_args arg;
5780 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005781 struct nfs4_lock_state *lsp;
5782 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005783 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005784 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005785 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005786};
5787
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005788static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5789 struct nfs_open_context *ctx,
5790 struct nfs4_lock_state *lsp,
5791 struct nfs_seqid *seqid)
5792{
5793 struct nfs4_unlockdata *p;
5794 struct inode *inode = lsp->ls_state->inode;
5795
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005796 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005797 if (p == NULL)
5798 return NULL;
5799 p->arg.fh = NFS_FH(inode);
5800 p->arg.fl = &p->fl;
5801 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005802 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005803 p->lsp = lsp;
5804 atomic_inc(&lsp->ls_count);
5805 /* Ensure we don't close file until we're done freeing locks! */
5806 p->ctx = get_nfs_open_context(ctx);
5807 memcpy(&p->fl, fl, sizeof(p->fl));
5808 p->server = NFS_SERVER(inode);
5809 return p;
5810}
5811
Trond Myklebust06f814a2006-01-03 09:55:07 +01005812static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005813{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005814 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005815 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005816 nfs4_put_lock_state(calldata->lsp);
5817 put_nfs_open_context(calldata->ctx);
5818 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005819}
5820
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005821static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005822{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005823 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005824
Trond Myklebust14516c32010-07-31 14:29:06 -04005825 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5826 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005827 switch (task->tk_status) {
5828 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005829 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005830 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005831 if (nfs4_update_lock_stateid(calldata->lsp,
5832 &calldata->res.stateid))
5833 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005834 case -NFS4ERR_ADMIN_REVOKED:
5835 case -NFS4ERR_EXPIRED:
5836 nfs4_free_revoked_stateid(calldata->server,
5837 &calldata->arg.stateid,
5838 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005839 case -NFS4ERR_BAD_STATEID:
5840 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005841 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005842 if (!nfs4_stateid_match(&calldata->arg.stateid,
5843 &calldata->lsp->ls_stateid))
5844 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005845 break;
5846 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005847 if (nfs4_async_handle_error(task, calldata->server,
5848 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005849 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005850 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005851 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852}
5853
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005854static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005856 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005859 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005860 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005861 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005862 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005863 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005864 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005865 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005866 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005867 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005868 &calldata->res.seq_res,
5869 task) != 0)
5870 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005871 return;
5872out_no_action:
5873 task->tk_action = NULL;
5874out_wait:
5875 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876}
5877
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005878static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005879 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005880 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005881 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005882};
5883
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005884static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5885 struct nfs_open_context *ctx,
5886 struct nfs4_lock_state *lsp,
5887 struct nfs_seqid *seqid)
5888{
5889 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005890 struct rpc_message msg = {
5891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5892 .rpc_cred = ctx->cred,
5893 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005894 struct rpc_task_setup task_setup_data = {
5895 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005896 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005897 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005898 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005899 .flags = RPC_TASK_ASYNC,
5900 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005901
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005902 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5903 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5904
Frank Filz137d6ac2007-07-09 15:32:29 -07005905 /* Ensure this is an unlock - when canceling a lock, the
5906 * canceled lock is passed in, and it won't be an unlock.
5907 */
5908 fl->fl_type = F_UNLCK;
5909
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005910 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5911 if (data == NULL) {
5912 nfs_free_seqid(seqid);
5913 return ERR_PTR(-ENOMEM);
5914 }
5915
Chuck Levera9c92d62013-08-09 12:48:18 -04005916 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005917 msg.rpc_argp = &data->arg;
5918 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005919 task_setup_data.callback_data = data;
5920 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921}
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5924{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005925 struct inode *inode = state->inode;
5926 struct nfs4_state_owner *sp = state->owner;
5927 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005928 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005929 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005930 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005931 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005932 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005933 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
Trond Myklebust9b073572006-06-29 16:38:34 -04005935 status = nfs4_set_lock_state(state, request);
5936 /* Unlock _before_ we do the RPC call */
5937 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005938 /* Exclude nfs_delegation_claim_locks() */
5939 mutex_lock(&sp->so_delegreturn_mutex);
5940 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005941 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005942 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005943 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005944 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005945 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005946 }
5947 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005948 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005949 if (status != 0)
5950 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005951 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005952 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005953 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5954 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005955 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5956 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005957 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005958 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005959 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005960 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005961 status = PTR_ERR(task);
5962 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005963 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005965 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005966out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005967 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005968 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005969 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005972struct nfs4_lockdata {
5973 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005974 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005975 struct nfs4_lock_state *lsp;
5976 struct nfs_open_context *ctx;
5977 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005978 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979 int rpc_status;
5980 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005981 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982};
5983
5984static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005985 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5986 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005987{
5988 struct nfs4_lockdata *p;
5989 struct inode *inode = lsp->ls_state->inode;
5990 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005991 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005993 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994 if (p == NULL)
5995 return NULL;
5996
5997 p->arg.fh = NFS_FH(inode);
5998 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005999 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006000 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006001 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006002 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6003 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006004 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006005 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006006 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006007 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006008 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006009 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006011 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012 atomic_inc(&lsp->ls_count);
6013 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006014 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006015 memcpy(&p->fl, fl, sizeof(p->fl));
6016 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006017out_free_seqid:
6018 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019out_free:
6020 kfree(p);
6021 return NULL;
6022}
6023
6024static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6025{
6026 struct nfs4_lockdata *data = calldata;
6027 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Harvey Harrison3110ff82008-05-02 13:42:44 -07006029 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006030 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006031 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006032 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006033 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006034 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006035 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006036 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006037 nfs4_stateid_copy(&data->arg.open_stateid,
6038 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006039 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006040 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006041 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006042 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006043 nfs4_stateid_copy(&data->arg.lock_stateid,
6044 &data->lsp->ls_stateid);
6045 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006046 if (!nfs4_valid_open_stateid(state)) {
6047 data->rpc_status = -EBADF;
6048 task->tk_action = NULL;
6049 goto out_release_open_seqid;
6050 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006051 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006052 if (nfs4_setup_sequence(data->server,
6053 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006054 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006055 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006056 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006057out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006058 nfs_release_seqid(data->arg.open_seqid);
6059out_release_lock_seqid:
6060 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006061out_wait:
6062 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006063 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006064}
6065
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6067{
6068 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006069 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006070
Harvey Harrison3110ff82008-05-02 13:42:44 -07006071 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006072
Trond Myklebust14516c32010-07-31 14:29:06 -04006073 if (!nfs4_sequence_done(task, &data->res.seq_res))
6074 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006075
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006077 switch (task->tk_status) {
6078 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006079 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006080 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006081 if (data->arg.new_lock) {
6082 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006083 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006084 rpc_restart_call_prepare(task);
6085 break;
6086 }
6087 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006088 if (data->arg.new_lock_owner != 0) {
6089 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6090 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6091 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6092 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6093 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006094 break;
6095 case -NFS4ERR_BAD_STATEID:
6096 case -NFS4ERR_OLD_STATEID:
6097 case -NFS4ERR_STALE_STATEID:
6098 case -NFS4ERR_EXPIRED:
6099 if (data->arg.new_lock_owner != 0) {
6100 if (!nfs4_stateid_match(&data->arg.open_stateid,
6101 &lsp->ls_state->open_stateid))
6102 rpc_restart_call_prepare(task);
6103 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6104 &lsp->ls_stateid))
6105 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006106 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006107 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108}
6109
6110static void nfs4_lock_release(void *calldata)
6111{
6112 struct nfs4_lockdata *data = calldata;
6113
Harvey Harrison3110ff82008-05-02 13:42:44 -07006114 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006115 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006116 if (data->cancelled != 0) {
6117 struct rpc_task *task;
6118 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6119 data->arg.lock_seqid);
6120 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006121 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006122 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006123 } else
6124 nfs_free_seqid(data->arg.lock_seqid);
6125 nfs4_put_lock_state(data->lsp);
6126 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006127 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006128 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006129 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130}
6131
6132static const struct rpc_call_ops nfs4_lock_ops = {
6133 .rpc_call_prepare = nfs4_lock_prepare,
6134 .rpc_call_done = nfs4_lock_done,
6135 .rpc_release = nfs4_lock_release,
6136};
6137
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006138static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6139{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006140 switch (error) {
6141 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006142 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006143 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006144 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006145 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006146 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006147 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006148 break;
6149 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006150 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006151 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006152 };
6153}
6154
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006155static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156{
6157 struct nfs4_lockdata *data;
6158 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006159 struct rpc_message msg = {
6160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6161 .rpc_cred = state->owner->so_cred,
6162 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006163 struct rpc_task_setup task_setup_data = {
6164 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006165 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006166 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006167 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006168 .flags = RPC_TASK_ASYNC,
6169 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 int ret;
6171
Harvey Harrison3110ff82008-05-02 13:42:44 -07006172 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006173 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006174 fl->fl_u.nfs4_fl.owner,
6175 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006176 if (data == NULL)
6177 return -ENOMEM;
6178 if (IS_SETLKW(cmd))
6179 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006180 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006181 msg.rpc_argp = &data->arg;
6182 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006183 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006184 if (recovery_type > NFS_LOCK_NEW) {
6185 if (recovery_type == NFS_LOCK_RECLAIM)
6186 data->arg.reclaim = NFS_LOCK_RECLAIM;
6187 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006188 } else
6189 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006190 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006191 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006192 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006193 ret = nfs4_wait_for_completion_rpc_task(task);
6194 if (ret == 0) {
6195 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006196 if (ret)
6197 nfs4_handle_setlk_error(data->server, data->lsp,
6198 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 } else
6200 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006201 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006202 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006203 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006204 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
6207static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6208{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006209 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006210 struct nfs4_exception exception = {
6211 .inode = state->inode,
6212 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006213 int err;
6214
6215 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006216 /* Cache the lock if possible... */
6217 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6218 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006219 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006220 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006221 break;
6222 nfs4_handle_exception(server, err, &exception);
6223 } while (exception.retry);
6224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225}
6226
6227static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6228{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006229 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006230 struct nfs4_exception exception = {
6231 .inode = state->inode,
6232 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006233 int err;
6234
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006235 err = nfs4_set_lock_state(state, request);
6236 if (err != 0)
6237 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006238 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006239 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6240 return 0;
6241 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006242 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6244 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006245 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006246 switch (err) {
6247 default:
6248 goto out;
6249 case -NFS4ERR_GRACE:
6250 case -NFS4ERR_DELAY:
6251 nfs4_handle_exception(server, err, &exception);
6252 err = 0;
6253 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006254 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006255out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006259#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006260static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6261{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006262 struct nfs4_lock_state *lsp;
6263 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006264
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006265 status = nfs4_set_lock_state(state, request);
6266 if (status != 0)
6267 return status;
6268 lsp = request->fl_u.nfs4_fl.owner;
6269 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6270 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6271 return 0;
6272 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006273 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006274}
6275#endif
6276
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6278{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006279 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006280 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006281 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006282 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
Trond Myklebust01c3b862006-06-29 16:38:39 -04006284 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006285 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006286 if (status < 0)
6287 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006288 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006289 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006290 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006291 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006292 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006293 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006294 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006295 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006296 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006297 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006298 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006299 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006300 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006301 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006302out:
6303 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 return status;
6305}
6306
6307static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6308{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006309 struct nfs4_exception exception = {
6310 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006311 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 int err;
6314
6315 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006316 err = _nfs4_proc_setlk(state, cmd, request);
6317 if (err == -NFS4ERR_DENIED)
6318 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006320 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 } while (exception.retry);
6322 return err;
6323}
6324
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006325#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6326#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6327
6328static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006329nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6330 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006331{
6332 int status = -ERESTARTSYS;
6333 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6334
6335 while(!signalled()) {
6336 status = nfs4_proc_setlk(state, cmd, request);
6337 if ((status != -EAGAIN) || IS_SETLK(cmd))
6338 break;
6339 freezable_schedule_timeout_interruptible(timeout);
6340 timeout *= 2;
6341 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6342 status = -ERESTARTSYS;
6343 }
6344 return status;
6345}
6346
Jeff Laytona1d617d82016-09-17 18:17:39 -04006347#ifdef CONFIG_NFS_V4_1
6348struct nfs4_lock_waiter {
6349 struct task_struct *task;
6350 struct inode *inode;
6351 struct nfs_lowner *owner;
6352 bool notified;
6353};
6354
6355static int
6356nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6357{
6358 int ret;
6359 struct cb_notify_lock_args *cbnl = key;
6360 struct nfs4_lock_waiter *waiter = wait->private;
6361 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6362 *wowner = waiter->owner;
6363
6364 /* Only wake if the callback was for the same owner */
6365 if (lowner->clientid != wowner->clientid ||
6366 lowner->id != wowner->id ||
6367 lowner->s_dev != wowner->s_dev)
6368 return 0;
6369
6370 /* Make sure it's for the right inode */
6371 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6372 return 0;
6373
6374 waiter->notified = true;
6375
6376 /* override "private" so we can use default_wake_function */
6377 wait->private = waiter->task;
6378 ret = autoremove_wake_function(wait, mode, flags, key);
6379 wait->private = waiter;
6380 return ret;
6381}
6382
6383static int
6384nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6385{
6386 int status = -ERESTARTSYS;
6387 unsigned long flags;
6388 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6389 struct nfs_server *server = NFS_SERVER(state->inode);
6390 struct nfs_client *clp = server->nfs_client;
6391 wait_queue_head_t *q = &clp->cl_lock_waitq;
6392 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6393 .id = lsp->ls_seqid.owner_id,
6394 .s_dev = server->s_dev };
6395 struct nfs4_lock_waiter waiter = { .task = current,
6396 .inode = state->inode,
6397 .owner = &owner,
6398 .notified = false };
6399 wait_queue_t wait;
6400
6401 /* Don't bother with waitqueue if we don't expect a callback */
6402 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6403 return nfs4_retry_setlk_simple(state, cmd, request);
6404
6405 init_wait(&wait);
6406 wait.private = &waiter;
6407 wait.func = nfs4_wake_lock_waiter;
6408 add_wait_queue(q, &wait);
6409
6410 while(!signalled()) {
6411 status = nfs4_proc_setlk(state, cmd, request);
6412 if ((status != -EAGAIN) || IS_SETLK(cmd))
6413 break;
6414
6415 status = -ERESTARTSYS;
6416 spin_lock_irqsave(&q->lock, flags);
6417 if (waiter.notified) {
6418 spin_unlock_irqrestore(&q->lock, flags);
6419 continue;
6420 }
6421 set_current_state(TASK_INTERRUPTIBLE);
6422 spin_unlock_irqrestore(&q->lock, flags);
6423
6424 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6425 }
6426
6427 finish_wait(q, &wait);
6428 return status;
6429}
6430#else /* !CONFIG_NFS_V4_1 */
6431static inline int
6432nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6433{
6434 return nfs4_retry_setlk_simple(state, cmd, request);
6435}
6436#endif
6437
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438static int
6439nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6440{
6441 struct nfs_open_context *ctx;
6442 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 int status;
6444
6445 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006446 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 state = ctx->state;
6448
6449 if (request->fl_start < 0 || request->fl_end < 0)
6450 return -EINVAL;
6451
Trond Myklebustd9531262009-07-21 19:22:38 -04006452 if (IS_GETLK(cmd)) {
6453 if (state != NULL)
6454 return nfs4_proc_getlk(state, F_GETLK, request);
6455 return 0;
6456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6459 return -EINVAL;
6460
Trond Myklebustd9531262009-07-21 19:22:38 -04006461 if (request->fl_type == F_UNLCK) {
6462 if (state != NULL)
6463 return nfs4_proc_unlck(state, cmd, request);
6464 return 0;
6465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
Trond Myklebustd9531262009-07-21 19:22:38 -04006467 if (state == NULL)
6468 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006469
6470 if ((request->fl_flags & FL_POSIX) &&
6471 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6472 return -ENOLCK;
6473
Trond Myklebust55725512012-04-18 12:48:35 -04006474 /*
6475 * Don't rely on the VFS having checked the file open mode,
6476 * since it won't do this for flock() locks.
6477 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006478 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006479 case F_RDLCK:
6480 if (!(filp->f_mode & FMODE_READ))
6481 return -EBADF;
6482 break;
6483 case F_WRLCK:
6484 if (!(filp->f_mode & FMODE_WRITE))
6485 return -EBADF;
6486 }
6487
Jeff Layton1ea67db2016-09-17 18:17:37 -04006488 status = nfs4_set_lock_state(state, request);
6489 if (status != 0)
6490 return status;
6491
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006492 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493}
6494
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006495int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006496{
6497 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006498 int err;
6499
6500 err = nfs4_set_lock_state(state, fl);
6501 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006502 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006503 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006504 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006505}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006506
Trond Myklebustcf470c32012-03-07 13:49:12 -05006507struct nfs_release_lockowner_data {
6508 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006509 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006510 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006511 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006512 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006513};
6514
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006515static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6516{
6517 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006518 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006519 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6520 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006521 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006522 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006523}
6524
6525static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6526{
6527 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006528 struct nfs_server *server = data->server;
6529
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006530 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006531
6532 switch (task->tk_status) {
6533 case 0:
6534 renew_lease(server, data->timestamp);
6535 break;
6536 case -NFS4ERR_STALE_CLIENTID:
6537 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006538 nfs4_schedule_lease_recovery(server->nfs_client);
6539 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006540 case -NFS4ERR_LEASE_MOVED:
6541 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006542 if (nfs4_async_handle_error(task, server,
6543 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006544 rpc_restart_call_prepare(task);
6545 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006546}
6547
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006548static void nfs4_release_lockowner_release(void *calldata)
6549{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006550 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006551 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006552 kfree(calldata);
6553}
6554
Trond Myklebust17280172012-03-11 13:11:00 -04006555static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006556 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6557 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006558 .rpc_release = nfs4_release_lockowner_release,
6559};
6560
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006561static void
6562nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006563{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006564 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006565 struct rpc_message msg = {
6566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6567 };
6568
6569 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006570 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006571
Trond Myklebustcf470c32012-03-07 13:49:12 -05006572 data = kmalloc(sizeof(*data), GFP_NOFS);
6573 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006574 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006575 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006576 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006577 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6578 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6579 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006580
Trond Myklebustcf470c32012-03-07 13:49:12 -05006581 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006582 msg.rpc_resp = &data->res;
6583 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006584 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006585}
6586
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006587#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6588
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006589static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006590 struct dentry *unused, struct inode *inode,
6591 const char *key, const void *buf,
6592 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006593{
Al Viro59301222016-05-27 10:19:30 -04006594 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006595}
6596
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006597static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006598 struct dentry *unused, struct inode *inode,
6599 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006600{
Al Virob2968212016-04-10 20:48:24 -04006601 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006602}
6603
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006604static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006605{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006606 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006607}
6608
David Quigleyc9bccef2013-05-22 12:50:45 -04006609#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006610
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006611static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006612 struct dentry *unused, struct inode *inode,
6613 const char *key, const void *buf,
6614 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006615{
6616 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006617 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006618
6619 return -EOPNOTSUPP;
6620}
6621
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006622static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006623 struct dentry *unused, struct inode *inode,
6624 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006625{
6626 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006627 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006628 return -EOPNOTSUPP;
6629}
6630
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006631static ssize_t
6632nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006633{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006634 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006635
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006636 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6637 len = security_inode_listsecurity(inode, list, list_len);
6638 if (list_len && len > list_len)
6639 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006640 }
6641 return len;
6642}
6643
6644static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6645 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006646 .get = nfs4_xattr_get_nfs4_label,
6647 .set = nfs4_xattr_set_nfs4_label,
6648};
David Quigleyc9bccef2013-05-22 12:50:45 -04006649
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006650#else
6651
6652static ssize_t
6653nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6654{
6655 return 0;
6656}
6657
6658#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006659
Andy Adamson533eb462011-06-13 18:25:56 -04006660/*
6661 * nfs_fhget will use either the mounted_on_fileid or the fileid
6662 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006663static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6664{
Andy Adamson533eb462011-06-13 18:25:56 -04006665 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6666 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6667 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006668 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006669 return;
6670
6671 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006672 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006673 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6674 fattr->nlink = 2;
6675}
6676
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006677static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6678 const struct qstr *name,
6679 struct nfs4_fs_locations *fs_locations,
6680 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006681{
6682 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006683 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006684 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006685 };
6686 struct nfs4_fs_locations_arg args = {
6687 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006688 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006689 .page = page,
6690 .bitmask = bitmask,
6691 };
Benny Halevy22958462009-04-01 09:22:02 -04006692 struct nfs4_fs_locations_res res = {
6693 .fs_locations = fs_locations,
6694 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006695 struct rpc_message msg = {
6696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6697 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006698 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006699 };
6700 int status;
6701
Harvey Harrison3110ff82008-05-02 13:42:44 -07006702 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006703
6704 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6705 * is not supported */
6706 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6707 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6708 else
6709 bitmask[0] |= FATTR4_WORD0_FILEID;
6710
Trond Myklebustc228fd32007-01-13 02:28:11 -05006711 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006712 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006713 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006714 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006715 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006716 return status;
6717}
6718
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006719int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6720 const struct qstr *name,
6721 struct nfs4_fs_locations *fs_locations,
6722 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006723{
6724 struct nfs4_exception exception = { };
6725 int err;
6726 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006727 err = _nfs4_proc_fs_locations(client, dir, name,
6728 fs_locations, page);
6729 trace_nfs4_get_fs_locations(dir, name, err);
6730 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006731 &exception);
6732 } while (exception.retry);
6733 return err;
6734}
6735
Chuck Leverb03d7352013-10-17 14:12:50 -04006736/*
6737 * This operation also signals the server that this client is
6738 * performing migration recovery. The server can stop returning
6739 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6740 * appended to this compound to identify the client ID which is
6741 * performing recovery.
6742 */
6743static int _nfs40_proc_get_locations(struct inode *inode,
6744 struct nfs4_fs_locations *locations,
6745 struct page *page, struct rpc_cred *cred)
6746{
6747 struct nfs_server *server = NFS_SERVER(inode);
6748 struct rpc_clnt *clnt = server->client;
6749 u32 bitmask[2] = {
6750 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6751 };
6752 struct nfs4_fs_locations_arg args = {
6753 .clientid = server->nfs_client->cl_clientid,
6754 .fh = NFS_FH(inode),
6755 .page = page,
6756 .bitmask = bitmask,
6757 .migration = 1, /* skip LOOKUP */
6758 .renew = 1, /* append RENEW */
6759 };
6760 struct nfs4_fs_locations_res res = {
6761 .fs_locations = locations,
6762 .migration = 1,
6763 .renew = 1,
6764 };
6765 struct rpc_message msg = {
6766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6767 .rpc_argp = &args,
6768 .rpc_resp = &res,
6769 .rpc_cred = cred,
6770 };
6771 unsigned long now = jiffies;
6772 int status;
6773
6774 nfs_fattr_init(&locations->fattr);
6775 locations->server = server;
6776 locations->nlocations = 0;
6777
6778 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6779 nfs4_set_sequence_privileged(&args.seq_args);
6780 status = nfs4_call_sync_sequence(clnt, server, &msg,
6781 &args.seq_args, &res.seq_res);
6782 if (status)
6783 return status;
6784
6785 renew_lease(server, now);
6786 return 0;
6787}
6788
6789#ifdef CONFIG_NFS_V4_1
6790
6791/*
6792 * This operation also signals the server that this client is
6793 * performing migration recovery. The server can stop asserting
6794 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6795 * performing this operation is identified in the SEQUENCE
6796 * operation in this compound.
6797 *
6798 * When the client supports GETATTR(fs_locations_info), it can
6799 * be plumbed in here.
6800 */
6801static int _nfs41_proc_get_locations(struct inode *inode,
6802 struct nfs4_fs_locations *locations,
6803 struct page *page, struct rpc_cred *cred)
6804{
6805 struct nfs_server *server = NFS_SERVER(inode);
6806 struct rpc_clnt *clnt = server->client;
6807 u32 bitmask[2] = {
6808 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6809 };
6810 struct nfs4_fs_locations_arg args = {
6811 .fh = NFS_FH(inode),
6812 .page = page,
6813 .bitmask = bitmask,
6814 .migration = 1, /* skip LOOKUP */
6815 };
6816 struct nfs4_fs_locations_res res = {
6817 .fs_locations = locations,
6818 .migration = 1,
6819 };
6820 struct rpc_message msg = {
6821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6822 .rpc_argp = &args,
6823 .rpc_resp = &res,
6824 .rpc_cred = cred,
6825 };
6826 int status;
6827
6828 nfs_fattr_init(&locations->fattr);
6829 locations->server = server;
6830 locations->nlocations = 0;
6831
6832 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6833 nfs4_set_sequence_privileged(&args.seq_args);
6834 status = nfs4_call_sync_sequence(clnt, server, &msg,
6835 &args.seq_args, &res.seq_res);
6836 if (status == NFS4_OK &&
6837 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6838 status = -NFS4ERR_LEASE_MOVED;
6839 return status;
6840}
6841
6842#endif /* CONFIG_NFS_V4_1 */
6843
6844/**
6845 * nfs4_proc_get_locations - discover locations for a migrated FSID
6846 * @inode: inode on FSID that is migrating
6847 * @locations: result of query
6848 * @page: buffer
6849 * @cred: credential to use for this operation
6850 *
6851 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6852 * operation failed, or a negative errno if a local error occurred.
6853 *
6854 * On success, "locations" is filled in, but if the server has
6855 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6856 * asserted.
6857 *
6858 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6859 * from this client that require migration recovery.
6860 */
6861int nfs4_proc_get_locations(struct inode *inode,
6862 struct nfs4_fs_locations *locations,
6863 struct page *page, struct rpc_cred *cred)
6864{
6865 struct nfs_server *server = NFS_SERVER(inode);
6866 struct nfs_client *clp = server->nfs_client;
6867 const struct nfs4_mig_recovery_ops *ops =
6868 clp->cl_mvops->mig_recovery_ops;
6869 struct nfs4_exception exception = { };
6870 int status;
6871
6872 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6873 (unsigned long long)server->fsid.major,
6874 (unsigned long long)server->fsid.minor,
6875 clp->cl_hostname);
6876 nfs_display_fhandle(NFS_FH(inode), __func__);
6877
6878 do {
6879 status = ops->get_locations(inode, locations, page, cred);
6880 if (status != -NFS4ERR_DELAY)
6881 break;
6882 nfs4_handle_exception(server, status, &exception);
6883 } while (exception.retry);
6884 return status;
6885}
6886
Chuck Lever44c99932013-10-17 14:13:30 -04006887/*
6888 * This operation also signals the server that this client is
6889 * performing "lease moved" recovery. The server can stop
6890 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6891 * is appended to this compound to identify the client ID which is
6892 * performing recovery.
6893 */
6894static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6895{
6896 struct nfs_server *server = NFS_SERVER(inode);
6897 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6898 struct rpc_clnt *clnt = server->client;
6899 struct nfs4_fsid_present_arg args = {
6900 .fh = NFS_FH(inode),
6901 .clientid = clp->cl_clientid,
6902 .renew = 1, /* append RENEW */
6903 };
6904 struct nfs4_fsid_present_res res = {
6905 .renew = 1,
6906 };
6907 struct rpc_message msg = {
6908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6909 .rpc_argp = &args,
6910 .rpc_resp = &res,
6911 .rpc_cred = cred,
6912 };
6913 unsigned long now = jiffies;
6914 int status;
6915
6916 res.fh = nfs_alloc_fhandle();
6917 if (res.fh == NULL)
6918 return -ENOMEM;
6919
6920 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6921 nfs4_set_sequence_privileged(&args.seq_args);
6922 status = nfs4_call_sync_sequence(clnt, server, &msg,
6923 &args.seq_args, &res.seq_res);
6924 nfs_free_fhandle(res.fh);
6925 if (status)
6926 return status;
6927
6928 do_renew_lease(clp, now);
6929 return 0;
6930}
6931
6932#ifdef CONFIG_NFS_V4_1
6933
6934/*
6935 * This operation also signals the server that this client is
6936 * performing "lease moved" recovery. The server can stop asserting
6937 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6938 * this operation is identified in the SEQUENCE operation in this
6939 * compound.
6940 */
6941static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6942{
6943 struct nfs_server *server = NFS_SERVER(inode);
6944 struct rpc_clnt *clnt = server->client;
6945 struct nfs4_fsid_present_arg args = {
6946 .fh = NFS_FH(inode),
6947 };
6948 struct nfs4_fsid_present_res res = {
6949 };
6950 struct rpc_message msg = {
6951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6952 .rpc_argp = &args,
6953 .rpc_resp = &res,
6954 .rpc_cred = cred,
6955 };
6956 int status;
6957
6958 res.fh = nfs_alloc_fhandle();
6959 if (res.fh == NULL)
6960 return -ENOMEM;
6961
6962 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6963 nfs4_set_sequence_privileged(&args.seq_args);
6964 status = nfs4_call_sync_sequence(clnt, server, &msg,
6965 &args.seq_args, &res.seq_res);
6966 nfs_free_fhandle(res.fh);
6967 if (status == NFS4_OK &&
6968 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6969 status = -NFS4ERR_LEASE_MOVED;
6970 return status;
6971}
6972
6973#endif /* CONFIG_NFS_V4_1 */
6974
6975/**
6976 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6977 * @inode: inode on FSID to check
6978 * @cred: credential to use for this operation
6979 *
6980 * Server indicates whether the FSID is present, moved, or not
6981 * recognized. This operation is necessary to clear a LEASE_MOVED
6982 * condition for this client ID.
6983 *
6984 * Returns NFS4_OK if the FSID is present on this server,
6985 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6986 * NFS4ERR code if some error occurred on the server, or a
6987 * negative errno if a local failure occurred.
6988 */
6989int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6990{
6991 struct nfs_server *server = NFS_SERVER(inode);
6992 struct nfs_client *clp = server->nfs_client;
6993 const struct nfs4_mig_recovery_ops *ops =
6994 clp->cl_mvops->mig_recovery_ops;
6995 struct nfs4_exception exception = { };
6996 int status;
6997
6998 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6999 (unsigned long long)server->fsid.major,
7000 (unsigned long long)server->fsid.minor,
7001 clp->cl_hostname);
7002 nfs_display_fhandle(NFS_FH(inode), __func__);
7003
7004 do {
7005 status = ops->fsid_present(inode, cred);
7006 if (status != -NFS4ERR_DELAY)
7007 break;
7008 nfs4_handle_exception(server, status, &exception);
7009 } while (exception.retry);
7010 return status;
7011}
7012
Andy Adamson5ec16a82013-08-08 10:57:55 -04007013/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007014 * If 'use_integrity' is true and the state managment nfs_client
7015 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7016 * and the machine credential as per RFC3530bis and RFC5661 Security
7017 * Considerations sections. Otherwise, just use the user cred with the
7018 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007019 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007020static 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 +00007021{
7022 int status;
7023 struct nfs4_secinfo_arg args = {
7024 .dir_fh = NFS_FH(dir),
7025 .name = name,
7026 };
7027 struct nfs4_secinfo_res res = {
7028 .flavors = flavors,
7029 };
7030 struct rpc_message msg = {
7031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7032 .rpc_argp = &args,
7033 .rpc_resp = &res,
7034 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007035 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007036 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007037
7038 if (use_integrity) {
7039 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007040 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7041 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007042 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007043
7044 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007045
7046 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7047 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7048
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007049 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7050 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007051 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007052
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007053 if (cred)
7054 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007055
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007056 return status;
7057}
7058
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007059int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7060 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007061{
7062 struct nfs4_exception exception = { };
7063 int err;
7064 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007065 err = -NFS4ERR_WRONGSEC;
7066
7067 /* try to use integrity protection with machine cred */
7068 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7069 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7070
7071 /*
7072 * if unable to use integrity protection, or SECINFO with
7073 * integrity protection returns NFS4ERR_WRONGSEC (which is
7074 * disallowed by spec, but exists in deployed servers) use
7075 * the current filesystem's rpc_client and the user cred.
7076 */
7077 if (err == -NFS4ERR_WRONGSEC)
7078 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7079
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007080 trace_nfs4_secinfo(dir, name, err);
7081 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007082 &exception);
7083 } while (exception.retry);
7084 return err;
7085}
7086
Andy Adamson557134a2009-04-01 09:21:53 -04007087#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007088/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007089 * Check the exchange flags returned by the server for invalid flags, having
7090 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7091 * DS flags set.
7092 */
7093static int nfs4_check_cl_exchange_flags(u32 flags)
7094{
7095 if (flags & ~EXCHGID4_FLAG_MASK_R)
7096 goto out_inval;
7097 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7098 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7099 goto out_inval;
7100 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7101 goto out_inval;
7102 return NFS_OK;
7103out_inval:
7104 return -NFS4ERR_INVAL;
7105}
7106
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007107static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007108nfs41_same_server_scope(struct nfs41_server_scope *a,
7109 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007110{
7111 if (a->server_scope_sz == b->server_scope_sz &&
7112 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7113 return true;
7114
7115 return false;
7116}
7117
Andy Adamson02a95de2016-02-05 16:08:37 -05007118static void
7119nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7120{
7121}
7122
7123static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7124 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7125};
7126
Andy Adamson357f54d2010-12-14 10:11:57 -05007127/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007128 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007129 *
7130 * The 4.1 client currently uses the same TCP connection for the
7131 * fore and backchannel.
7132 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007133static
7134int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7135 struct rpc_xprt *xprt,
7136 struct nfs_client *clp,
7137 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007138{
7139 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007140 struct nfs41_bind_conn_to_session_args args = {
7141 .client = clp,
7142 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7143 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007144 struct nfs41_bind_conn_to_session_res res;
7145 struct rpc_message msg = {
7146 .rpc_proc =
7147 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007148 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007149 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007150 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007151 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007152 struct rpc_task_setup task_setup_data = {
7153 .rpc_client = clnt,
7154 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007155 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007156 .rpc_message = &msg,
7157 .flags = RPC_TASK_TIMEOUT,
7158 };
7159 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007160
7161 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007162
Trond Myklebust71a097c2015-02-18 09:27:18 -08007163 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7164 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7165 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007166
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007167 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7168 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7169 args.dir = NFS4_CDFC4_FORE;
7170
7171 task = rpc_run_task(&task_setup_data);
7172 if (!IS_ERR(task)) {
7173 status = task->tk_status;
7174 rpc_put_task(task);
7175 } else
7176 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007177 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007179 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7181 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7182 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007183 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007185 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007186 dprintk("NFS: %s: Unexpected direction from server\n",
7187 __func__);
7188 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007189 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007190 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007191 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007192 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7193 __func__);
7194 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007195 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007196 }
7197 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007198out:
7199 dprintk("<-- %s status= %d\n", __func__, status);
7200 return status;
7201}
7202
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007203struct rpc_bind_conn_calldata {
7204 struct nfs_client *clp;
7205 struct rpc_cred *cred;
7206};
7207
7208static int
7209nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7210 struct rpc_xprt *xprt,
7211 void *calldata)
7212{
7213 struct rpc_bind_conn_calldata *p = calldata;
7214
7215 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7216}
7217
7218int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7219{
7220 struct rpc_bind_conn_calldata data = {
7221 .clp = clp,
7222 .cred = cred,
7223 };
7224 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7225 nfs4_proc_bind_conn_to_session_callback, &data);
7226}
7227
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007228/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007229 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7230 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007231 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007232static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7233 .how = SP4_MACH_CRED,
7234 .enforce.u.words = {
7235 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7236 1 << (OP_EXCHANGE_ID - 32) |
7237 1 << (OP_CREATE_SESSION - 32) |
7238 1 << (OP_DESTROY_SESSION - 32) |
7239 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007240 },
7241 .allow.u.words = {
7242 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007243 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007244 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007245 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007246 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007247 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007248 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007249 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007250 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007251 1 << (OP_FREE_STATEID - 32) |
7252 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007253 }
7254};
7255
7256/*
7257 * Select the state protection mode for client `clp' given the server results
7258 * from exchange_id in `sp'.
7259 *
7260 * Returns 0 on success, negative errno otherwise.
7261 */
7262static int nfs4_sp4_select_mode(struct nfs_client *clp,
7263 struct nfs41_state_protection *sp)
7264{
7265 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7266 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7267 1 << (OP_EXCHANGE_ID - 32) |
7268 1 << (OP_CREATE_SESSION - 32) |
7269 1 << (OP_DESTROY_SESSION - 32) |
7270 1 << (OP_DESTROY_CLIENTID - 32)
7271 };
7272 unsigned int i;
7273
7274 if (sp->how == SP4_MACH_CRED) {
7275 /* Print state protect result */
7276 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7277 for (i = 0; i <= LAST_NFS4_OP; i++) {
7278 if (test_bit(i, sp->enforce.u.longs))
7279 dfprintk(MOUNT, " enforce op %d\n", i);
7280 if (test_bit(i, sp->allow.u.longs))
7281 dfprintk(MOUNT, " allow op %d\n", i);
7282 }
7283
7284 /* make sure nothing is on enforce list that isn't supported */
7285 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7286 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7287 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7288 return -EINVAL;
7289 }
7290 }
7291
7292 /*
7293 * Minimal mode - state operations are allowed to use machine
7294 * credential. Note this already happens by default, so the
7295 * client doesn't have to do anything more than the negotiation.
7296 *
7297 * NOTE: we don't care if EXCHANGE_ID is in the list -
7298 * we're already using the machine cred for exchange_id
7299 * and will never use a different cred.
7300 */
7301 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7302 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7303 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7304 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7305 dfprintk(MOUNT, "sp4_mach_cred:\n");
7306 dfprintk(MOUNT, " minimal mode enabled\n");
7307 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7308 } else {
7309 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7310 return -EINVAL;
7311 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007312
7313 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007314 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7315 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007316 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7317 dfprintk(MOUNT, " cleanup mode enabled\n");
7318 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7319 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007320
Andrew Elble99ade3c2015-12-02 09:39:51 -05007321 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7322 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7323 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7324 &clp->cl_sp4_flags);
7325 }
7326
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007327 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7328 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7329 dfprintk(MOUNT, " secinfo mode enabled\n");
7330 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7331 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007332
7333 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7334 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7335 dfprintk(MOUNT, " stateid mode enabled\n");
7336 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7337 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007338
7339 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7340 dfprintk(MOUNT, " write mode enabled\n");
7341 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7342 }
7343
7344 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7345 dfprintk(MOUNT, " commit mode enabled\n");
7346 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7347 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007348 }
7349
7350 return 0;
7351}
7352
Andy Adamson8d89bd72016-09-09 09:22:18 -04007353struct nfs41_exchange_id_data {
7354 struct nfs41_exchange_id_res res;
7355 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007356 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007357 int rpc_status;
7358};
7359
7360static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007361{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007362 struct nfs41_exchange_id_data *cdata =
7363 (struct nfs41_exchange_id_data *)data;
7364 struct nfs_client *clp = cdata->args.client;
7365 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007366
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007367 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007368
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007369 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007370 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007371
7372 if (cdata->xprt && status == 0) {
7373 status = nfs4_detect_session_trunking(clp, &cdata->res,
7374 cdata->xprt);
7375 goto out;
7376 }
7377
Andy Adamson8d89bd72016-09-09 09:22:18 -04007378 if (status == 0)
7379 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007380
Chuck Lever177313f2012-05-21 22:44:58 -04007381 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007382 clp->cl_clientid = cdata->res.clientid;
7383 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007384 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007386 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007387 &clp->cl_session->session_state);
7388 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007389 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007390
Chuck Leveracdeb692012-05-21 22:46:16 -04007391 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007392 clp->cl_serverowner = cdata->res.server_owner;
7393 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007394
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007395 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007396 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397 clp->cl_implid = cdata->res.impl_id;
7398 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007399
Chuck Lever177313f2012-05-21 22:44:58 -04007400 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007401 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007403 dprintk("%s: server_scope mismatch detected\n",
7404 __func__);
7405 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007406 kfree(clp->cl_serverscope);
7407 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007408 }
7409
Chuck Lever177313f2012-05-21 22:44:58 -04007410 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007411 clp->cl_serverscope = cdata->res.server_scope;
7412 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007413 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007414 /* Save the EXCHANGE_ID verifier session trunk tests */
7415 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7416 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007418out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007419 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007420 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007421}
7422
7423static void nfs4_exchange_id_release(void *data)
7424{
7425 struct nfs41_exchange_id_data *cdata =
7426 (struct nfs41_exchange_id_data *)data;
7427
Andy Adamsonad0849a2016-09-09 09:22:28 -04007428 if (cdata->xprt) {
7429 xprt_put(cdata->xprt);
7430 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7431 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007432 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007433 kfree(cdata->res.impl_id);
7434 kfree(cdata->res.server_scope);
7435 kfree(cdata->res.server_owner);
7436 kfree(cdata);
7437}
7438
7439static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7440 .rpc_call_done = nfs4_exchange_id_done,
7441 .rpc_release = nfs4_exchange_id_release,
7442};
7443
Benny Halevy99fe60d2009-04-01 09:22:29 -04007444/*
7445 * _nfs4_proc_exchange_id()
7446 *
7447 * Wrapper for EXCHANGE_ID operation.
7448 */
7449static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007450 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007451{
7452 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007453 struct rpc_message msg = {
7454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455 .rpc_cred = cred,
7456 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 struct rpc_task_setup task_setup_data = {
7458 .rpc_client = clp->cl_rpcclient,
7459 .callback_ops = &nfs4_exchange_id_call_ops,
7460 .rpc_message = &msg,
7461 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7462 };
7463 struct nfs41_exchange_id_data *calldata;
7464 struct rpc_task *task;
7465 int status = -EIO;
7466
7467 if (!atomic_inc_not_zero(&clp->cl_count))
7468 goto out;
7469
7470 status = -ENOMEM;
7471 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7472 if (!calldata)
7473 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474
Andy Adamsonad0849a2016-09-09 09:22:28 -04007475 if (!xprt)
7476 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007477
7478 status = nfs4_init_uniform_client_string(clp);
7479 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007481
7482 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7483 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7484 clp->cl_owner_id);
7485
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7487 GFP_NOFS);
7488 status = -ENOMEM;
7489 if (unlikely(calldata->res.server_owner == NULL))
7490 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007491
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007493 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007495 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007496
Andy Adamson8d89bd72016-09-09 09:22:18 -04007497 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7498 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499 goto out_server_scope;
7500
7501 switch (sp4_how) {
7502 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007504 break;
7505
7506 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007507 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007508 break;
7509
7510 default:
7511 /* unsupported! */
7512 WARN_ON_ONCE(1);
7513 status = -EINVAL;
7514 goto out_impl_id;
7515 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007516 if (xprt) {
7517 calldata->xprt = xprt;
7518 task_setup_data.rpc_xprt = xprt;
7519 task_setup_data.flags =
7520 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7521 calldata->args.verifier = &clp->cl_confirm;
7522 } else {
7523 calldata->args.verifier = &verifier;
7524 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 calldata->args.client = clp;
7526#ifdef CONFIG_NFS_V4_1_MIGRATION
7527 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7528 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7529 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7530#else
7531 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7532 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7533#endif
7534 msg.rpc_argp = &calldata->args;
7535 msg.rpc_resp = &calldata->res;
7536 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007537
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007539 if (IS_ERR(task))
7540 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007541
Andy Adamsonad0849a2016-09-09 09:22:28 -04007542 if (!xprt) {
7543 status = rpc_wait_for_completion_task(task);
7544 if (!status)
7545 status = calldata->rpc_status;
7546 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007547 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007548
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007550out:
Chuck Lever177313f2012-05-21 22:44:58 -04007551 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007552 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007553 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007554 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007555 clp->cl_implid->date.seconds,
7556 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007557 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007558 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007559
7560out_impl_id:
7561 kfree(calldata->res.impl_id);
7562out_server_scope:
7563 kfree(calldata->res.server_scope);
7564out_server_owner:
7565 kfree(calldata->res.server_owner);
7566out_calldata:
7567 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007568 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570}
7571
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007572/*
7573 * nfs4_proc_exchange_id()
7574 *
7575 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7576 *
7577 * Since the clientid has expired, all compounds using sessions
7578 * associated with the stale clientid will be returning
7579 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7580 * be in some phase of session reset.
7581 *
7582 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7583 */
7584int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7585{
7586 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7587 int status;
7588
7589 /* try SP4_MACH_CRED if krb5i/p */
7590 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7591 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007592 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007593 if (!status)
7594 return 0;
7595 }
7596
7597 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007598 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007599}
7600
Andy Adamson04fa2c62016-09-09 09:22:29 -04007601/**
7602 * nfs4_test_session_trunk
7603 *
7604 * This is an add_xprt_test() test function called from
7605 * rpc_clnt_setup_test_and_add_xprt.
7606 *
7607 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7608 * and is dereferrenced in nfs4_exchange_id_release
7609 *
7610 * Upon success, add the new transport to the rpc_clnt
7611 *
7612 * @clnt: struct rpc_clnt to get new transport
7613 * @xprt: the rpc_xprt to test
7614 * @data: call data for _nfs4_proc_exchange_id.
7615 */
7616int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7617 void *data)
7618{
7619 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7620 u32 sp4_how;
7621
7622 dprintk("--> %s try %s\n", __func__,
7623 xprt->address_strings[RPC_DISPLAY_ADDR]);
7624
7625 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7626
7627 /* Test connection for session trunking. Async exchange_id call */
7628 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7629}
7630EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7631
Trond Myklebust66245532012-05-25 17:18:09 -04007632static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7633 struct rpc_cred *cred)
7634{
7635 struct rpc_message msg = {
7636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7637 .rpc_argp = clp,
7638 .rpc_cred = cred,
7639 };
7640 int status;
7641
7642 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007643 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007644 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007645 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007646 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7647 return status;
7648}
7649
7650static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7651 struct rpc_cred *cred)
7652{
7653 unsigned int loop;
7654 int ret;
7655
7656 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7657 ret = _nfs4_proc_destroy_clientid(clp, cred);
7658 switch (ret) {
7659 case -NFS4ERR_DELAY:
7660 case -NFS4ERR_CLIENTID_BUSY:
7661 ssleep(1);
7662 break;
7663 default:
7664 return ret;
7665 }
7666 }
7667 return 0;
7668}
7669
7670int nfs4_destroy_clientid(struct nfs_client *clp)
7671{
7672 struct rpc_cred *cred;
7673 int ret = 0;
7674
7675 if (clp->cl_mvops->minor_version < 1)
7676 goto out;
7677 if (clp->cl_exchange_flags == 0)
7678 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007679 if (clp->cl_preserve_clid)
7680 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007681 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007682 ret = nfs4_proc_destroy_clientid(clp, cred);
7683 if (cred)
7684 put_rpccred(cred);
7685 switch (ret) {
7686 case 0:
7687 case -NFS4ERR_STALE_CLIENTID:
7688 clp->cl_exchange_flags = 0;
7689 }
7690out:
7691 return ret;
7692}
7693
Andy Adamson2050f0c2009-04-01 09:22:30 -04007694struct nfs4_get_lease_time_data {
7695 struct nfs4_get_lease_time_args *args;
7696 struct nfs4_get_lease_time_res *res;
7697 struct nfs_client *clp;
7698};
7699
7700static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7701 void *calldata)
7702{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007703 struct nfs4_get_lease_time_data *data =
7704 (struct nfs4_get_lease_time_data *)calldata;
7705
7706 dprintk("--> %s\n", __func__);
7707 /* just setup sequence, do not trigger session recovery
7708 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007709 nfs41_setup_sequence(data->clp->cl_session,
7710 &data->args->la_seq_args,
7711 &data->res->lr_seq_res,
7712 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007713 dprintk("<-- %s\n", __func__);
7714}
7715
7716/*
7717 * Called from nfs4_state_manager thread for session setup, so don't recover
7718 * from sequence operation or clientid errors.
7719 */
7720static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7721{
7722 struct nfs4_get_lease_time_data *data =
7723 (struct nfs4_get_lease_time_data *)calldata;
7724
7725 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007726 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7727 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007728 switch (task->tk_status) {
7729 case -NFS4ERR_DELAY:
7730 case -NFS4ERR_GRACE:
7731 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7732 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7733 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007734 /* fall through */
7735 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007736 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007737 return;
7738 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007739 dprintk("<-- %s\n", __func__);
7740}
7741
Trond Myklebust17280172012-03-11 13:11:00 -04007742static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007743 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7744 .rpc_call_done = nfs4_get_lease_time_done,
7745};
7746
7747int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7748{
7749 struct rpc_task *task;
7750 struct nfs4_get_lease_time_args args;
7751 struct nfs4_get_lease_time_res res = {
7752 .lr_fsinfo = fsinfo,
7753 };
7754 struct nfs4_get_lease_time_data data = {
7755 .args = &args,
7756 .res = &res,
7757 .clp = clp,
7758 };
7759 struct rpc_message msg = {
7760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7761 .rpc_argp = &args,
7762 .rpc_resp = &res,
7763 };
7764 struct rpc_task_setup task_setup = {
7765 .rpc_client = clp->cl_rpcclient,
7766 .rpc_message = &msg,
7767 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007768 .callback_data = &data,
7769 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007770 };
7771 int status;
7772
Chuck Levera9c92d62013-08-09 12:48:18 -04007773 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007774 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007775 dprintk("--> %s\n", __func__);
7776 task = rpc_run_task(&task_setup);
7777
7778 if (IS_ERR(task))
7779 status = PTR_ERR(task);
7780 else {
7781 status = task->tk_status;
7782 rpc_put_task(task);
7783 }
7784 dprintk("<-- %s return %d\n", __func__, status);
7785
7786 return status;
7787}
7788
Andy Adamsonfc931582009-04-01 09:22:31 -04007789/*
7790 * Initialize the values to be used by the client in CREATE_SESSION
7791 * If nfs4_init_session set the fore channel request and response sizes,
7792 * use them.
7793 *
7794 * Set the back channel max_resp_sz_cached to zero to force the client to
7795 * always set csa_cachethis to FALSE because the current implementation
7796 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7797 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007798static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7799 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007800{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007801 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007802 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007803
Andy Adamson18aad3d2013-06-26 12:21:49 -04007804 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7805 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7806
Andy Adamsonfc931582009-04-01 09:22:31 -04007807 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007808 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7809 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007810 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007811 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007812
7813 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007814 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007815 __func__,
7816 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007817 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007818
7819 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007820 args->bc_attrs.max_rqst_sz = max_bc_payload;
7821 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007822 args->bc_attrs.max_resp_sz_cached = 0;
7823 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007824 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007825
7826 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7827 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7828 __func__,
7829 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7830 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7831 args->bc_attrs.max_reqs);
7832}
7833
Trond Myklebust79969dd2015-02-18 11:30:18 -08007834static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7835 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007836{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007837 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007838 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007839
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007840 if (rcvd->max_resp_sz > sent->max_resp_sz)
7841 return -EINVAL;
7842 /*
7843 * Our requested max_ops is the minimum we need; we're not
7844 * prepared to break up compounds into smaller pieces than that.
7845 * So, no point even trying to continue if the server won't
7846 * cooperate:
7847 */
7848 if (rcvd->max_ops < sent->max_ops)
7849 return -EINVAL;
7850 if (rcvd->max_reqs == 0)
7851 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007852 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7853 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007855}
7856
Trond Myklebust79969dd2015-02-18 11:30:18 -08007857static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7858 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007859{
7860 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007861 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007862
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007863 if (!(res->flags & SESSION4_BACK_CHAN))
7864 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007865 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7866 return -EINVAL;
7867 if (rcvd->max_resp_sz < sent->max_resp_sz)
7868 return -EINVAL;
7869 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7870 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007871 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007872 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007873 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007874 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007875out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007876 return 0;
7877}
Andy Adamson8d353012009-04-01 09:22:32 -04007878
Andy Adamson8d353012009-04-01 09:22:32 -04007879static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007880 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007881{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007882 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007883
Trond Myklebust79969dd2015-02-18 11:30:18 -08007884 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 if (ret)
7886 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007887 return nfs4_verify_back_channel_attrs(args, res);
7888}
7889
7890static void nfs4_update_session(struct nfs4_session *session,
7891 struct nfs41_create_session_res *res)
7892{
7893 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007894 /* Mark client id and session as being confirmed */
7895 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7896 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007897 session->flags = res->flags;
7898 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007899 if (res->flags & SESSION4_BACK_CHAN)
7900 memcpy(&session->bc_attrs, &res->bc_attrs,
7901 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007902}
7903
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007904static int _nfs4_proc_create_session(struct nfs_client *clp,
7905 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007906{
7907 struct nfs4_session *session = clp->cl_session;
7908 struct nfs41_create_session_args args = {
7909 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007910 .clientid = clp->cl_clientid,
7911 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007912 .cb_program = NFS4_CALLBACK,
7913 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914 struct nfs41_create_session_res res;
7915
Andy Adamsonfc931582009-04-01 09:22:31 -04007916 struct rpc_message msg = {
7917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7918 .rpc_argp = &args,
7919 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007920 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 };
7922 int status;
7923
Chuck Lever6b26cc82016-05-02 14:40:40 -04007924 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007925 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007926
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007927 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007928 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007929
Trond Myklebustb519d402016-09-11 14:50:01 -04007930 switch (status) {
7931 case -NFS4ERR_STALE_CLIENTID:
7932 case -NFS4ERR_DELAY:
7933 case -ETIMEDOUT:
7934 case -EACCES:
7935 case -EAGAIN:
7936 goto out;
7937 };
7938
7939 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007940 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007941 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007942 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007944 if (status)
7945 goto out;
7946 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007947 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007948out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007949 return status;
7950}
7951
7952/*
7953 * Issues a CREATE_SESSION operation to the server.
7954 * It is the responsibility of the caller to verify the session is
7955 * expired before calling this routine.
7956 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007957int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007958{
7959 int status;
7960 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007961 struct nfs4_session *session = clp->cl_session;
7962
7963 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7964
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007965 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007966 if (status)
7967 goto out;
7968
Andy Adamsonaacd5532011-11-09 13:58:21 -05007969 /* Init or reset the session slot tables */
7970 status = nfs4_setup_session_slot_tables(session);
7971 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007972 if (status)
7973 goto out;
7974
7975 ptr = (unsigned *)&session->sess_id.data[0];
7976 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7977 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007978out:
7979 dprintk("<-- %s\n", __func__);
7980 return status;
7981}
7982
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007983/*
7984 * Issue the over-the-wire RPC DESTROY_SESSION.
7985 * The caller must serialize access to this routine.
7986 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007987int nfs4_proc_destroy_session(struct nfs4_session *session,
7988 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007989{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007990 struct rpc_message msg = {
7991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7992 .rpc_argp = session,
7993 .rpc_cred = cred,
7994 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007995 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007996
7997 dprintk("--> nfs4_proc_destroy_session\n");
7998
7999 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008000 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8001 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008002
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008003 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008004 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008005
8006 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008007 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008 "Session has been destroyed regardless...\n", status);
8009
8010 dprintk("<-- nfs4_proc_destroy_session\n");
8011 return status;
8012}
8013
Trond Myklebust7b38c362012-05-23 13:23:31 -04008014/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008015 * Renew the cl_session lease.
8016 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008017struct nfs4_sequence_data {
8018 struct nfs_client *clp;
8019 struct nfs4_sequence_args args;
8020 struct nfs4_sequence_res res;
8021};
8022
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008023static void nfs41_sequence_release(void *data)
8024{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008025 struct nfs4_sequence_data *calldata = data;
8026 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008027
Alexandros Batsakis71358402010-02-05 03:45:05 -08008028 if (atomic_read(&clp->cl_count) > 1)
8029 nfs4_schedule_state_renewal(clp);
8030 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008031 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008032}
8033
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008034static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8035{
8036 switch(task->tk_status) {
8037 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008038 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8039 return -EAGAIN;
8040 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008041 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008042 }
8043 return 0;
8044}
8045
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008046static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008047{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008048 struct nfs4_sequence_data *calldata = data;
8049 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008050
Trond Myklebust14516c32010-07-31 14:29:06 -04008051 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8052 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008054 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055 if (task->tk_status < 0) {
8056 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008057 if (atomic_read(&clp->cl_count) == 1)
8058 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008060 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8061 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062 return;
8063 }
8064 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008065 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008066out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008067 dprintk("<-- %s\n", __func__);
8068}
8069
8070static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074 struct nfs4_sequence_args *args;
8075 struct nfs4_sequence_res *res;
8076
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077 args = task->tk_msg.rpc_argp;
8078 res = task->tk_msg.rpc_resp;
8079
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008080 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081}
8082
8083static const struct rpc_call_ops nfs41_sequence_ops = {
8084 .rpc_call_done = nfs41_sequence_call_done,
8085 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008086 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087};
8088
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008089static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8090 struct rpc_cred *cred,
8091 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008092{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008093 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094 struct rpc_message msg = {
8095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8096 .rpc_cred = cred,
8097 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008098 struct rpc_task_setup task_setup_data = {
8099 .rpc_client = clp->cl_rpcclient,
8100 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008101 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008102 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008103 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008104
Alexandros Batsakis71358402010-02-05 03:45:05 -08008105 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008107 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008108 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008109 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008110 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008111 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008112 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008113 if (is_privileged)
8114 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008115 msg.rpc_argp = &calldata->args;
8116 msg.rpc_resp = &calldata->res;
8117 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008118 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008120 return rpc_run_task(&task_setup_data);
8121}
8122
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008123static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008124{
8125 struct rpc_task *task;
8126 int ret = 0;
8127
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008128 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008129 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008130 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008131 if (IS_ERR(task))
8132 ret = PTR_ERR(task);
8133 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008134 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008135 dprintk("<-- %s status=%d\n", __func__, ret);
8136 return ret;
8137}
8138
8139static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8140{
8141 struct rpc_task *task;
8142 int ret;
8143
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008144 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008145 if (IS_ERR(task)) {
8146 ret = PTR_ERR(task);
8147 goto out;
8148 }
8149 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008150 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008151 ret = task->tk_status;
8152 rpc_put_task(task);
8153out:
8154 dprintk("<-- %s status=%d\n", __func__, ret);
8155 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008156}
8157
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008158struct nfs4_reclaim_complete_data {
8159 struct nfs_client *clp;
8160 struct nfs41_reclaim_complete_args arg;
8161 struct nfs41_reclaim_complete_res res;
8162};
8163
8164static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8165{
8166 struct nfs4_reclaim_complete_data *calldata = data;
8167
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008168 nfs41_setup_sequence(calldata->clp->cl_session,
8169 &calldata->arg.seq_args,
8170 &calldata->res.seq_res,
8171 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008172}
8173
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008174static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8175{
8176 switch(task->tk_status) {
8177 case 0:
8178 case -NFS4ERR_COMPLETE_ALREADY:
8179 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8180 break;
8181 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008182 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008183 /* fall through */
8184 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008185 return -EAGAIN;
8186 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008187 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008188 }
8189 return 0;
8190}
8191
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008192static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8193{
8194 struct nfs4_reclaim_complete_data *calldata = data;
8195 struct nfs_client *clp = calldata->clp;
8196 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8197
8198 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008199 if (!nfs41_sequence_done(task, res))
8200 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008201
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008202 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008203 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8204 rpc_restart_call_prepare(task);
8205 return;
8206 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008207 dprintk("<-- %s\n", __func__);
8208}
8209
8210static void nfs4_free_reclaim_complete_data(void *data)
8211{
8212 struct nfs4_reclaim_complete_data *calldata = data;
8213
8214 kfree(calldata);
8215}
8216
8217static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8218 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8219 .rpc_call_done = nfs4_reclaim_complete_done,
8220 .rpc_release = nfs4_free_reclaim_complete_data,
8221};
8222
8223/*
8224 * Issue a global reclaim complete.
8225 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008226static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8227 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008228{
8229 struct nfs4_reclaim_complete_data *calldata;
8230 struct rpc_task *task;
8231 struct rpc_message msg = {
8232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008233 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234 };
8235 struct rpc_task_setup task_setup_data = {
8236 .rpc_client = clp->cl_rpcclient,
8237 .rpc_message = &msg,
8238 .callback_ops = &nfs4_reclaim_complete_call_ops,
8239 .flags = RPC_TASK_ASYNC,
8240 };
8241 int status = -ENOMEM;
8242
8243 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008244 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008245 if (calldata == NULL)
8246 goto out;
8247 calldata->clp = clp;
8248 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008249
Chuck Levera9c92d62013-08-09 12:48:18 -04008250 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008251 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252 msg.rpc_argp = &calldata->arg;
8253 msg.rpc_resp = &calldata->res;
8254 task_setup_data.callback_data = calldata;
8255 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008256 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008257 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008258 goto out;
8259 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008260 status = nfs4_wait_for_completion_rpc_task(task);
8261 if (status == 0)
8262 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008263 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008264 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008265out:
8266 dprintk("<-- %s status=%d\n", __func__, status);
8267 return status;
8268}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008269
8270static void
8271nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8272{
8273 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008274 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008275 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008276
8277 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008278 nfs41_setup_sequence(session, &lgp->args.seq_args,
8279 &lgp->res.seq_res, task);
8280 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008281}
8282
8283static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8284{
8285 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008286
8287 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008288 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008289 dprintk("<-- %s\n", __func__);
8290}
8291
8292static int
8293nfs4_layoutget_handle_exception(struct rpc_task *task,
8294 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8295{
Trond Myklebustee314c22012-10-01 17:25:48 -07008296 struct inode *inode = lgp->args.inode;
8297 struct nfs_server *server = NFS_SERVER(inode);
8298 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008299 int nfs4err = task->tk_status;
8300 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008301 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008302
Boaz Harroshed7e5422014-01-22 20:34:54 +02008303 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008304
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008305 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008306 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008307 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008308
8309 /*
8310 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8311 * on the file. set tk_status to -ENODATA to tell upper layer to
8312 * retry go inband.
8313 */
8314 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008315 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008316 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008317 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008318 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8319 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8320 */
8321 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008322 status = -EOVERFLOW;
8323 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008324 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008325 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008326 * (or clients) writing to the same RAID stripe except when
8327 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008328 *
8329 * Treat it like we would RECALLCONFLICT -- we retry for a little
8330 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008331 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008333 if (lgp->args.minlength == 0) {
8334 status = -EOVERFLOW;
8335 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008336 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008337 status = -EBUSY;
8338 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008339 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008341 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008342 case -NFS4ERR_DELEG_REVOKED:
8343 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008344 case -NFS4ERR_EXPIRED:
8345 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008346 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008347 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008348 lo = NFS_I(inode)->layout;
8349 /* If the open stateid was bad, then recover it. */
8350 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8351 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008352 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008353 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008354 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008355 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008356 break;
8357 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008358
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008359 /*
8360 * Mark the bad layout state as invalid, then retry
8361 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008362 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008363 spin_unlock(&inode->i_lock);
8364 pnfs_free_lseg_list(&head);
8365 status = -EAGAIN;
8366 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008367 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008368
Trond Myklebust0a702352017-01-23 22:44:12 -05008369 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008370 err = nfs4_handle_exception(server, nfs4err, exception);
8371 if (!status) {
8372 if (exception->retry)
8373 status = -EAGAIN;
8374 else
8375 status = err;
8376 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008377out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008378 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008379 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008380}
8381
Idan Kedar85541162012-08-02 11:47:10 +03008382static size_t max_response_pages(struct nfs_server *server)
8383{
8384 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8385 return nfs_page_array_len(0, max_resp_sz);
8386}
8387
8388static void nfs4_free_pages(struct page **pages, size_t size)
8389{
8390 int i;
8391
8392 if (!pages)
8393 return;
8394
8395 for (i = 0; i < size; i++) {
8396 if (!pages[i])
8397 break;
8398 __free_page(pages[i]);
8399 }
8400 kfree(pages);
8401}
8402
8403static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8404{
8405 struct page **pages;
8406 int i;
8407
8408 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8409 if (!pages) {
8410 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8411 return NULL;
8412 }
8413
8414 for (i = 0; i < size; i++) {
8415 pages[i] = alloc_page(gfp_flags);
8416 if (!pages[i]) {
8417 dprintk("%s: failed to allocate page\n", __func__);
8418 nfs4_free_pages(pages, size);
8419 return NULL;
8420 }
8421 }
8422
8423 return pages;
8424}
8425
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008426static void nfs4_layoutget_release(void *calldata)
8427{
8428 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008429 struct inode *inode = lgp->args.inode;
8430 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008431 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008432
8433 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008434 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008435 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008436 put_nfs_open_context(lgp->args.ctx);
8437 kfree(calldata);
8438 dprintk("<-- %s\n", __func__);
8439}
8440
8441static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8442 .rpc_call_prepare = nfs4_layoutget_prepare,
8443 .rpc_call_done = nfs4_layoutget_done,
8444 .rpc_release = nfs4_layoutget_release,
8445};
8446
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008447struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008448nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008450 struct inode *inode = lgp->args.inode;
8451 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008452 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453 struct rpc_task *task;
8454 struct rpc_message msg = {
8455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8456 .rpc_argp = &lgp->args,
8457 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008458 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459 };
8460 struct rpc_task_setup task_setup_data = {
8461 .rpc_client = server->client,
8462 .rpc_message = &msg,
8463 .callback_ops = &nfs4_layoutget_call_ops,
8464 .callback_data = lgp,
8465 .flags = RPC_TASK_ASYNC,
8466 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008467 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008468 struct nfs4_exception exception = {
8469 .inode = inode,
8470 .timeout = *timeout,
8471 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 int status = 0;
8473
8474 dprintk("--> %s\n", __func__);
8475
Peng Tao4bd5a982014-11-17 11:05:17 +08008476 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8477 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8478
Idan Kedar85541162012-08-02 11:47:10 +03008479 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8480 if (!lgp->args.layout.pages) {
8481 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008482 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008483 }
8484 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8485
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008486 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008487 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008488 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008489
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 task = rpc_run_task(&task_setup_data);
8491 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008492 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008493 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008494 if (status == 0) {
8495 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8496 *timeout = exception.timeout;
8497 }
8498
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008499 trace_nfs4_layoutget(lgp->args.ctx,
8500 &lgp->args.range,
8501 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008502 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008503 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008504
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008505 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8506 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008507 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008508 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509 rpc_put_task(task);
8510 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008511 if (status)
8512 return ERR_PTR(status);
8513 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008514}
8515
Benny Halevycbe82602011-05-22 19:52:37 +03008516static void
8517nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8518{
8519 struct nfs4_layoutreturn *lrp = calldata;
8520
8521 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008522 nfs41_setup_sequence(lrp->clp->cl_session,
8523 &lrp->args.seq_args,
8524 &lrp->res.seq_res,
8525 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008526}
8527
8528static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8529{
8530 struct nfs4_layoutreturn *lrp = calldata;
8531 struct nfs_server *server;
8532
8533 dprintk("--> %s\n", __func__);
8534
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008535 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008536 return;
8537
8538 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008539 switch (task->tk_status) {
8540 default:
8541 task->tk_status = 0;
8542 case 0:
8543 break;
8544 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008545 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008546 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008547 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008548 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008549 return;
8550 }
Benny Halevycbe82602011-05-22 19:52:37 +03008551 dprintk("<-- %s\n", __func__);
8552}
8553
8554static void nfs4_layoutreturn_release(void *calldata)
8555{
8556 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008557 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008558 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008559
8560 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008561 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008562 if (lrp->res.lrs_present) {
8563 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8564 &lrp->args.range,
8565 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008566 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008567 } else
8568 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008569 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008570 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008571 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008572 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008573 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008574 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008575 kfree(calldata);
8576 dprintk("<-- %s\n", __func__);
8577}
8578
8579static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8580 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8581 .rpc_call_done = nfs4_layoutreturn_done,
8582 .rpc_release = nfs4_layoutreturn_release,
8583};
8584
Peng Tao6c166052014-11-17 09:30:40 +08008585int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008586{
8587 struct rpc_task *task;
8588 struct rpc_message msg = {
8589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8590 .rpc_argp = &lrp->args,
8591 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008592 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008593 };
8594 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008595 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008596 .rpc_message = &msg,
8597 .callback_ops = &nfs4_layoutreturn_call_ops,
8598 .callback_data = lrp,
8599 };
Peng Tao6c166052014-11-17 09:30:40 +08008600 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008601
Andrew Elble99ade3c2015-12-02 09:39:51 -05008602 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8603 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8604 &task_setup_data.rpc_client, &msg);
8605
Benny Halevycbe82602011-05-22 19:52:37 +03008606 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008607 if (!sync) {
8608 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8609 if (!lrp->inode) {
8610 nfs4_layoutreturn_release(lrp);
8611 return -EAGAIN;
8612 }
8613 task_setup_data.flags |= RPC_TASK_ASYNC;
8614 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008615 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008616 task = rpc_run_task(&task_setup_data);
8617 if (IS_ERR(task))
8618 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008619 if (sync)
8620 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008621 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008622 dprintk("<-- %s status=%d\n", __func__, status);
8623 rpc_put_task(task);
8624 return status;
8625}
8626
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008628_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8629 struct pnfs_device *pdev,
8630 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008631{
8632 struct nfs4_getdeviceinfo_args args = {
8633 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008634 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8635 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008636 };
8637 struct nfs4_getdeviceinfo_res res = {
8638 .pdev = pdev,
8639 };
8640 struct rpc_message msg = {
8641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8642 .rpc_argp = &args,
8643 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008644 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008645 };
8646 int status;
8647
8648 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008649 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008650 if (res.notification & ~args.notify_types)
8651 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008652 if (res.notification != args.notify_types)
8653 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008654
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655 dprintk("<-- %s status=%d\n", __func__, status);
8656
8657 return status;
8658}
8659
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008660int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8661 struct pnfs_device *pdev,
8662 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663{
8664 struct nfs4_exception exception = { };
8665 int err;
8666
8667 do {
8668 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008669 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670 &exception);
8671 } while (exception.retry);
8672 return err;
8673}
8674EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8675
Andy Adamson863a3c62011-03-23 13:27:54 +00008676static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8677{
8678 struct nfs4_layoutcommit_data *data = calldata;
8679 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008680 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008681
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008682 nfs41_setup_sequence(session,
8683 &data->args.seq_args,
8684 &data->res.seq_res,
8685 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008686}
8687
8688static void
8689nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8690{
8691 struct nfs4_layoutcommit_data *data = calldata;
8692 struct nfs_server *server = NFS_SERVER(data->args.inode);
8693
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008694 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008695 return;
8696
8697 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008698 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8699 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8700 case -NFS4ERR_BADLAYOUT: /* no layout */
8701 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008702 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008703 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008704 break;
8705 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008706 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008707 rpc_restart_call_prepare(task);
8708 return;
8709 }
8710 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008711}
8712
8713static void nfs4_layoutcommit_release(void *calldata)
8714{
8715 struct nfs4_layoutcommit_data *data = calldata;
8716
Andy Adamsondb29c082011-07-30 20:52:38 -04008717 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008718 nfs_post_op_update_inode_force_wcc(data->args.inode,
8719 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008720 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008721 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008722 kfree(data);
8723}
8724
8725static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8726 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8727 .rpc_call_done = nfs4_layoutcommit_done,
8728 .rpc_release = nfs4_layoutcommit_release,
8729};
8730
8731int
Andy Adamsonef311532011-03-12 02:58:10 -05008732nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008733{
8734 struct rpc_message msg = {
8735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8736 .rpc_argp = &data->args,
8737 .rpc_resp = &data->res,
8738 .rpc_cred = data->cred,
8739 };
8740 struct rpc_task_setup task_setup_data = {
8741 .task = &data->task,
8742 .rpc_client = NFS_CLIENT(data->args.inode),
8743 .rpc_message = &msg,
8744 .callback_ops = &nfs4_layoutcommit_ops,
8745 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008746 };
8747 struct rpc_task *task;
8748 int status = 0;
8749
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008750 dprintk("NFS: initiating layoutcommit call. sync %d "
8751 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008752 data->args.lastbytewritten,
8753 data->args.inode->i_ino);
8754
Trond Myklebust472e2592015-02-05 16:50:30 -05008755 if (!sync) {
8756 data->inode = nfs_igrab_and_active(data->args.inode);
8757 if (data->inode == NULL) {
8758 nfs4_layoutcommit_release(data);
8759 return -EAGAIN;
8760 }
8761 task_setup_data.flags = RPC_TASK_ASYNC;
8762 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008763 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008764 task = rpc_run_task(&task_setup_data);
8765 if (IS_ERR(task))
8766 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008767 if (sync)
8768 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008769 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008770 dprintk("%s: status %d\n", __func__, status);
8771 rpc_put_task(task);
8772 return status;
8773}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008774
Andy Adamson97431202013-08-08 10:57:56 -04008775/**
8776 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8777 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8778 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008779static int
8780_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008781 struct nfs_fsinfo *info,
8782 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008783{
8784 struct nfs41_secinfo_no_name_args args = {
8785 .style = SECINFO_STYLE_CURRENT_FH,
8786 };
8787 struct nfs4_secinfo_res res = {
8788 .flavors = flavors,
8789 };
8790 struct rpc_message msg = {
8791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8792 .rpc_argp = &args,
8793 .rpc_resp = &res,
8794 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008795 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008796 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008797 int status;
8798
8799 if (use_integrity) {
8800 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008801 cred = nfs4_get_clid_cred(server->nfs_client);
8802 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008803 }
8804
8805 dprintk("--> %s\n", __func__);
8806 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8807 &res.seq_res, 0);
8808 dprintk("<-- %s status=%d\n", __func__, status);
8809
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008810 if (cred)
8811 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008812
8813 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008814}
8815
8816static int
8817nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8818 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8819{
8820 struct nfs4_exception exception = { };
8821 int err;
8822 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008823 /* first try using integrity protection */
8824 err = -NFS4ERR_WRONGSEC;
8825
8826 /* try to use integrity protection with machine cred */
8827 if (_nfs4_is_integrity_protected(server->nfs_client))
8828 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8829 flavors, true);
8830
8831 /*
8832 * if unable to use integrity protection, or SECINFO with
8833 * integrity protection returns NFS4ERR_WRONGSEC (which is
8834 * disallowed by spec, but exists in deployed servers) use
8835 * the current filesystem's rpc_client and the user cred.
8836 */
8837 if (err == -NFS4ERR_WRONGSEC)
8838 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8839 flavors, false);
8840
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008841 switch (err) {
8842 case 0:
8843 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008844 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008845 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008846 default:
8847 err = nfs4_handle_exception(server, err, &exception);
8848 }
8849 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008850out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008851 return err;
8852}
8853
8854static int
8855nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8856 struct nfs_fsinfo *info)
8857{
8858 int err;
8859 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008860 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008861 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008862 struct nfs4_secinfo4 *secinfo;
8863 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864
8865 page = alloc_page(GFP_KERNEL);
8866 if (!page) {
8867 err = -ENOMEM;
8868 goto out;
8869 }
8870
8871 flavors = page_address(page);
8872 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8873
8874 /*
8875 * Fall back on "guess and check" method if
8876 * the server doesn't support SECINFO_NO_NAME
8877 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008878 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879 err = nfs4_find_root_sec(server, fhandle, info);
8880 goto out_freepage;
8881 }
8882 if (err)
8883 goto out_freepage;
8884
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008885 for (i = 0; i < flavors->num_flavors; i++) {
8886 secinfo = &flavors->flavors[i];
8887
8888 switch (secinfo->flavor) {
8889 case RPC_AUTH_NULL:
8890 case RPC_AUTH_UNIX:
8891 case RPC_AUTH_GSS:
8892 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8893 &secinfo->flavor_info);
8894 break;
8895 default:
8896 flavor = RPC_AUTH_MAXFLAVOR;
8897 break;
8898 }
8899
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008900 if (!nfs_auth_info_match(&server->auth_info, flavor))
8901 flavor = RPC_AUTH_MAXFLAVOR;
8902
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008903 if (flavor != RPC_AUTH_MAXFLAVOR) {
8904 err = nfs4_lookup_root_sec(server, fhandle,
8905 info, flavor);
8906 if (!err)
8907 break;
8908 }
8909 }
8910
8911 if (flavor == RPC_AUTH_MAXFLAVOR)
8912 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008913
8914out_freepage:
8915 put_page(page);
8916 if (err == -EACCES)
8917 return -EPERM;
8918out:
8919 return err;
8920}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008921
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008922static int _nfs41_test_stateid(struct nfs_server *server,
8923 nfs4_stateid *stateid,
8924 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008925{
8926 int status;
8927 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008928 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008929 };
8930 struct nfs41_test_stateid_res res;
8931 struct rpc_message msg = {
8932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8933 .rpc_argp = &args,
8934 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008935 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008936 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008937 struct rpc_clnt *rpc_client = server->client;
8938
8939 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8940 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008941
Chuck Lever38527b12012-07-11 16:30:23 -04008942 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008943 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008944 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008945 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008946 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008947 if (status != NFS_OK) {
8948 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008949 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008950 }
8951 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008952 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008953}
8954
Trond Myklebust43912bb2016-09-22 13:38:56 -04008955static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8956 int err, struct nfs4_exception *exception)
8957{
8958 exception->retry = 0;
8959 switch(err) {
8960 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008961 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008962 nfs4_handle_exception(server, err, exception);
8963 break;
8964 case -NFS4ERR_BADSESSION:
8965 case -NFS4ERR_BADSLOT:
8966 case -NFS4ERR_BAD_HIGH_SLOT:
8967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8968 case -NFS4ERR_DEADSESSION:
8969 nfs4_do_handle_exception(server, err, exception);
8970 }
8971}
8972
Chuck Lever38527b12012-07-11 16:30:23 -04008973/**
8974 * nfs41_test_stateid - perform a TEST_STATEID operation
8975 *
8976 * @server: server / transport on which to perform the operation
8977 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008978 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008979 *
8980 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8981 * Otherwise a negative NFS4ERR value is returned if the operation
8982 * failed or the state ID is not currently valid.
8983 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008984static int nfs41_test_stateid(struct nfs_server *server,
8985 nfs4_stateid *stateid,
8986 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008987{
8988 struct nfs4_exception exception = { };
8989 int err;
8990 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008991 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008992 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008993 } while (exception.retry);
8994 return err;
8995}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008996
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008997struct nfs_free_stateid_data {
8998 struct nfs_server *server;
8999 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009000 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009001};
9002
9003static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9004{
9005 struct nfs_free_stateid_data *data = calldata;
9006 nfs41_setup_sequence(nfs4_get_session(data->server),
9007 &data->args.seq_args,
9008 &data->res.seq_res,
9009 task);
9010}
9011
9012static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9013{
9014 struct nfs_free_stateid_data *data = calldata;
9015
9016 nfs41_sequence_done(task, &data->res.seq_res);
9017
9018 switch (task->tk_status) {
9019 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009020 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009021 rpc_restart_call_prepare(task);
9022 }
9023}
9024
9025static void nfs41_free_stateid_release(void *calldata)
9026{
9027 kfree(calldata);
9028}
9029
Trond Myklebust17f26b12013-08-21 15:48:42 -04009030static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009031 .rpc_call_prepare = nfs41_free_stateid_prepare,
9032 .rpc_call_done = nfs41_free_stateid_done,
9033 .rpc_release = nfs41_free_stateid_release,
9034};
9035
9036static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009037 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009038 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009039 bool privileged)
9040{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009041 struct rpc_message msg = {
9042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009043 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009044 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009045 struct rpc_task_setup task_setup = {
9046 .rpc_client = server->client,
9047 .rpc_message = &msg,
9048 .callback_ops = &nfs41_free_stateid_ops,
9049 .flags = RPC_TASK_ASYNC,
9050 };
9051 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009052
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009053 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9054 &task_setup.rpc_client, &msg);
9055
Chuck Lever38527b12012-07-11 16:30:23 -04009056 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009057 data = kmalloc(sizeof(*data), GFP_NOFS);
9058 if (!data)
9059 return ERR_PTR(-ENOMEM);
9060 data->server = server;
9061 nfs4_stateid_copy(&data->args.stateid, stateid);
9062
9063 task_setup.callback_data = data;
9064
9065 msg.rpc_argp = &data->args;
9066 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009068 if (privileged)
9069 nfs4_set_sequence_privileged(&data->args.seq_args);
9070
9071 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009072}
9073
Chuck Lever38527b12012-07-11 16:30:23 -04009074/**
9075 * nfs41_free_stateid - perform a FREE_STATEID operation
9076 *
9077 * @server: server / transport on which to perform the operation
9078 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009079 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009080 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009081 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009082 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009083 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009084static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009085 const nfs4_stateid *stateid,
9086 struct rpc_cred *cred,
9087 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009088{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009089 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009090
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009091 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009092 if (IS_ERR(task))
9093 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009094 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009095 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009096}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009097
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009098static void
9099nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009100{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009102
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009103 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009104 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009105}
9106
Trond Myklebust36281ca2012-03-04 18:13:56 -05009107static bool nfs41_match_stateid(const nfs4_stateid *s1,
9108 const nfs4_stateid *s2)
9109{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009110 if (s1->type != s2->type)
9111 return false;
9112
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009113 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009114 return false;
9115
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009116 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009117 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009118 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009119 return true;
9120
9121 return false;
9122}
9123
Andy Adamson557134a2009-04-01 09:21:53 -04009124#endif /* CONFIG_NFS_V4_1 */
9125
Trond Myklebust36281ca2012-03-04 18:13:56 -05009126static bool nfs4_match_stateid(const nfs4_stateid *s1,
9127 const nfs4_stateid *s2)
9128{
Trond Myklebustf597c532012-03-04 18:13:56 -05009129 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009130}
9131
9132
Trond Myklebust17280172012-03-11 13:11:00 -04009133static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009134 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009135 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 .recover_open = nfs4_open_reclaim,
9137 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009138 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009139 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140};
9141
Andy Adamson591d71c2009-04-01 09:22:47 -04009142#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009143static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009144 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9145 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9146 .recover_open = nfs4_open_reclaim,
9147 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009148 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009149 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009150 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009151};
9152#endif /* CONFIG_NFS_V4_1 */
9153
Trond Myklebust17280172012-03-11 13:11:00 -04009154static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009155 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009156 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009157 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009159 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160};
9161
Andy Adamson591d71c2009-04-01 09:22:47 -04009162#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009163static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009164 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9165 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009166 .recover_open = nfs41_open_expired,
9167 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009168 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009169};
9170#endif /* CONFIG_NFS_V4_1 */
9171
Trond Myklebust17280172012-03-11 13:11:00 -04009172static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009173 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009174 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009175 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009176};
9177
9178#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009179static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009180 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009181 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009182 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009183};
9184#endif
9185
Chuck Leverec011fe2013-10-17 14:12:39 -04009186static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009187 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009188 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009189};
9190
9191#if defined(CONFIG_NFS_V4_1)
9192static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009193 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009194 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009195};
9196#endif /* CONFIG_NFS_V4_1 */
9197
Trond Myklebust97dc1352010-06-16 09:52:26 -04009198static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9199 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009200 .init_caps = NFS_CAP_READDIRPLUS
9201 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009202 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009203 .init_client = nfs40_init_client,
9204 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009205 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009206 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009207 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009208 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009209 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009210 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009211 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9212 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9213 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009214 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009215};
9216
9217#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009218static struct nfs_seqid *
9219nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9220{
9221 return NULL;
9222}
9223
Trond Myklebust97dc1352010-06-16 09:52:26 -04009224static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9225 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009226 .init_caps = NFS_CAP_READDIRPLUS
9227 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009228 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009229 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009230 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009231 .init_client = nfs41_init_client,
9232 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009233 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009234 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009235 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009236 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009237 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009238 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009239 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009240 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9241 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9242 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009243 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009244};
9245#endif
9246
Steve Dickson42c2c422013-05-22 12:50:38 -04009247#if defined(CONFIG_NFS_V4_2)
9248static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9249 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009250 .init_caps = NFS_CAP_READDIRPLUS
9251 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009252 | NFS_CAP_POSIX_LOCK
9253 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009254 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009255 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009256 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009257 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009258 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009259 | NFS_CAP_LAYOUTSTATS
9260 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009261 .init_client = nfs41_init_client,
9262 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009263 .match_stateid = nfs41_match_stateid,
9264 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009265 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009266 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009267 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009268 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009269 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009270 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9271 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9272 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009273 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009274};
9275#endif
9276
Trond Myklebust97dc1352010-06-16 09:52:26 -04009277const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9278 [0] = &nfs_v4_0_minor_ops,
9279#if defined(CONFIG_NFS_V4_1)
9280 [1] = &nfs_v4_1_minor_ops,
9281#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009282#if defined(CONFIG_NFS_V4_2)
9283 [2] = &nfs_v4_2_minor_ops,
9284#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009285};
9286
Trond Myklebust13997822016-07-24 17:10:52 -04009287static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009288{
9289 ssize_t error, error2;
9290
9291 error = generic_listxattr(dentry, list, size);
9292 if (error < 0)
9293 return error;
9294 if (list) {
9295 list += error;
9296 size -= error;
9297 }
9298
9299 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9300 if (error2 < 0)
9301 return error2;
9302 return error + error2;
9303}
9304
Trond Myklebust17f26b12013-08-21 15:48:42 -04009305static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009306 .create = nfs_create,
9307 .lookup = nfs_lookup,
9308 .atomic_open = nfs_atomic_open,
9309 .link = nfs_link,
9310 .unlink = nfs_unlink,
9311 .symlink = nfs_symlink,
9312 .mkdir = nfs_mkdir,
9313 .rmdir = nfs_rmdir,
9314 .mknod = nfs_mknod,
9315 .rename = nfs_rename,
9316 .permission = nfs_permission,
9317 .getattr = nfs_getattr,
9318 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009319 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009320};
9321
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009322static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009323 .permission = nfs_permission,
9324 .getattr = nfs_getattr,
9325 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009326 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009327};
9328
David Howells509de812006-08-22 20:06:11 -04009329const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .version = 4, /* protocol version */
9331 .dentry_ops = &nfs4_dentry_operations,
9332 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009333 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009334 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009336 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009337 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .getattr = nfs4_proc_getattr,
9339 .setattr = nfs4_proc_setattr,
9340 .lookup = nfs4_proc_lookup,
9341 .access = nfs4_proc_access,
9342 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 .create = nfs4_proc_create,
9344 .remove = nfs4_proc_remove,
9345 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009346 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009348 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009349 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009350 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 .link = nfs4_proc_link,
9352 .symlink = nfs4_proc_symlink,
9353 .mkdir = nfs4_proc_mkdir,
9354 .rmdir = nfs4_proc_remove,
9355 .readdir = nfs4_proc_readdir,
9356 .mknod = nfs4_proc_mknod,
9357 .statfs = nfs4_proc_statfs,
9358 .fsinfo = nfs4_proc_fsinfo,
9359 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009360 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009362 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009364 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009366 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009368 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009369 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009371 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009372 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009373 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009374 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009375 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009376 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009377 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009378 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009379 .create_server = nfs4_create_server,
9380 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381};
9382
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009383static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009384 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009385 .list = nfs4_xattr_list_nfs4_acl,
9386 .get = nfs4_xattr_get_nfs4_acl,
9387 .set = nfs4_xattr_set_nfs4_acl,
9388};
9389
9390const struct xattr_handler *nfs4_xattr_handlers[] = {
9391 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009392#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9393 &nfs4_xattr_nfs4_label_handler,
9394#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009395 NULL
9396};
9397
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398/*
9399 * Local variables:
9400 * c-basic-offset: 8
9401 * End:
9402 */