blob: ae5cde62195446903e6e7214f5e4f74bd9f637e1 [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
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust516285eb2015-09-20 16:15:24 -040081static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *, struct nfs4_state *, long *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84static 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 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040087 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040094#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040095
96#ifdef CONFIG_NFS_V4_SECURITY_LABEL
97static inline struct nfs4_label *
98nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
100{
101 int err;
102
103 if (label == NULL)
104 return NULL;
105
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
108
David Quigleyaa9c2662013-05-22 12:50:44 -0400109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400153 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400162 case -NFS4ERR_FILE_OPEN:
163 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700166 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * This is our standard bitmap for GETATTR requests.
174 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400175const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_TYPE
177 | FATTR4_WORD0_CHANGE
178 | FATTR4_WORD0_SIZE
179 | FATTR4_WORD0_FSID
180 | FATTR4_WORD0_FILEID,
181 FATTR4_WORD1_MODE
182 | FATTR4_WORD1_NUMLINKS
183 | FATTR4_WORD1_OWNER
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000298 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
NeilBrown8478eaa2014-09-18 16:09:27 +1000317static long nfs4_update_delay(long *timeout)
318{
319 long ret;
320 if (!timeout)
321 return NFS4_POLL_RETRY_MAX;
322 if (*timeout <= 0)
323 *timeout = NFS4_POLL_RETRY_MIN;
324 if (*timeout > NFS4_POLL_RETRY_MAX)
325 *timeout = NFS4_POLL_RETRY_MAX;
326 ret = *timeout;
327 *timeout <<= 1;
328 return ret;
329}
330
Trond Myklebust65de8722008-12-23 15:21:44 -0500331static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
332{
333 int res = 0;
334
335 might_sleep();
336
NeilBrown8478eaa2014-09-18 16:09:27 +1000337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500339 if (fatal_signal_pending(current))
340 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 return res;
342}
343
344/* This is the error handling routine for processes that are allowed
345 * to sleep.
346 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400347static int nfs4_do_handle_exception(struct nfs_server *server,
348 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500349{
350 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500351 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500352 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 int ret = errorcode;
354
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400355 exception->delay = 0;
356 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500357 exception->retry = 0;
358 switch(errorcode) {
359 case 0:
360 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400362 case -NFS4ERR_DELEG_REVOKED:
363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400365 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400366 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500367 exception->retry = 1;
368 return 0;
369 }
370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400386 case -NFS4ERR_MOVED:
387 ret = nfs4_schedule_migration_recovery(server);
388 if (ret < 0)
389 break;
390 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400391 case -NFS4ERR_LEASE_MOVED:
392 nfs4_schedule_lease_moved_recovery(clp);
393 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500394#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400395 case -NFS4ERR_BADSESSION:
396 case -NFS4ERR_BADSLOT:
397 case -NFS4ERR_BAD_HIGH_SLOT:
398 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
399 case -NFS4ERR_DEADSESSION:
400 case -NFS4ERR_SEQ_FALSE_RETRY:
401 case -NFS4ERR_SEQ_MISORDERED:
402 dprintk("%s ERROR: %d Reset session\n", __func__,
403 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400404 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400405 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500406#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500408 if (exception->timeout > HZ) {
409 /* We have retried a decent amount, time to
410 * fail
411 */
412 ret = -EBUSY;
413 break;
414 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500415 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400416 nfs_inc_server_stats(server, NFSIOS_DELAY);
417 case -NFS4ERR_GRACE:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400418 exception->delay = 1;
419 return 0;
420
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400421 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500422 case -NFS4ERR_OLD_STATEID:
423 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800424 break;
425 case -NFS4ERR_BADOWNER:
426 /* The following works around a Linux server bug! */
427 case -NFS4ERR_BADNAME:
428 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
429 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
430 exception->retry = 1;
431 printk(KERN_WARNING "NFS: v4 server %s "
432 "does not accept raw "
433 "uid/gids. "
434 "Reenabling the idmapper.\n",
435 server->nfs_client->cl_hostname);
436 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500437 }
438 /* We failed to handle the error */
439 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400441 exception->recovering = 1;
442 return 0;
443}
444
445/* This is the error handling routine for processes that are allowed
446 * to sleep.
447 */
448int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
449{
450 struct nfs_client *clp = server->nfs_client;
451 int ret;
452
453 ret = nfs4_do_handle_exception(server, errorcode, exception);
454 if (exception->delay) {
455 ret = nfs4_delay(server->client, &exception->timeout);
456 goto out_retry;
457 }
458 if (exception->recovering) {
459 ret = nfs4_wait_clnt_recover(clp);
460 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
461 return -EIO;
462 goto out_retry;
463 }
464 return ret;
465out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500466 if (ret == 0)
467 exception->retry = 1;
468 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500469}
470
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400471/*
472 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
473 * or 'false' otherwise.
474 */
475static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
476{
477 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
478
479 if (flavor == RPC_AUTH_GSS_KRB5I ||
480 flavor == RPC_AUTH_GSS_KRB5P)
481 return true;
482
483 return false;
484}
Trond Myklebust65de8722008-12-23 15:21:44 -0500485
Trond Myklebust452e9352010-07-31 14:29:06 -0400486static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 spin_lock(&clp->cl_lock);
489 if (time_before(clp->cl_last_renewal,timestamp))
490 clp->cl_last_renewal = timestamp;
491 spin_unlock(&clp->cl_lock);
492}
493
Trond Myklebust452e9352010-07-31 14:29:06 -0400494static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
495{
Trond Myklebustbe824162015-07-05 14:50:46 -0400496 struct nfs_client *clp = server->nfs_client;
497
498 if (!nfs4_has_session(clp))
499 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400500}
501
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400502struct nfs4_call_sync_data {
503 const struct nfs_server *seq_server;
504 struct nfs4_sequence_args *seq_args;
505 struct nfs4_sequence_res *seq_res;
506};
507
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800508void nfs4_init_sequence(struct nfs4_sequence_args *args,
509 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400510{
511 args->sa_slot = NULL;
512 args->sa_cache_this = cache_reply;
513 args->sa_privileged = 0;
514
515 res->sr_slot = NULL;
516}
517
518static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
519{
520 args->sa_privileged = 1;
521}
522
Peng Taocb04ad22014-06-11 05:24:15 +0800523int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
524 struct nfs4_sequence_args *args,
525 struct nfs4_sequence_res *res,
526 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400527{
Chuck Lever3bd23842013-08-09 12:49:19 -0400528 struct nfs4_slot *slot;
529
530 /* slot already allocated? */
531 if (res->sr_slot != NULL)
532 goto out_start;
533
534 spin_lock(&tbl->slot_tbl_lock);
535 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
536 goto out_sleep;
537
538 slot = nfs4_alloc_slot(tbl);
539 if (IS_ERR(slot)) {
540 if (slot == ERR_PTR(-ENOMEM))
541 task->tk_timeout = HZ >> 2;
542 goto out_sleep;
543 }
544 spin_unlock(&tbl->slot_tbl_lock);
545
546 args->sa_slot = slot;
547 res->sr_slot = slot;
548
549out_start:
550 rpc_call_start(task);
551 return 0;
552
553out_sleep:
554 if (args->sa_privileged)
555 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
556 NULL, RPC_PRIORITY_PRIVILEGED);
557 else
558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
559 spin_unlock(&tbl->slot_tbl_lock);
560 return -EAGAIN;
561}
Peng Taocb04ad22014-06-11 05:24:15 +0800562EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400563
564static int nfs40_sequence_done(struct rpc_task *task,
565 struct nfs4_sequence_res *res)
566{
567 struct nfs4_slot *slot = res->sr_slot;
568 struct nfs4_slot_table *tbl;
569
Trond Myklebustcab92c12014-01-29 12:12:15 -0500570 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400571 goto out;
572
573 tbl = slot->table;
574 spin_lock(&tbl->slot_tbl_lock);
575 if (!nfs41_wake_and_assign_slot(tbl, slot))
576 nfs4_free_slot(tbl, slot);
577 spin_unlock(&tbl->slot_tbl_lock);
578
579 res->sr_slot = NULL;
580out:
581 return 1;
582}
583
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400584#if defined(CONFIG_NFS_V4_1)
585
Trond Myklebustd185a332010-06-16 09:52:25 -0400586static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400587{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500588 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400589 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500590 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500591 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400592
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500593 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500594 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500595
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500596 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500597 /* Be nice to the server: try to ensure that the last transmitted
598 * value for highest_user_slotid <= target_highest_slotid
599 */
600 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
601 send_new_highest_used_slotid = true;
602
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500603 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500604 send_new_highest_used_slotid = false;
605 goto out_unlock;
606 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500607 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500608
609 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
610 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500611out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500612 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400613 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500614 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400615 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400616}
617
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500618int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400619{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500620 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500621 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400622 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500623 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500624 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400625
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500626 if (slot == NULL)
627 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400628 /* don't increment the sequence number if the task wasn't sent */
629 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400630 goto out;
631
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500632 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500633
Trond Myklebustac20d162012-12-15 15:36:07 -0500634 if (slot->interrupted) {
635 slot->interrupted = 0;
636 interrupted = true;
637 }
638
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400639 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500640 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400641 switch (res->sr_status) {
642 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400643 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500644 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500645 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500646 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500647 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400648 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500649 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400650 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500651 case 1:
652 /*
653 * sr_status remains 1 if an RPC level error occurred.
654 * The server may or may not have processed the sequence
655 * operation..
656 * Mark the slot as having hosted an interrupted RPC call.
657 */
658 slot->interrupted = 1;
659 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400660 case -NFS4ERR_DELAY:
661 /* The server detected a resend of the RPC call and
662 * returned NFS4ERR_DELAY as per Section 2.10.6.2
663 * of RFC5661.
664 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500665 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400666 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500667 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500668 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400669 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500670 case -NFS4ERR_BADSLOT:
671 /*
672 * The slot id we used was probably retired. Try again
673 * using a different slot id.
674 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500675 goto retry_nowait;
676 case -NFS4ERR_SEQ_MISORDERED:
677 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500678 * Was the last operation on this sequence interrupted?
679 * If so, retry after bumping the sequence number.
680 */
681 if (interrupted) {
682 ++slot->seq_nr;
683 goto retry_nowait;
684 }
685 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500686 * Could this slot have been previously retired?
687 * If so, then the server may be expecting seq_nr = 1!
688 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500689 if (slot->seq_nr != 1) {
690 slot->seq_nr = 1;
691 goto retry_nowait;
692 }
693 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500694 case -NFS4ERR_SEQ_FALSE_RETRY:
695 ++slot->seq_nr;
696 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400697 default:
698 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500699 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400700 }
701out:
702 /* The session may be reset by one of the error handlers. */
703 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400704 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500706 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500707retry_nowait:
708 if (rpc_restart_call_prepare(task)) {
709 task->tk_status = 0;
710 ret = 0;
711 }
712 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400713out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400714 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400715 goto out;
716 rpc_delay(task, NFS4_POLL_RETRY_MAX);
717 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400718}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500719EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720
Peng Tao2c4b1312014-06-11 05:24:16 +0800721int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400722{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400725 if (!res->sr_slot->table->session)
726 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400727 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400728}
Peng Tao2c4b1312014-06-11 05:24:16 +0800729EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400730
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000731int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400732 struct nfs4_sequence_args *args,
733 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400734 struct rpc_task *task)
735{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736 struct nfs4_slot *slot;
737 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738
739 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400741 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400742 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400744 tbl = &session->fc_slot_table;
745
Trond Myklebust69d206b2012-11-22 13:21:02 -0500746 task->tk_timeout = 0;
747
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400748 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400749 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400750 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500751 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500752 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400753 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400754 }
755
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500756 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500757 if (IS_ERR(slot)) {
758 /* If out of memory, try again in 1/4 second */
759 if (slot == ERR_PTR(-ENOMEM))
760 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400761 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400762 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400763 }
764 spin_unlock(&tbl->slot_tbl_lock);
765
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500766 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400767
Chuck Levere8d92382013-08-09 12:47:51 -0400768 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500769 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400770
Benny Halevydfb4f3092010-09-24 09:17:01 -0400771 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500772 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400773 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400774 /*
775 * sr_status is only set in decode_sequence, and so will remain
776 * set to 1 if an rpc level failure occurs.
777 */
778 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400779 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400780out_success:
781 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400782 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400783out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400784 /* Privileged tasks are queued with top priority */
785 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400786 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
787 NULL, RPC_PRIORITY_PRIVILEGED);
788 else
789 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400790 spin_unlock(&tbl->slot_tbl_lock);
791 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000793EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400794
Chuck Lever5a580e02013-08-09 12:48:09 -0400795static int nfs4_setup_sequence(const struct nfs_server *server,
796 struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res,
798 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799{
Trond Myklebust035168a2010-06-16 09:52:26 -0400800 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400801 int ret = 0;
802
Chuck Lever3bd23842013-08-09 12:49:19 -0400803 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800804 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
805 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400806
Chuck Levere8d92382013-08-09 12:47:51 -0400807 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400808 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400809 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400810
Trond Myklebust9d12b212012-01-17 22:04:25 -0500811 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400812
Andy Adamsonce5039c2009-04-01 09:22:13 -0400813 dprintk("<-- %s status=%d\n", __func__, ret);
814 return ret;
815}
816
Andy Adamsonce5039c2009-04-01 09:22:13 -0400817static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
818{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400819 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400820 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400821
Trond Myklebust035168a2010-06-16 09:52:26 -0400822 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
823
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400824 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400825}
826
Andy Adamson69ab40c2009-04-01 09:22:19 -0400827static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
828{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400829 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400830
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400832}
833
Trond Myklebust17280172012-03-11 13:11:00 -0400834static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400835 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400836 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400837};
838
Chuck Lever3bd23842013-08-09 12:49:19 -0400839#else /* !CONFIG_NFS_V4_1 */
840
Chuck Lever5a580e02013-08-09 12:48:09 -0400841static int nfs4_setup_sequence(const struct nfs_server *server,
842 struct nfs4_sequence_args *args,
843 struct nfs4_sequence_res *res,
844 struct rpc_task *task)
845{
Peng Taocb04ad22014-06-11 05:24:15 +0800846 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
847 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400848}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400849
Peng Tao2c4b1312014-06-11 05:24:16 +0800850int nfs4_sequence_done(struct rpc_task *task,
851 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400852{
Chuck Lever3bd23842013-08-09 12:49:19 -0400853 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400854}
Peng Tao2c4b1312014-06-11 05:24:16 +0800855EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400856
857#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400858
Chuck Lever9915ea72013-08-09 12:48:27 -0400859static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
860{
861 struct nfs4_call_sync_data *data = calldata;
862 nfs4_setup_sequence(data->seq_server,
863 data->seq_args, data->seq_res, task);
864}
865
866static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
867{
868 struct nfs4_call_sync_data *data = calldata;
869 nfs4_sequence_done(task, data->seq_res);
870}
871
872static const struct rpc_call_ops nfs40_call_sync_ops = {
873 .rpc_call_prepare = nfs40_call_sync_prepare,
874 .rpc_call_done = nfs40_call_sync_done,
875};
876
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400877static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400878 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 struct nfs4_sequence_args *args,
881 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400882{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883 int ret;
884 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400885 struct nfs_client *clp = server->nfs_client;
886 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 .seq_server = server,
888 .seq_args = args,
889 .seq_res = res,
890 };
891 struct rpc_task_setup task_setup = {
892 .rpc_client = clnt,
893 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400894 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895 .callback_data = &data
896 };
897
898 task = rpc_run_task(&task_setup);
899 if (IS_ERR(task))
900 ret = PTR_ERR(task);
901 else {
902 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400903 rpc_put_task(task);
904 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 return ret;
906}
907
Bryan Schumaker7c513052011-03-24 17:12:24 +0000908int nfs4_call_sync(struct rpc_clnt *clnt,
909 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000910 struct rpc_message *msg,
911 struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 int cache_reply)
914{
Chuck Levera9c92d62013-08-09 12:48:18 -0400915 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400916 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000917}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
920{
921 struct nfs_inode *nfsi = NFS_I(dir);
922
923 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400924 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700925 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700927 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500928 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000929 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 spin_unlock(&dir->i_lock);
931}
932
933struct nfs4_opendata {
934 struct kref kref;
935 struct nfs_openargs o_arg;
936 struct nfs_openres o_res;
937 struct nfs_open_confirmargs c_arg;
938 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500939 struct nfs4_string owner_name;
940 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +0800941 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400943 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400945 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct nfs4_state_owner *owner;
947 struct nfs4_state *state;
948 struct iattr attrs;
949 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400950 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400951 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400952 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400953 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int cancelled;
955};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400956
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400957static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
958 int err, struct nfs4_exception *exception)
959{
960 if (err != -EINVAL)
961 return false;
962 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
963 return false;
964 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
965 exception->retry = 1;
966 return true;
967}
968
Trond Myklebust6ae37332015-01-30 14:21:14 -0500969static u32
970nfs4_map_atomic_open_share(struct nfs_server *server,
971 fmode_t fmode, int openflags)
972{
973 u32 res = 0;
974
975 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
976 case FMODE_READ:
977 res = NFS4_SHARE_ACCESS_READ;
978 break;
979 case FMODE_WRITE:
980 res = NFS4_SHARE_ACCESS_WRITE;
981 break;
982 case FMODE_READ|FMODE_WRITE:
983 res = NFS4_SHARE_ACCESS_BOTH;
984 }
985 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
986 goto out;
987 /* Want no delegation if we're using O_DIRECT */
988 if (openflags & O_DIRECT)
989 res |= NFS4_SHARE_WANT_NO_DELEG;
990out:
991 return res;
992}
993
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400994static enum open_claim_type4
995nfs4_map_atomic_open_claim(struct nfs_server *server,
996 enum open_claim_type4 claim)
997{
998 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
999 return claim;
1000 switch (claim) {
1001 default:
1002 return claim;
1003 case NFS4_OPEN_CLAIM_FH:
1004 return NFS4_OPEN_CLAIM_NULL;
1005 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1006 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1007 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1008 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1009 }
1010}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001013{
1014 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001015 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001016 p->o_res.seqid = p->o_arg.seqid;
1017 p->c_res.seqid = p->c_arg.seqid;
1018 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001019 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001021 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001022}
1023
Al Viro82a2c1b2011-06-22 18:30:55 -04001024static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001025 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001026 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001027 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001028 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001029 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030{
Al Viro82a2c1b2011-06-22 18:30:55 -04001031 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001032 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001033 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001034 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 struct nfs4_opendata *p;
1036
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001037 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 if (p == NULL)
1039 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001040
1041 p->f_label = nfs4_label_alloc(server, gfp_mask);
1042 if (IS_ERR(p->f_label))
1043 goto err_free_p;
1044
Kinglong Meea49c2692015-07-27 15:31:38 +08001045 p->a_label = nfs4_label_alloc(server, gfp_mask);
1046 if (IS_ERR(p->a_label))
1047 goto err_free_f;
1048
Trond Myklebust63f5f792015-01-23 19:19:25 -05001049 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1050 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001051 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001052 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001053 nfs_sb_active(dentry->d_sb);
1054 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001055 p->dir = parent;
1056 p->owner = sp;
1057 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 p->o_arg.open_flags = flags;
1059 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001060 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1061 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001062 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1063 * will return permission denied for all bits until close */
1064 if (!(flags & O_EXCL)) {
1065 /* ask server to check for all possible rights as results
1066 * are cached */
1067 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1068 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001069 }
David Howells7539bba2006-08-22 20:06:09 -04001070 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001071 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1072 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001073 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001074 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001075 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001076 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001077 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001078 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1079 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001080 case NFS4_OPEN_CLAIM_NULL:
1081 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1082 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1083 p->o_arg.fh = NFS_FH(dir);
1084 break;
1085 case NFS4_OPEN_CLAIM_PREVIOUS:
1086 case NFS4_OPEN_CLAIM_FH:
1087 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1088 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001089 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001090 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001091 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001092 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001093
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001094 p->o_arg.u.attrs = &p->attrs;
1095 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001096
1097 verf[0] = jiffies;
1098 verf[1] = current->pid;
1099 memcpy(p->o_arg.u.verifier.data, verf,
1100 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001101 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001102 p->c_arg.fh = &p->o_res.fh;
1103 p->c_arg.stateid = &p->o_res.stateid;
1104 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001106 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001107 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001108
1109err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001110 nfs4_label_free(p->a_label);
1111err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001112 nfs4_label_free(p->f_label);
1113err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001114 kfree(p);
1115err:
1116 dput(parent);
1117 return NULL;
1118}
1119
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001120static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001121{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001122 struct nfs4_opendata *p = container_of(kref,
1123 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001124 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001125
1126 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001127 if (p->state != NULL)
1128 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001129 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001130
Kinglong Meea49c2692015-07-27 15:31:38 +08001131 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001132 nfs4_label_free(p->f_label);
1133
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001134 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001135 dput(p->dentry);
1136 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001137 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001138 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001139 kfree(p);
1140}
1141
1142static void nfs4_opendata_put(struct nfs4_opendata *p)
1143{
1144 if (p != NULL)
1145 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146}
1147
Trond Myklebust06f814a2006-01-03 09:55:07 +01001148static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1149{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001150 int ret;
1151
Trond Myklebust06f814a2006-01-03 09:55:07 +01001152 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001153 return ret;
1154}
1155
Trond Myklebust24311f82015-09-20 10:50:17 -04001156static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1157 fmode_t fmode)
1158{
1159 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1160 case FMODE_READ|FMODE_WRITE:
1161 return state->n_rdwr != 0;
1162 case FMODE_WRITE:
1163 return state->n_wronly != 0;
1164 case FMODE_READ:
1165 return state->n_rdonly != 0;
1166 }
1167 WARN_ON_ONCE(1);
1168 return false;
1169}
1170
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001172{
1173 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174
Trond Myklebust536e43d2012-01-17 22:04:26 -05001175 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001176 goto out;
1177 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001178 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001179 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1180 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001181 break;
1182 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001183 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1184 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001185 break;
1186 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001187 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1188 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001189 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001190out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001191 return ret;
1192}
1193
Trond Myklebust2a606182015-08-19 22:30:00 -05001194static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1195 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001197 if (delegation == NULL)
1198 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001199 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001200 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001201 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1202 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001203 switch (claim) {
1204 case NFS4_OPEN_CLAIM_NULL:
1205 case NFS4_OPEN_CLAIM_FH:
1206 break;
1207 case NFS4_OPEN_CLAIM_PREVIOUS:
1208 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1209 break;
1210 default:
1211 return 0;
1212 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001213 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001214 return 1;
1215}
1216
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001217static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001218{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001220 case FMODE_WRITE:
1221 state->n_wronly++;
1222 break;
1223 case FMODE_READ:
1224 state->n_rdonly++;
1225 break;
1226 case FMODE_READ|FMODE_WRITE:
1227 state->n_rdwr++;
1228 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001229 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001230}
1231
Trond Myklebust4f14c192014-02-12 19:15:06 -05001232static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001233{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001234 struct nfs_client *clp = state->owner->so_server->nfs_client;
1235 bool need_recover = false;
1236
1237 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1238 need_recover = true;
1239 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1240 need_recover = true;
1241 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1242 need_recover = true;
1243 if (need_recover)
1244 nfs4_state_mark_reclaim_nograce(clp, state);
1245}
1246
Trond Myklebuste999e802014-02-10 18:20:47 -05001247static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1248 nfs4_stateid *stateid)
1249{
1250 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1251 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001252 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1253 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001254 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001255 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001256 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1257 return true;
1258 return false;
1259}
1260
Trond Myklebustf95549c2015-01-23 18:06:09 -05001261static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1262{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001263 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1264 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001265 if (state->n_wronly)
1266 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1267 if (state->n_rdonly)
1268 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1269 if (state->n_rdwr)
1270 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001271 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001272}
1273
Trond Myklebust226056c2014-02-11 10:41:07 -05001274static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001275 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001276 nfs4_stateid *stateid, fmode_t fmode)
1277{
1278 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1279 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1280 case FMODE_WRITE:
1281 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1282 break;
1283 case FMODE_READ:
1284 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1285 break;
1286 case 0:
1287 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1288 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1289 clear_bit(NFS_OPEN_STATE, &state->flags);
1290 }
1291 if (stateid == NULL)
1292 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001293 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001294 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1295 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1296 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001297 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001298 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001299 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001300 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001301 nfs4_stateid_copy(&state->stateid, stateid);
1302 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001303}
1304
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001305static void nfs_clear_open_stateid(struct nfs4_state *state,
1306 nfs4_stateid *arg_stateid,
1307 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001308{
1309 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001310 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001311 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001312 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1313 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001314}
1315
Trond Myklebust003707c2007-07-05 18:07:55 -04001316static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1317{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001319 case FMODE_READ:
1320 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1321 break;
1322 case FMODE_WRITE:
1323 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1324 break;
1325 case FMODE_READ|FMODE_WRITE:
1326 set_bit(NFS_O_RDWR_STATE, &state->flags);
1327 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001328 if (!nfs_need_update_open_stateid(state, stateid))
1329 return;
1330 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1331 nfs4_stateid_copy(&state->stateid, stateid);
1332 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001333}
1334
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001337 /*
1338 * Protect the call to nfs4_state_set_mode_locked and
1339 * serialise the stateid update
1340 */
1341 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001342 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001343 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001344 set_bit(NFS_DELEGATED_STATE, &state->flags);
1345 }
1346 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001348 write_sequnlock(&state->seqlock);
1349 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001351 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001354static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001355{
1356 struct nfs_inode *nfsi = NFS_I(state->inode);
1357 struct nfs_delegation *deleg_cur;
1358 int ret = 0;
1359
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001361
1362 rcu_read_lock();
1363 deleg_cur = rcu_dereference(nfsi->delegation);
1364 if (deleg_cur == NULL)
1365 goto no_delegation;
1366
1367 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001368 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001369 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001371 goto no_delegation_unlock;
1372
1373 if (delegation == NULL)
1374 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001375 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001376 goto no_delegation_unlock;
1377
Trond Myklebustb7391f42008-12-23 15:21:52 -05001378 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001380 ret = 1;
1381no_delegation_unlock:
1382 spin_unlock(&deleg_cur->lock);
1383no_delegation:
1384 rcu_read_unlock();
1385
1386 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001388 ret = 1;
1389 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001390 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1391 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001392
1393 return ret;
1394}
1395
Trond Myklebust39071e62015-01-24 15:07:56 -05001396static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1397 const nfs4_stateid *stateid)
1398{
1399 struct nfs4_state *state = lsp->ls_state;
1400 bool ret = false;
1401
1402 spin_lock(&state->state_lock);
1403 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1404 goto out_noupdate;
1405 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1406 goto out_noupdate;
1407 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1408 ret = true;
1409out_noupdate:
1410 spin_unlock(&state->state_lock);
1411 return ret;
1412}
Trond Myklebust34310432008-12-23 15:21:38 -05001413
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001414static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001415{
1416 struct nfs_delegation *delegation;
1417
1418 rcu_read_lock();
1419 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001420 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001421 rcu_read_unlock();
1422 return;
1423 }
1424 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001425 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001426}
1427
Trond Myklebust6ee41262007-07-08 14:11:36 -04001428static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001429{
1430 struct nfs4_state *state = opendata->state;
1431 struct nfs_inode *nfsi = NFS_I(state->inode);
1432 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001433 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001434 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001435 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001436 nfs4_stateid stateid;
1437 int ret = -EAGAIN;
1438
Trond Myklebustaac00a82007-07-05 19:02:21 -04001439 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001440 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001442 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001444 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001446 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001447 rcu_read_lock();
1448 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001449 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001450 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001451 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001452 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001454 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001455 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001456 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001457 if (!opendata->is_recover) {
1458 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1459 if (ret != 0)
1460 goto out;
1461 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001462 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001463
1464 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001465 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001466 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468out:
1469 return ERR_PTR(ret);
1470out_return_state:
1471 atomic_inc(&state->count);
1472 return state;
1473}
1474
Andy Adamsone23008e2012-10-02 21:07:32 -04001475static void
1476nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1477{
1478 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1479 struct nfs_delegation *delegation;
1480 int delegation_flags = 0;
1481
1482 rcu_read_lock();
1483 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1484 if (delegation)
1485 delegation_flags = delegation->flags;
1486 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001487 switch (data->o_arg.claim) {
1488 default:
1489 break;
1490 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1491 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001492 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1493 "returning a delegation for "
1494 "OPEN(CLAIM_DELEGATE_CUR)\n",
1495 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001496 return;
1497 }
1498 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001499 nfs_inode_set_delegation(state->inode,
1500 data->owner->so_cred,
1501 &data->o_res);
1502 else
1503 nfs_inode_reclaim_delegation(state->inode,
1504 data->owner->so_cred,
1505 &data->o_res);
1506}
1507
1508/*
1509 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1510 * and update the nfs4_state.
1511 */
1512static struct nfs4_state *
1513_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1514{
1515 struct inode *inode = data->state->inode;
1516 struct nfs4_state *state = data->state;
1517 int ret;
1518
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001519 if (!data->rpc_done) {
1520 if (data->rpc_status) {
1521 ret = data->rpc_status;
1522 goto err;
1523 }
1524 /* cached opens have already been processed */
1525 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001526 }
1527
Andy Adamsone23008e2012-10-02 21:07:32 -04001528 ret = nfs_refresh_inode(inode, &data->f_attr);
1529 if (ret)
1530 goto err;
1531
1532 if (data->o_res.delegation_type != 0)
1533 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001534update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001535 update_open_stateid(state, &data->o_res.stateid, NULL,
1536 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001537 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001538
1539 return state;
1540err:
1541 return ERR_PTR(ret);
1542
1543}
1544
1545static struct nfs4_state *
1546_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547{
1548 struct inode *inode;
1549 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001553 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001554 goto out;
1555 }
1556
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001559 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001560 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001561 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001562 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001563 goto err;
1564 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565 state = nfs4_get_open_state(inode, data->owner);
1566 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001567 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001568 if (data->o_res.delegation_type != 0)
1569 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001570 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001571 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001574 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001575 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001576err_put_inode:
1577 iput(inode);
1578err:
1579 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580}
1581
Andy Adamsone23008e2012-10-02 21:07:32 -04001582static struct nfs4_state *
1583nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1584{
1585 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1586 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1587 return _nfs4_opendata_to_nfs4_state(data);
1588}
1589
Trond Myklebust864472e2006-01-03 09:55:15 +01001590static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1591{
1592 struct nfs_inode *nfsi = NFS_I(state->inode);
1593 struct nfs_open_context *ctx;
1594
1595 spin_lock(&state->inode->i_lock);
1596 list_for_each_entry(ctx, &nfsi->open_files, list) {
1597 if (ctx->state != state)
1598 continue;
1599 get_nfs_open_context(ctx);
1600 spin_unlock(&state->inode->i_lock);
1601 return ctx;
1602 }
1603 spin_unlock(&state->inode->i_lock);
1604 return ERR_PTR(-ENOENT);
1605}
1606
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001607static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1608 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001609{
1610 struct nfs4_opendata *opendata;
1611
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001612 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001613 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001614 if (opendata == NULL)
1615 return ERR_PTR(-ENOMEM);
1616 opendata->state = state;
1617 atomic_inc(&state->count);
1618 return opendata;
1619}
1620
Trond Myklebust24311f82015-09-20 10:50:17 -04001621static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1622 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001623{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001624 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001625 int ret;
1626
Trond Myklebust24311f82015-09-20 10:50:17 -04001627 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001628 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001629 opendata->o_arg.open_flags = 0;
1630 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001631 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1632 NFS_SB(opendata->dentry->d_sb),
1633 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001634 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1635 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1636 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001637 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001638 if (ret != 0)
1639 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001640 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001641 if (IS_ERR(newstate))
1642 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001643 if (newstate != opendata->state)
1644 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001645 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001646 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001647}
1648
1649static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1650{
Trond Myklebust864472e2006-01-03 09:55:15 +01001651 int ret;
1652
Trond Myklebust4f14c192014-02-12 19:15:06 -05001653 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1654 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1655 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1656 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001657 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001658 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001659 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001660 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001661 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1662 if (ret != 0)
1663 return ret;
1664 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1665 if (ret != 0)
1666 return ret;
1667 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1668 if (ret != 0)
1669 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001670 /*
1671 * We may have performed cached opens for all three recoveries.
1672 * Check if we need to update the current stateid.
1673 */
1674 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001675 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001676 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001677 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001678 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001679 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001680 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 return 0;
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/*
1685 * OPEN_RECLAIM:
1686 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001688static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001690 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001691 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int status;
1694
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001695 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1696 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001697 if (IS_ERR(opendata))
1698 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001699 rcu_read_lock();
1700 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001701 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001702 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001703 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 opendata->o_arg.u.delegation_type = delegation_type;
1705 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001706 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return status;
1708}
1709
Trond Myklebust539cd032007-06-05 11:46:42 -04001710static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct nfs_server *server = NFS_SERVER(state->inode);
1713 struct nfs4_exception exception = { };
1714 int err;
1715 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001716 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001717 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001718 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1719 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001720 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001721 break;
1722 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 } while (exception.retry);
1724 return err;
1725}
1726
Trond Myklebust864472e2006-01-03 09:55:15 +01001727static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1728{
1729 struct nfs_open_context *ctx;
1730 int ret;
1731
1732 ctx = nfs4_state_find_open_context(state);
1733 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001734 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001735 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001736 put_nfs_open_context(ctx);
1737 return ret;
1738}
1739
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001740static 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 -07001741{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001742 switch (err) {
1743 default:
1744 printk(KERN_ERR "NFS: %s: unhandled error "
1745 "%d.\n", __func__, err);
1746 case 0:
1747 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001748 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001749 case -ESTALE:
1750 break;
1751 case -NFS4ERR_BADSESSION:
1752 case -NFS4ERR_BADSLOT:
1753 case -NFS4ERR_BAD_HIGH_SLOT:
1754 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1755 case -NFS4ERR_DEADSESSION:
1756 set_bit(NFS_DELEGATED_STATE, &state->flags);
1757 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1758 return -EAGAIN;
1759 case -NFS4ERR_STALE_CLIENTID:
1760 case -NFS4ERR_STALE_STATEID:
1761 set_bit(NFS_DELEGATED_STATE, &state->flags);
1762 case -NFS4ERR_EXPIRED:
1763 /* Don't recall a delegation if it was lost */
1764 nfs4_schedule_lease_recovery(server->nfs_client);
1765 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001766 case -NFS4ERR_MOVED:
1767 nfs4_schedule_migration_recovery(server);
1768 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001769 case -NFS4ERR_LEASE_MOVED:
1770 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1771 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001772 case -NFS4ERR_DELEG_REVOKED:
1773 case -NFS4ERR_ADMIN_REVOKED:
1774 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001775 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001776 nfs_inode_find_state_and_recover(state->inode,
1777 stateid);
1778 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001779 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001780 case -NFS4ERR_DELAY:
1781 case -NFS4ERR_GRACE:
1782 set_bit(NFS_DELEGATED_STATE, &state->flags);
1783 ssleep(1);
1784 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001785 case -ENOMEM:
1786 case -NFS4ERR_DENIED:
1787 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1788 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return err;
1791}
1792
Trond Myklebust24311f82015-09-20 10:50:17 -04001793int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1794 struct nfs4_state *state, const nfs4_stateid *stateid,
1795 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001796{
1797 struct nfs_server *server = NFS_SERVER(state->inode);
1798 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001799 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001800
1801 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1802 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1803 if (IS_ERR(opendata))
1804 return PTR_ERR(opendata);
1805 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001806 write_seqlock(&state->seqlock);
1807 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1808 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001809 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1810 switch (type & (FMODE_READ|FMODE_WRITE)) {
1811 case FMODE_READ|FMODE_WRITE:
1812 case FMODE_WRITE:
1813 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1814 if (err)
1815 break;
1816 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1817 if (err)
1818 break;
1819 case FMODE_READ:
1820 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1821 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001822 nfs4_opendata_put(opendata);
1823 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1824}
1825
Chuck Leverbe05c862013-08-09 12:49:47 -04001826static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1827{
1828 struct nfs4_opendata *data = calldata;
1829
Peng Taocb04ad22014-06-11 05:24:15 +08001830 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1831 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001832}
1833
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001834static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1835{
1836 struct nfs4_opendata *data = calldata;
1837
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001838 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001839
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001840 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001841 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001842 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001843 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001844 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001845 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001846 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001847}
1848
1849static void nfs4_open_confirm_release(void *calldata)
1850{
1851 struct nfs4_opendata *data = calldata;
1852 struct nfs4_state *state = NULL;
1853
1854 /* If this request hasn't been cancelled, do nothing */
1855 if (data->cancelled == 0)
1856 goto out_free;
1857 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001858 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001859 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001860 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001861 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001862 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001863out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001864 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001865}
1866
1867static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001868 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001869 .rpc_call_done = nfs4_open_confirm_done,
1870 .rpc_release = nfs4_open_confirm_release,
1871};
1872
1873/*
1874 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1875 */
1876static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1877{
David Howells2b0143b2015-03-17 22:25:59 +00001878 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001879 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001880 struct rpc_message msg = {
1881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1882 .rpc_argp = &data->c_arg,
1883 .rpc_resp = &data->c_res,
1884 .rpc_cred = data->owner->so_cred,
1885 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001886 struct rpc_task_setup task_setup_data = {
1887 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001888 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001889 .callback_ops = &nfs4_open_confirm_ops,
1890 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001891 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001892 .flags = RPC_TASK_ASYNC,
1893 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int status;
1895
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001896 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001897 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001898 data->rpc_done = 0;
1899 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001900 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001901 if (data->is_recover)
1902 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001903 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001904 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001905 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001906 status = nfs4_wait_for_completion_rpc_task(task);
1907 if (status != 0) {
1908 data->cancelled = 1;
1909 smp_wmb();
1910 } else
1911 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001912 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return status;
1914}
1915
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001916static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001918 struct nfs4_opendata *data = calldata;
1919 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001920 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001921 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001922
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001923 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001924 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001925 /*
1926 * Check if we still need to send an OPEN call, or if we can use
1927 * a delegation instead.
1928 */
1929 if (data->state != NULL) {
1930 struct nfs_delegation *delegation;
1931
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001932 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001933 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001934 rcu_read_lock();
1935 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001936 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05001937 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001938 rcu_read_unlock();
1939 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001940 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001941 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05001942 switch (claim) {
1943 default:
1944 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04001945 case NFS4_OPEN_CLAIM_PREVIOUS:
1946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1947 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001948 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001949 case NFS4_OPEN_CLAIM_FH:
1950 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001951 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1952 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001953 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001954 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001955 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001956 &data->o_res.seq_res,
1957 task) != 0)
1958 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001959
1960 /* Set the create mode (note dependency on the session type) */
1961 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1962 if (data->o_arg.open_flags & O_EXCL) {
1963 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1964 if (nfs4_has_persistent_session(clp))
1965 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1966 else if (clp->cl_mvops->minor_version > 0)
1967 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1968 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001969 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001970unlock_no_action:
1971 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001972out_no_action:
1973 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001974out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001975 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001976}
1977
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001978static void nfs4_open_done(struct rpc_task *task, void *calldata)
1979{
1980 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001982 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001983
Trond Myklebust14516c32010-07-31 14:29:06 -04001984 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1985 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001986
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001987 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001988 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1989 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001990 case S_IFREG:
1991 break;
1992 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001993 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001994 break;
1995 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001996 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001997 break;
1998 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001999 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002000 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002001 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002002 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002003 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2004 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002005 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002006 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002007}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002008
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002009static void nfs4_open_release(void *calldata)
2010{
2011 struct nfs4_opendata *data = calldata;
2012 struct nfs4_state *state = NULL;
2013
2014 /* If this request hasn't been cancelled, do nothing */
2015 if (data->cancelled == 0)
2016 goto out_free;
2017 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002018 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002019 goto out_free;
2020 /* In case we need an open_confirm, no cleanup! */
2021 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2022 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002023 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002024 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002025 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002026out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002027 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002028}
2029
2030static const struct rpc_call_ops nfs4_open_ops = {
2031 .rpc_call_prepare = nfs4_open_prepare,
2032 .rpc_call_done = nfs4_open_done,
2033 .rpc_release = nfs4_open_release,
2034};
2035
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002036static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037{
David Howells2b0143b2015-03-17 22:25:59 +00002038 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039 struct nfs_server *server = NFS_SERVER(dir);
2040 struct nfs_openargs *o_arg = &data->o_arg;
2041 struct nfs_openres *o_res = &data->o_res;
2042 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002043 struct rpc_message msg = {
2044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2045 .rpc_argp = o_arg,
2046 .rpc_resp = o_res,
2047 .rpc_cred = data->owner->so_cred,
2048 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002049 struct rpc_task_setup task_setup_data = {
2050 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002051 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002052 .callback_ops = &nfs4_open_ops,
2053 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002054 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002055 .flags = RPC_TASK_ASYNC,
2056 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002057 int status;
2058
Chuck Levera9c92d62013-08-09 12:48:18 -04002059 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002060 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002061 data->rpc_done = 0;
2062 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002063 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002064 data->is_recover = 0;
2065 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002066 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002067 data->is_recover = 1;
2068 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002069 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002070 if (IS_ERR(task))
2071 return PTR_ERR(task);
2072 status = nfs4_wait_for_completion_rpc_task(task);
2073 if (status != 0) {
2074 data->cancelled = 1;
2075 smp_wmb();
2076 } else
2077 status = data->rpc_status;
2078 rpc_put_task(task);
2079
2080 return status;
2081}
2082
2083static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2084{
David Howells2b0143b2015-03-17 22:25:59 +00002085 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002086 struct nfs_openres *o_res = &data->o_res;
2087 int status;
2088
2089 status = nfs4_run_open_task(data, 1);
2090 if (status != 0 || !data->rpc_done)
2091 return status;
2092
Trond Myklebust6926afd2012-01-07 13:22:46 -05002093 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2094
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002095 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2096 status = _nfs4_proc_open_confirm(data);
2097 if (status != 0)
2098 return status;
2099 }
2100
2101 return status;
2102}
2103
Trond Myklebustf3792d62014-07-10 08:54:32 -04002104/*
2105 * Additional permission checks in order to distinguish between an
2106 * open for read, and an open for execute. This works around the
2107 * fact that NFSv4 OPEN treats read and execute permissions as being
2108 * the same.
2109 * Note that in the non-execute case, we want to turn off permission
2110 * checking if we just created a new file (POSIX open() semantics).
2111 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002112static int nfs4_opendata_access(struct rpc_cred *cred,
2113 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002114 struct nfs4_state *state, fmode_t fmode,
2115 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002116{
2117 struct nfs_access_entry cache;
2118 u32 mask;
2119
2120 /* access call failed or for some reason the server doesn't
2121 * support any access modes -- defer access call until later */
2122 if (opendata->o_res.access_supported == 0)
2123 return 0;
2124
2125 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002126 /*
2127 * Use openflags to check for exec, because fmode won't
2128 * always have FMODE_EXEC set when file open for exec.
2129 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002130 if (openflags & __FMODE_EXEC) {
2131 /* ONLY check for exec rights */
2132 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002133 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002134 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002135
2136 cache.cred = cred;
2137 cache.jiffies = jiffies;
2138 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2139 nfs_access_add_cache(state->inode, &cache);
2140
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002141 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002142 return 0;
2143
2144 /* even though OPEN succeeded, access is denied. Close the file */
2145 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002146 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002147}
2148
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002149/*
2150 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2151 */
2152static int _nfs4_proc_open(struct nfs4_opendata *data)
2153{
David Howells2b0143b2015-03-17 22:25:59 +00002154 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002155 struct nfs_server *server = NFS_SERVER(dir);
2156 struct nfs_openargs *o_arg = &data->o_arg;
2157 struct nfs_openres *o_res = &data->o_res;
2158 int status;
2159
2160 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002161 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002162 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002163 if (status != 0) {
2164 if (status == -NFS4ERR_BADNAME &&
2165 !(o_arg->open_flags & O_CREAT))
2166 return -ENOENT;
2167 return status;
2168 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002169
Trond Myklebust6926afd2012-01-07 13:22:46 -05002170 nfs_fattr_map_and_free_names(server, &data->f_attr);
2171
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002172 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002173 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002174 if (o_arg->open_flags & O_EXCL)
2175 data->file_created = 1;
2176 else if (o_res->cinfo.before != o_res->cinfo.after)
2177 data->file_created = 1;
2178 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002179 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2180 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002182 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002184 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002187 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Andy Adamsond83217c2011-03-01 01:34:17 +00002191static int nfs4_recover_expired_lease(struct nfs_server *server)
2192{
2193 return nfs4_client_recover_expired_lease(server->nfs_client);
2194}
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196/*
2197 * OPEN_EXPIRED:
2198 * reclaim state on the server after a network partition.
2199 * Assumes caller holds the appropriate lock
2200 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002201static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002203 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002204 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002206 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002207 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002208 if (IS_ERR(opendata))
2209 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002210 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002211 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002212 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002213 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002217static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002218{
Trond Myklebust539cd032007-06-05 11:46:42 -04002219 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002220 struct nfs4_exception exception = { };
2221 int err;
2222
2223 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002224 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002225 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002226 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2227 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002228 switch (err) {
2229 default:
2230 goto out;
2231 case -NFS4ERR_GRACE:
2232 case -NFS4ERR_DELAY:
2233 nfs4_handle_exception(server, err, &exception);
2234 err = 0;
2235 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002236 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002237out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002238 return err;
2239}
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Trond Myklebust864472e2006-01-03 09:55:15 +01002246 ctx = nfs4_state_find_open_context(state);
2247 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002248 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002249 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002250 put_nfs_open_context(ctx);
2251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002254static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2255{
2256 nfs_remove_bad_delegation(state->inode);
2257 write_seqlock(&state->seqlock);
2258 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2259 write_sequnlock(&state->seqlock);
2260 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2261}
2262
2263static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2264{
2265 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2266 nfs_finish_clear_delegation_stateid(state);
2267}
2268
2269static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2270{
2271 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2272 nfs40_clear_delegation_stateid(state);
2273 return nfs4_open_expired(sp, state);
2274}
2275
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002276#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002277static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002278{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002279 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002280 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002281 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002282 struct rpc_cred *cred;
2283 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002284
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002285 /* Get the delegation credential for use by test/free_stateid */
2286 rcu_read_lock();
2287 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002288 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002289 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002290 return;
2291 }
2292
2293 nfs4_stateid_copy(&stateid, &delegation->stateid);
2294 cred = get_rpccred(delegation->cred);
2295 rcu_read_unlock();
2296 status = nfs41_test_stateid(server, &stateid, cred);
2297 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002298
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002299 if (status != NFS_OK) {
2300 /* Free the stateid unless the server explicitly
2301 * informs us the stateid is unrecognized. */
2302 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002303 nfs41_free_stateid(server, &stateid, cred);
2304 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002305 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002306
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002307 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002308}
2309
2310/**
2311 * nfs41_check_open_stateid - possibly free an open stateid
2312 *
2313 * @state: NFSv4 state for an inode
2314 *
2315 * Returns NFS_OK if recovery for this stateid is now finished.
2316 * Otherwise a negative NFS4ERR value is returned.
2317 */
2318static int nfs41_check_open_stateid(struct nfs4_state *state)
2319{
2320 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002321 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002322 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002323 int status;
2324
2325 /* If a state reset has been done, test_stateid is unneeded */
2326 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2327 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2328 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2329 return -NFS4ERR_BAD_STATEID;
2330
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002331 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002332 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002333 if (status != NFS_OK) {
2334 /* Free the stateid unless the server explicitly
2335 * informs us the stateid is unrecognized. */
2336 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002337 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002338
2339 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2340 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2341 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002342 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002343 }
2344 return status;
2345}
2346
2347static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2348{
Chuck Levereb64cf92012-07-11 16:30:05 -04002349 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002350
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002351 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002352 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002353 if (status != NFS_OK)
2354 status = nfs4_open_expired(sp, state);
2355 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002356}
2357#endif
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002360 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2361 * fields corresponding to attributes that were used to store the verifier.
2362 * Make sure we clobber those fields in the later setattr call
2363 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002364static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2365 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002366{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002367 const u32 *attrset = opendata->o_res.attrset;
2368
2369 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002370 !(sattr->ia_valid & ATTR_ATIME_SET))
2371 sattr->ia_valid |= ATTR_ATIME;
2372
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002373 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002374 !(sattr->ia_valid & ATTR_MTIME_SET))
2375 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002376
2377 /* Except MODE, it seems harmless of setting twice. */
2378 if ((attrset[1] & FATTR4_WORD1_MODE))
2379 sattr->ia_valid &= ~ATTR_MODE;
2380
2381 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2382 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002383}
2384
Trond Myklebustc21443c2013-02-07 14:26:21 -05002385static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2386 fmode_t fmode,
2387 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002388 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002389{
2390 struct nfs4_state_owner *sp = opendata->owner;
2391 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002392 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002393 struct nfs4_state *state;
2394 unsigned int seq;
2395 int ret;
2396
2397 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2398
2399 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002400 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002401 goto out;
2402
2403 state = nfs4_opendata_to_nfs4_state(opendata);
2404 ret = PTR_ERR(state);
2405 if (IS_ERR(state))
2406 goto out;
2407 if (server->caps & NFS_CAP_POSIX_LOCK)
2408 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2409
Trond Myklebust275bb302013-05-29 13:11:28 -04002410 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002411 if (d_really_is_negative(dentry)) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002412 /* FIXME: Is this d_drop() ever needed? */
2413 d_drop(dentry);
2414 dentry = d_add_unique(dentry, igrab(state->inode));
2415 if (dentry == NULL) {
2416 dentry = opendata->dentry;
2417 } else if (dentry != ctx->dentry) {
2418 dput(ctx->dentry);
2419 ctx->dentry = dget(dentry);
2420 }
2421 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002422 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002423 }
2424
Trond Myklebustc21443c2013-02-07 14:26:21 -05002425 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2426 if (ret != 0)
2427 goto out;
2428
Trond Myklebust3efb9722013-05-29 13:17:04 -04002429 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002430 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002431 nfs_inode_attach_open_context(ctx);
2432 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2433 nfs4_schedule_stateid_recovery(server, state);
2434 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002435out:
2436 return ret;
2437}
2438
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002439/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002440 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 */
Andy Adamson82be4172012-05-23 05:02:35 -04002442static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002443 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002444 int flags,
2445 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002446 struct nfs4_label *label,
2447 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 struct nfs4_state_owner *sp;
2450 struct nfs4_state *state = NULL;
2451 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002452 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002453 struct dentry *dentry = ctx->dentry;
2454 struct rpc_cred *cred = ctx->cred;
2455 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2456 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002457 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002458 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002459 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002463 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2464 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2466 goto out_err;
2467 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002468 status = nfs4_recover_expired_lease(server);
2469 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002470 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002471 if (d_really_is_positive(dentry))
2472 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002473 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002474 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002475 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002476 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002477 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002478 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002479 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
David Quigley14c43f72013-05-22 12:50:43 -04002481 if (label) {
2482 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2483 if (IS_ERR(olabel)) {
2484 status = PTR_ERR(olabel);
2485 goto err_opendata_put;
2486 }
2487 }
2488
Trond Myklebuste911b812014-03-26 13:24:37 -07002489 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2490 if (!opendata->f_attr.mdsthreshold) {
2491 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2492 if (!opendata->f_attr.mdsthreshold)
2493 goto err_free_label;
2494 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002495 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002496 }
David Howells2b0143b2015-03-17 22:25:59 +00002497 if (d_really_is_positive(dentry))
2498 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002499
Trond Myklebust3efb9722013-05-29 13:17:04 -04002500 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002501 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002502 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002503 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002504
NeilBrownefcbc042015-07-30 13:00:56 +10002505 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002506 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002507 nfs4_exclusive_attrset(opendata, sattr, &label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002508
2509 nfs_fattr_init(opendata->o_res.f_attr);
2510 status = nfs4_do_setattr(state->inode, cred,
2511 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002512 state, label, olabel);
2513 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05002514 nfs_setattr_update_inode(state->inode, sattr,
2515 opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002516 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002517 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002518 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002519 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002520 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002521
Trond Myklebuste911b812014-03-26 13:24:37 -07002522 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002523 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002524 opendata->f_attr.mdsthreshold = NULL;
2525 }
Andy Adamson82be4172012-05-23 05:02:35 -04002526
David Quigley14c43f72013-05-22 12:50:43 -04002527 nfs4_label_free(olabel);
2528
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002529 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002532err_free_label:
2533 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002534err_opendata_put:
2535 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002536err_put_state_owner:
2537 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return status;
2540}
2541
2542
Andy Adamson82be4172012-05-23 05:02:35 -04002543static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002544 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002545 int flags,
2546 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002547 struct nfs4_label *label,
2548 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002550 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 struct nfs4_exception exception = { };
2552 struct nfs4_state *res;
2553 int status;
2554
2555 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002556 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002557 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002558 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (status == 0)
2560 break;
2561 /* NOTE: BAD_SEQID means the server and client disagree about the
2562 * book-keeping w.r.t. state-changing operations
2563 * (OPEN/CLOSE/LOCK/LOCKU...)
2564 * It is actually a sign of a bug on the client or on the server.
2565 *
2566 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002567 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * have unhashed the old state_owner for us, and that we can
2569 * therefore safely retry using a new one. We should still warn
2570 * the user though...
2571 */
2572 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002573 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002574 " returned a bad sequence-id error!\n",
2575 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 exception.retry = 1;
2577 continue;
2578 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002579 /*
2580 * BAD_STATEID on OPEN means that the server cancelled our
2581 * state before it received the OPEN_CONFIRM.
2582 * Recover by retrying the request as per the discussion
2583 * on Page 181 of RFC3530.
2584 */
2585 if (status == -NFS4ERR_BAD_STATEID) {
2586 exception.retry = 1;
2587 continue;
2588 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002589 if (status == -EAGAIN) {
2590 /* We must have found a delegation */
2591 exception.retry = 1;
2592 continue;
2593 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002594 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2595 continue;
2596 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 status, &exception));
2598 } while (exception.retry);
2599 return res;
2600}
2601
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002602static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2603 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002604 struct nfs4_state *state, struct nfs4_label *ilabel,
2605 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002607 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002609 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .iap = sattr,
2611 .server = server,
2612 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002613 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 };
2615 struct nfs_setattrres res = {
2616 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002617 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 .server = server,
2619 };
2620 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2622 .rpc_argp = &arg,
2623 .rpc_resp = &res,
2624 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002626 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002627 fmode_t fmode;
2628 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002629 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
David Quigleyaa9c2662013-05-22 12:50:44 -04002631 arg.bitmask = nfs4_bitmask(server, ilabel);
2632 if (ilabel)
2633 arg.bitmask = nfs4_bitmask(server, olabel);
2634
Trond Myklebust0e574af2005-10-27 22:12:38 -04002635 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Trond Myklebustee3ae842013-04-29 10:35:36 -04002637 /* Servers should only apply open mode checks for file size changes */
2638 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2639 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2640
2641 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2642 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002643 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002644 struct nfs_lockowner lockowner = {
2645 .l_owner = current->files,
2646 .l_pid = current->tgid,
2647 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002648 if (!nfs4_valid_open_stateid(state))
2649 return -EBADF;
2650 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2651 &lockowner) == -EIO)
2652 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002653 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002654 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Bryan Schumaker7c513052011-03-24 17:12:24 +00002656 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002657 if (status == 0 && state != NULL)
2658 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002659 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002662static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2663 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002664 struct nfs4_state *state, struct nfs4_label *ilabel,
2665 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002667 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002668 struct nfs4_exception exception = {
2669 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002670 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int err;
2673 do {
David Quigley1775fd32013-05-22 12:50:42 -04002674 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002675 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002676 switch (err) {
2677 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002678 if (!(sattr->ia_valid & ATTR_SIZE)) {
2679 pr_warn_once("NFSv4: server %s is incorrectly "
2680 "applying open mode checks to "
2681 "a SETATTR that is not "
2682 "changing file size.\n",
2683 server->nfs_client->cl_hostname);
2684 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002685 if (state && !(state->state & FMODE_WRITE)) {
2686 err = -EBADF;
2687 if (sattr->ia_valid & ATTR_OPEN)
2688 err = -EACCES;
2689 goto out;
2690 }
2691 }
2692 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return err;
2696}
2697
Peng Tao500d7012015-09-22 11:35:22 +08002698static bool
2699nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2700{
2701 if (inode == NULL || !nfs_have_layout(inode))
2702 return false;
2703
2704 return pnfs_wait_on_layoutreturn(inode, task);
2705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707struct nfs4_closedata {
2708 struct inode *inode;
2709 struct nfs4_state *state;
2710 struct nfs_closeargs arg;
2711 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002712 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002713 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002714 bool roc;
2715 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716};
2717
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002718static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002719{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002720 struct nfs4_closedata *calldata = data;
2721 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002722 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002723
Fred Isamanf7e89172011-01-06 11:36:32 +00002724 if (calldata->roc)
2725 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002726 nfs4_put_open_state(calldata->state);
2727 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002728 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002729 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002730 kfree(calldata);
2731}
2732
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002733static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002735 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002738 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Chuck Levera3ca5652012-03-01 17:00:40 -05002740 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002741 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2742 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002743 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /* hmm. we are done with the inode, and in the process of freeing
2745 * the state_owner. we keep this around to process errors
2746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 switch (task->tk_status) {
2748 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002749 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002750 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002751 pnfs_roc_set_barrier(state->inode,
2752 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002753 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002754 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002755 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002757 case -NFS4ERR_OLD_STATEID:
2758 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002760 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002761 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002762 rpc_restart_call_prepare(task);
2763 goto out_release;
2764 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002765 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002768 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002769 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002770 goto out_release;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002773 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2774 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002775out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002776 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002777 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002778 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002781static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002783 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002784 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002785 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002786 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002787 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002788
Chuck Levera3ca5652012-03-01 17:00:40 -05002789 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002790 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002791 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002792
Trond Myklebust88069f72009-12-08 08:33:16 -05002793 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002794 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002795 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2796 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2797 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002798 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002799 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002800 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002801 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002802 if (state->n_rdonly == 0)
2803 call_close |= is_rdonly;
2804 else if (is_rdonly)
2805 calldata->arg.fmode |= FMODE_READ;
2806 if (state->n_wronly == 0)
2807 call_close |= is_wronly;
2808 else if (is_wronly)
2809 calldata->arg.fmode |= FMODE_WRITE;
2810 } else if (is_rdwr)
2811 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2812
2813 if (calldata->arg.fmode == 0)
2814 call_close |= is_rdwr;
2815
Trond Myklebust5d422302013-03-14 16:57:48 -04002816 if (!nfs4_valid_open_stateid(state))
2817 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002818 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002819
2820 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002821 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002822 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002823 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002824
Peng Tao500d7012015-09-22 11:35:22 +08002825 if (nfs4_wait_on_layoutreturn(inode, task)) {
2826 nfs_release_seqid(calldata->arg.seqid);
2827 goto out_wait;
2828 }
2829
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002830 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002831 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002832 if (calldata->roc)
2833 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2834
Trond Myklebust6ae37332015-01-30 14:21:14 -05002835 calldata->arg.share_access =
2836 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2837 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002838
Trond Myklebust516a6af2005-10-27 22:12:41 -04002839 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002840 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002841 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002842 &calldata->arg.seq_args,
2843 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002844 task) != 0)
2845 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002846 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002847 return;
2848out_no_action:
2849 task->tk_action = NULL;
2850out_wait:
2851 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002854static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002855 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002856 .rpc_call_done = nfs4_close_done,
2857 .rpc_release = nfs4_free_closedata,
2858};
2859
Peng Taofe08c542014-07-03 13:05:01 +08002860static bool nfs4_roc(struct inode *inode)
2861{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002862 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002863 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002864 return pnfs_roc(inode);
2865}
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867/*
2868 * It is possible for data to be read/written from a mem-mapped file
2869 * after the sys_close call (which hits the vfs layer as a flush).
2870 * This means that we can't safely call nfsv4 close on a file until
2871 * the inode is cleared. This in turn means that we are not good
2872 * NFSv4 citizens - we do not indicate to the server to update the file's
2873 * share state even when we are done with one of the three share
2874 * stateid's in the inode.
2875 *
2876 * NOTE: Caller must be holding the sp->so_owner semaphore!
2877 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002878int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002880 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002881 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002883 struct nfs4_state_owner *sp = state->owner;
2884 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002885 struct rpc_message msg = {
2886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2887 .rpc_cred = state->owner->so_cred,
2888 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002889 struct rpc_task_setup task_setup_data = {
2890 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002891 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002892 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002893 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002894 .flags = RPC_TASK_ASYNC,
2895 };
Trond Myklebust95121352005-10-18 14:20:12 -07002896 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002898 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2899 &task_setup_data.rpc_client, &msg);
2900
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002901 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002903 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002904 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002905 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002907 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002909 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2910 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002911 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002912 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002913 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002914 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002915 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002916 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002917 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002918 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002919 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002920
Trond Myklebust1174dd12010-12-21 10:52:24 -05002921 msg.rpc_argp = &calldata->arg;
2922 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002923 task_setup_data.callback_data = calldata;
2924 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002925 if (IS_ERR(task))
2926 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002927 status = 0;
2928 if (wait)
2929 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002930 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002931 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002932out_free_calldata:
2933 kfree(calldata);
2934out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002935 nfs4_put_open_state(state);
2936 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Trond Myklebust2b484292010-09-17 10:56:51 -04002940static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002941nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2942 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002945 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2946
2947 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Trond Myklebust565277f2007-10-15 18:17:53 -04002949 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002950 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002951
2952 nfs4_label_release_security(label);
2953
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002954 if (IS_ERR(state))
2955 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002956 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Trond Myklebust1185a552009-12-03 15:54:02 -05002959static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002960{
2961 if (ctx->state == NULL)
2962 return;
2963 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002964 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002965 else
Al Viro643168c2011-06-22 18:20:23 -04002966 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Trond Myklebustb944dba2013-11-04 15:20:20 -05002969#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2970#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04002971#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2974{
Kinglong Mee8c612822015-08-26 21:12:58 +08002975 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04002976 struct nfs4_server_caps_arg args = {
2977 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08002978 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04002979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct nfs4_server_caps_res res = {};
2981 struct rpc_message msg = {
2982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002983 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 .rpc_resp = &res,
2985 };
2986 int status;
2987
Kinglong Mee8c612822015-08-26 21:12:58 +08002988 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
2989 FATTR4_WORD0_FH_EXPIRE_TYPE |
2990 FATTR4_WORD0_LINK_SUPPORT |
2991 FATTR4_WORD0_SYMLINK_SUPPORT |
2992 FATTR4_WORD0_ACLSUPPORT;
2993 if (minorversion)
2994 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
2995
Bryan Schumaker7c513052011-03-24 17:12:24 +00002996 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002998 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08002999 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003000 case 0:
3001 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3002 res.attr_bitmask[2] = 0;
3003 break;
3004 case 1:
3005 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3006 break;
3007 case 2:
3008 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003011 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3012 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3013 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3014 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003015 NFS_CAP_CTIME|NFS_CAP_MTIME|
3016 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003017 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3018 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 server->caps |= NFS_CAP_ACLS;
3020 if (res.has_links != 0)
3021 server->caps |= NFS_CAP_HARDLINKS;
3022 if (res.has_symlinks != 0)
3023 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003024 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3025 server->caps |= NFS_CAP_FILEID;
3026 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3027 server->caps |= NFS_CAP_MODE;
3028 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3029 server->caps |= NFS_CAP_NLINK;
3030 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3031 server->caps |= NFS_CAP_OWNER;
3032 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3033 server->caps |= NFS_CAP_OWNER_GROUP;
3034 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3035 server->caps |= NFS_CAP_ATIME;
3036 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3037 server->caps |= NFS_CAP_CTIME;
3038 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3039 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003040#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3041 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3042 server->caps |= NFS_CAP_SECURITY_LABEL;
3043#endif
3044 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3045 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003046 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003047
Trond Myklebusta65318b2009-03-11 14:10:28 -04003048 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3049 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3050 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003051 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003052 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3053 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003055 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return status;
3059}
3060
Trond Myklebust55a97592006-06-09 09:34:19 -04003061int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 struct nfs4_exception exception = { };
3064 int err;
3065 do {
3066 err = nfs4_handle_exception(server,
3067 _nfs4_server_capabilities(server, fhandle),
3068 &exception);
3069 } while (exception.retry);
3070 return err;
3071}
3072
3073static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3074 struct nfs_fsinfo *info)
3075{
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003078 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 };
3080 struct nfs4_lookup_res res = {
3081 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003082 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 .fh = fhandle,
3084 };
3085 struct rpc_message msg = {
3086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3087 .rpc_argp = &args,
3088 .rpc_resp = &res,
3089 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003090
David Quigleyaa9c2662013-05-22 12:50:44 -04003091 bitmask[0] = nfs4_fattr_bitmap[0];
3092 bitmask[1] = nfs4_fattr_bitmap[1];
3093 /*
3094 * Process the label in the upcoming getfattr
3095 */
3096 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3097
Trond Myklebust0e574af2005-10-27 22:12:38 -04003098 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
3102static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3103 struct nfs_fsinfo *info)
3104{
3105 struct nfs4_exception exception = { };
3106 int err;
3107 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003108 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003109 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003110 switch (err) {
3111 case 0:
3112 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003113 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003114 default:
3115 err = nfs4_handle_exception(server, err, &exception);
3116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003118out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return err;
3120}
3121
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003122static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3123 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3124{
Trond Myklebustc2190662013-08-26 19:23:04 -04003125 struct rpc_auth_create_args auth_args = {
3126 .pseudoflavor = flavor,
3127 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003128 struct rpc_auth *auth;
3129 int ret;
3130
Trond Myklebustc2190662013-08-26 19:23:04 -04003131 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003132 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003133 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003134 goto out;
3135 }
3136 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003137out:
3138 return ret;
3139}
3140
Chuck Lever9a744ba2013-03-16 15:56:02 -04003141/*
3142 * Retry pseudoroot lookup with various security flavors. We do this when:
3143 *
3144 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3145 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3146 *
3147 * Returns zero on success, or a negative NFS4ERR value, or a
3148 * negative errno value.
3149 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003150static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003151 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003153 /* Per 3530bis 15.33.5 */
3154 static const rpc_authflavor_t flav_array[] = {
3155 RPC_AUTH_GSS_KRB5P,
3156 RPC_AUTH_GSS_KRB5I,
3157 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003158 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003159 RPC_AUTH_NULL,
3160 };
3161 int status = -EPERM;
3162 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003164 if (server->auth_info.flavor_len > 0) {
3165 /* try each flavor specified by user */
3166 for (i = 0; i < server->auth_info.flavor_len; i++) {
3167 status = nfs4_lookup_root_sec(server, fhandle, info,
3168 server->auth_info.flavors[i]);
3169 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3170 continue;
3171 break;
3172 }
3173 } else {
3174 /* no flavors specified by user, try default list */
3175 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3176 status = nfs4_lookup_root_sec(server, fhandle, info,
3177 flav_array[i]);
3178 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3179 continue;
3180 break;
3181 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003182 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003183
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003184 /*
3185 * -EACCESS could mean that the user doesn't have correct permissions
3186 * to access the mount. It could also mean that we tried to mount
3187 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3188 * existing mount programs don't handle -EACCES very well so it should
3189 * be mapped to -EPERM instead.
3190 */
3191 if (status == -EACCES)
3192 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003193 return status;
3194}
3195
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003196static int nfs4_do_find_root_sec(struct nfs_server *server,
3197 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3198{
3199 int mv = server->nfs_client->cl_minorversion;
3200 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3201}
3202
3203/**
3204 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3205 * @server: initialized nfs_server handle
3206 * @fhandle: we fill in the pseudo-fs root file handle
3207 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003208 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003209 *
3210 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003211 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003212int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003213 struct nfs_fsinfo *info,
3214 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003215{
Andre Przywarac7757072015-04-23 17:17:40 +01003216 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003217
Andre Przywarac7757072015-04-23 17:17:40 +01003218 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003219 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003220
3221 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003222 status = nfs4_do_find_root_sec(server, fhandle, info);
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (status == 0)
3225 status = nfs4_server_capabilities(server, fhandle);
3226 if (status == 0)
3227 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003228
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003229 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Bryan Schumakerbae36242012-05-10 15:07:31 -04003232static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3233 struct nfs_fsinfo *info)
3234{
3235 int error;
3236 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003237 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003238
3239 error = nfs4_server_capabilities(server, mntfh);
3240 if (error < 0) {
3241 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3242 return error;
3243 }
3244
David Quigley14c43f72013-05-22 12:50:43 -04003245 label = nfs4_label_alloc(server, GFP_KERNEL);
3246 if (IS_ERR(label))
3247 return PTR_ERR(label);
3248
David Quigley1775fd32013-05-22 12:50:42 -04003249 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003250 if (error < 0) {
3251 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003252 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003253 }
3254
3255 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3256 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3257 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3258
David Quigley14c43f72013-05-22 12:50:43 -04003259err_free_label:
3260 nfs4_label_free(label);
3261
Bryan Schumakerbae36242012-05-10 15:07:31 -04003262 return error;
3263}
3264
Manoj Naik6b97fd32006-06-09 09:34:29 -04003265/*
3266 * Get locations and (maybe) other attributes of a referral.
3267 * Note that we'll actually follow the referral later when
3268 * we detect fsid mismatch in inode revalidation
3269 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003270static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3271 const struct qstr *name, struct nfs_fattr *fattr,
3272 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003273{
3274 int status = -ENOMEM;
3275 struct page *page = NULL;
3276 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003277
3278 page = alloc_page(GFP_KERNEL);
3279 if (page == NULL)
3280 goto out;
3281 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3282 if (locations == NULL)
3283 goto out;
3284
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003285 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003286 if (status != 0)
3287 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003288
3289 /*
3290 * If the fsid didn't change, this is a migration event, not a
3291 * referral. Cause us to drop into the exception handler, which
3292 * will kick off migration recovery.
3293 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003294 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003295 dprintk("%s: server did not return a different fsid for"
3296 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003297 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003298 goto out;
3299 }
Andy Adamson533eb462011-06-13 18:25:56 -04003300 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3301 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003302
Andy Adamson533eb462011-06-13 18:25:56 -04003303 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003304 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003305 memset(fhandle, 0, sizeof(struct nfs_fh));
3306out:
3307 if (page)
3308 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003309 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003310 return status;
3311}
3312
David Quigley1775fd32013-05-22 12:50:42 -04003313static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3314 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 struct nfs4_getattr_arg args = {
3317 .fh = fhandle,
3318 .bitmask = server->attr_bitmask,
3319 };
3320 struct nfs4_getattr_res res = {
3321 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003322 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 .server = server,
3324 };
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3327 .rpc_argp = &args,
3328 .rpc_resp = &res,
3329 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003330
3331 args.bitmask = nfs4_bitmask(server, label);
3332
Trond Myklebust0e574af2005-10-27 22:12:38 -04003333 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
David Quigley1775fd32013-05-22 12:50:42 -04003337static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 struct nfs4_exception exception = { };
3341 int err;
3342 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003343 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3344 trace_nfs4_getattr(server, fhandle, fattr, err);
3345 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 &exception);
3347 } while (exception.retry);
3348 return err;
3349}
3350
3351/*
3352 * The file is not closed if it is opened due to the a request to change
3353 * the size of the file. The open call will not be needed once the
3354 * VFS layer lookup-intents are implemented.
3355 *
3356 * Close is called when the inode is destroyed.
3357 * If we haven't opened the file for O_WRONLY, we
3358 * need to in the size_change case to obtain a stateid.
3359 *
3360 * Got race?
3361 * Because OPEN is always done by name in nfsv4, it is
3362 * possible that we opened a different file by the same
3363 * name. We can recognize this race condition, but we
3364 * can't do anything about it besides returning an error.
3365 *
3366 * This will be fixed with VFS changes (lookup-intent).
3367 */
3368static int
3369nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3370 struct iattr *sattr)
3371{
David Howells2b0143b2015-03-17 22:25:59 +00003372 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003373 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003374 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003375 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 int status;
3377
Peng Tao88ac8152014-09-12 11:04:10 +08003378 if (pnfs_ld_layoutret_on_setattr(inode) &&
3379 sattr->ia_valid & ATTR_SIZE &&
3380 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003381 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003382
Trond Myklebust0e574af2005-10-27 22:12:38 -04003383 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Andy Adamson26699402012-05-30 16:12:24 -04003385 /* Deal with open(O_TRUNC) */
3386 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003387 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003388
3389 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003390 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003391 return 0;
3392
Trond Myklebustd5308382005-11-04 15:33:38 -05003393 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003394 if (sattr->ia_valid & ATTR_FILE) {
3395 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003396
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003397 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003398 if (ctx) {
3399 cred = ctx->cred;
3400 state = ctx->state;
3401 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003402 }
3403
David Quigley14c43f72013-05-22 12:50:43 -04003404 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3405 if (IS_ERR(label))
3406 return PTR_ERR(label);
3407
3408 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003409 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003410 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003411 nfs_setsecurity(inode, fattr, label);
3412 }
David Quigley14c43f72013-05-22 12:50:43 -04003413 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return status;
3415}
3416
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003417static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3418 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003419 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003420{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003421 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003422 int status;
3423 struct nfs4_lookup_arg args = {
3424 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003425 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003426 .name = name,
3427 };
3428 struct nfs4_lookup_res res = {
3429 .server = server,
3430 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003431 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003432 .fh = fhandle,
3433 };
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3436 .rpc_argp = &args,
3437 .rpc_resp = &res,
3438 };
3439
David Quigleyaa9c2662013-05-22 12:50:44 -04003440 args.bitmask = nfs4_bitmask(server, label);
3441
David Howells2b3de442006-08-22 20:06:09 -04003442 nfs_fattr_init(fattr);
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003445 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 dprintk("NFS reply lookup: %d\n", status);
3447 return status;
3448}
3449
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003450static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003451{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003452 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003453 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003454 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3455 fattr->nlink = 2;
3456}
3457
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003458static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3459 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003460 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003461{
3462 struct nfs4_exception exception = { };
3463 struct rpc_clnt *client = *clnt;
3464 int err;
3465 do {
David Quigley1775fd32013-05-22 12:50:42 -04003466 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003467 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003468 switch (err) {
3469 case -NFS4ERR_BADNAME:
3470 err = -ENOENT;
3471 goto out;
3472 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003473 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003474 if (err == -NFS4ERR_MOVED)
3475 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003476 goto out;
3477 case -NFS4ERR_WRONGSEC:
3478 err = -EPERM;
3479 if (client != *clnt)
3480 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003481 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003482 if (IS_ERR(client))
3483 return PTR_ERR(client);
3484
3485 exception.retry = 1;
3486 break;
3487 default:
3488 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3489 }
3490 } while (exception.retry);
3491
3492out:
3493 if (err == 0)
3494 *clnt = client;
3495 else if (client != *clnt)
3496 rpc_shutdown_client(client);
3497
3498 return err;
3499}
3500
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003501static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003502 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3503 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003505 int status;
3506 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003507
David Quigley1775fd32013-05-22 12:50:42 -04003508 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003509 if (client != NFS_CLIENT(dir)) {
3510 rpc_shutdown_client(client);
3511 nfs_fixup_secinfo_attributes(fattr);
3512 }
3513 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003516struct rpc_clnt *
3517nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3518 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3519{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003520 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003521 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003522
David Quigley1775fd32013-05-22 12:50:42 -04003523 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003524 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003525 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003526 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003527}
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3530{
Trond Myklebust76b32992007-08-10 17:45:11 -04003531 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct nfs4_accessargs args = {
3533 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003534 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003536 struct nfs4_accessres res = {
3537 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003538 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3541 .rpc_argp = &args,
3542 .rpc_resp = &res,
3543 .rpc_cred = entry->cred,
3544 };
3545 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003546 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 /*
3549 * Determine which access bits we want to ask for...
3550 */
3551 if (mode & MAY_READ)
3552 args.access |= NFS4_ACCESS_READ;
3553 if (S_ISDIR(inode->i_mode)) {
3554 if (mode & MAY_WRITE)
3555 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3556 if (mode & MAY_EXEC)
3557 args.access |= NFS4_ACCESS_LOOKUP;
3558 } else {
3559 if (mode & MAY_WRITE)
3560 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3561 if (mode & MAY_EXEC)
3562 args.access |= NFS4_ACCESS_EXECUTE;
3563 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003564
3565 res.fattr = nfs_alloc_fattr();
3566 if (res.fattr == NULL)
3567 return -ENOMEM;
3568
Bryan Schumaker7c513052011-03-24 17:12:24 +00003569 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003571 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003572 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003574 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return status;
3576}
3577
3578static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3579{
3580 struct nfs4_exception exception = { };
3581 int err;
3582 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003583 err = _nfs4_proc_access(inode, entry);
3584 trace_nfs4_access(inode, err);
3585 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 &exception);
3587 } while (exception.retry);
3588 return err;
3589}
3590
3591/*
3592 * TODO: For the time being, we don't try to get any attributes
3593 * along with any of the zero-copy operations READ, READDIR,
3594 * READLINK, WRITE.
3595 *
3596 * In the case of the first three, we want to put the GETATTR
3597 * after the read-type operation -- this is because it is hard
3598 * to predict the length of a GETATTR response in v4, and thus
3599 * align the READ data correctly. This means that the GETATTR
3600 * may end up partially falling into the page cache, and we should
3601 * shift it into the 'tail' of the xdr_buf before processing.
3602 * To do this efficiently, we need to know the total length
3603 * of data received, which doesn't seem to be available outside
3604 * of the RPC layer.
3605 *
3606 * In the case of WRITE, we also want to put the GETATTR after
3607 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003608 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 *
3610 * Both of these changes to the XDR layer would in fact be quite
3611 * minor, but I decided to leave them for a subsequent patch.
3612 */
3613static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3614 unsigned int pgbase, unsigned int pglen)
3615{
3616 struct nfs4_readlink args = {
3617 .fh = NFS_FH(inode),
3618 .pgbase = pgbase,
3619 .pglen = pglen,
3620 .pages = &page,
3621 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003622 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3625 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003626 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 };
3628
Bryan Schumaker7c513052011-03-24 17:12:24 +00003629 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 -07003630}
3631
3632static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3633 unsigned int pgbase, unsigned int pglen)
3634{
3635 struct nfs4_exception exception = { };
3636 int err;
3637 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003638 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3639 trace_nfs4_readlink(inode, err);
3640 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 &exception);
3642 } while (exception.retry);
3643 return err;
3644}
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003647 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649static int
3650nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003651 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
David Quigleyaa9c2662013-05-22 12:50:44 -04003653 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003654 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 int status = 0;
3657
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003658 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3659 if (IS_ERR(ctx))
3660 return PTR_ERR(ctx);
3661
David Quigleyaa9c2662013-05-22 12:50:44 -04003662 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3663
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003664 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003665 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 if (IS_ERR(state)) {
3667 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003671 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003672 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return status;
3674}
3675
3676static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3677{
Trond Myklebust16e42952005-10-27 22:12:44 -04003678 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003679 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003681 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003683 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003684 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003685 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3688 .rpc_argp = &args,
3689 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003691 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003692
Bryan Schumaker7c513052011-03-24 17:12:24 +00003693 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003694 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003695 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return status;
3697}
3698
3699static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3700{
3701 struct nfs4_exception exception = { };
3702 int err;
3703 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003704 err = _nfs4_proc_remove(dir, name);
3705 trace_nfs4_remove(dir, name, err);
3706 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 &exception);
3708 } while (exception.retry);
3709 return err;
3710}
3711
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003712static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003714 struct nfs_server *server = NFS_SERVER(dir);
3715 struct nfs_removeargs *args = msg->rpc_argp;
3716 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003718 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003720 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003721
3722 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003725static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3726{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003727 nfs4_setup_sequence(NFS_SERVER(data->dir),
3728 &data->args.seq_args,
3729 &data->res.seq_res,
3730 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003733static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003735 struct nfs_unlinkdata *data = task->tk_calldata;
3736 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003737
Trond Myklebust14516c32010-07-31 14:29:06 -04003738 if (!nfs4_sequence_done(task, &res->seq_res))
3739 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003740 if (nfs4_async_handle_error(task, res->server, NULL,
3741 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003742 return 0;
3743 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003744 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Jeff Laytond3d41522010-09-17 17:31:57 -04003747static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3748{
3749 struct nfs_server *server = NFS_SERVER(dir);
3750 struct nfs_renameargs *arg = msg->rpc_argp;
3751 struct nfs_renameres *res = msg->rpc_resp;
3752
3753 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003754 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003755 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003756}
3757
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003758static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3759{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003760 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3761 &data->args.seq_args,
3762 &data->res.seq_res,
3763 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003764}
3765
3766static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3767 struct inode *new_dir)
3768{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003769 struct nfs_renamedata *data = task->tk_calldata;
3770 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003771
3772 if (!nfs4_sequence_done(task, &res->seq_res))
3773 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003774 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003775 return 0;
3776
3777 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003778 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003779 return 1;
3780}
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3783{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003784 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 struct nfs4_link_arg arg = {
3786 .fh = NFS_FH(inode),
3787 .dir_fh = NFS_FH(dir),
3788 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003789 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003791 struct nfs4_link_res res = {
3792 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003793 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003794 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3797 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003798 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003800 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Trond Myklebust136f2622010-04-16 16:22:49 -04003802 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003803 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003804 goto out;
3805
David Quigley14c43f72013-05-22 12:50:43 -04003806 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3807 if (IS_ERR(res.label)) {
3808 status = PTR_ERR(res.label);
3809 goto out;
3810 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003811 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003812
Bryan Schumaker7c513052011-03-24 17:12:24 +00003813 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003814 if (!status) {
3815 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003816 status = nfs_post_op_update_inode(inode, res.fattr);
3817 if (!status)
3818 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003819 }
David Quigley14c43f72013-05-22 12:50:43 -04003820
3821
3822 nfs4_label_free(res.label);
3823
Trond Myklebust136f2622010-04-16 16:22:49 -04003824out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003825 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return status;
3827}
3828
3829static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3830{
3831 struct nfs4_exception exception = { };
3832 int err;
3833 do {
3834 err = nfs4_handle_exception(NFS_SERVER(inode),
3835 _nfs4_proc_link(inode, dir, name),
3836 &exception);
3837 } while (exception.retry);
3838 return err;
3839}
3840
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003841struct nfs4_createdata {
3842 struct rpc_message msg;
3843 struct nfs4_create_arg arg;
3844 struct nfs4_create_res res;
3845 struct nfs_fh fh;
3846 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003847 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003848};
3849
3850static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3851 struct qstr *name, struct iattr *sattr, u32 ftype)
3852{
3853 struct nfs4_createdata *data;
3854
3855 data = kzalloc(sizeof(*data), GFP_KERNEL);
3856 if (data != NULL) {
3857 struct nfs_server *server = NFS_SERVER(dir);
3858
David Quigley14c43f72013-05-22 12:50:43 -04003859 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3860 if (IS_ERR(data->label))
3861 goto out_free;
3862
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003863 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3864 data->msg.rpc_argp = &data->arg;
3865 data->msg.rpc_resp = &data->res;
3866 data->arg.dir_fh = NFS_FH(dir);
3867 data->arg.server = server;
3868 data->arg.name = name;
3869 data->arg.attrs = sattr;
3870 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003871 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003872 data->res.server = server;
3873 data->res.fh = &data->fh;
3874 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003875 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003876 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003877 }
3878 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003879out_free:
3880 kfree(data);
3881 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003882}
3883
3884static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3885{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003886 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003887 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003888 if (status == 0) {
3889 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003890 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003891 }
3892 return status;
3893}
3894
3895static void nfs4_free_createdata(struct nfs4_createdata *data)
3896{
David Quigley14c43f72013-05-22 12:50:43 -04003897 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003898 kfree(data);
3899}
3900
Chuck Lever4f390c12006-08-22 20:06:22 -04003901static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003902 struct page *page, unsigned int len, struct iattr *sattr,
3903 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003905 struct nfs4_createdata *data;
3906 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Chuck Lever94a6d752006-08-22 20:06:23 -04003908 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003909 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003910
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003911 status = -ENOMEM;
3912 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3913 if (data == NULL)
3914 goto out;
3915
3916 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3917 data->arg.u.symlink.pages = &page;
3918 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003919 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003921 status = nfs4_do_create(dir, dentry, data);
3922
3923 nfs4_free_createdata(data);
3924out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return status;
3926}
3927
Chuck Lever4f390c12006-08-22 20:06:22 -04003928static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003929 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
3931 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003932 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003934
3935 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003938 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3939 trace_nfs4_symlink(dir, &dentry->d_name, err);
3940 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 &exception);
3942 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003943
3944 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return err;
3946}
3947
3948static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003949 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003951 struct nfs4_createdata *data;
3952 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003954 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3955 if (data == NULL)
3956 goto out;
3957
David Quigley1775fd32013-05-22 12:50:42 -04003958 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003959 status = nfs4_do_create(dir, dentry, data);
3960
3961 nfs4_free_createdata(data);
3962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return status;
3964}
3965
3966static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3967 struct iattr *sattr)
3968{
3969 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003970 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003972
David Quigleyaa9c2662013-05-22 12:50:44 -04003973 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3974
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003975 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003977 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3978 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3979 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 &exception);
3981 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003982 nfs4_label_release_security(label);
3983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return err;
3985}
3986
3987static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003988 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
David Howells2b0143b2015-03-17 22:25:59 +00003990 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 struct nfs4_readdir_arg args = {
3992 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003993 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 .pgbase = 0,
3995 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00003996 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003997 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 };
3999 struct nfs4_readdir_res res;
4000 struct rpc_message msg = {
4001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4002 .rpc_argp = &args,
4003 .rpc_resp = &res,
4004 .rpc_cred = cred,
4005 };
4006 int status;
4007
Al Viro6de14722013-09-16 10:53:17 -04004008 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4009 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004010 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004011 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004013 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 -05004014 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004015 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004016 status += args.pgbase;
4017 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004018
4019 nfs_invalidate_atime(dir);
4020
Harvey Harrison3110ff82008-05-02 13:42:44 -07004021 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return status;
4023}
4024
4025static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004026 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
4028 struct nfs4_exception exception = { };
4029 int err;
4030 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004031 err = _nfs4_proc_readdir(dentry, cred, cookie,
4032 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004033 trace_nfs4_readdir(d_inode(dentry), err);
4034 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 &exception);
4036 } while (exception.retry);
4037 return err;
4038}
4039
4040static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004041 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004043 struct nfs4_createdata *data;
4044 int mode = sattr->ia_mode;
4045 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004047 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4048 if (data == NULL)
4049 goto out;
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004052 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004054 data->arg.ftype = NF4BLK;
4055 data->arg.u.device.specdata1 = MAJOR(rdev);
4056 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
4058 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004059 data->arg.ftype = NF4CHR;
4060 data->arg.u.device.specdata1 = MAJOR(rdev);
4061 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004062 } else if (!S_ISSOCK(mode)) {
4063 status = -EINVAL;
4064 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
David Quigley1775fd32013-05-22 12:50:42 -04004066
David Quigleyaa9c2662013-05-22 12:50:44 -04004067 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004068 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004069out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004070 nfs4_free_createdata(data);
4071out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return status;
4073}
4074
4075static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4076 struct iattr *sattr, dev_t rdev)
4077{
4078 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004079 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004081
David Quigleyaa9c2662013-05-22 12:50:44 -04004082 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4083
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004084 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004086 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4087 trace_nfs4_mknod(dir, &dentry->d_name, err);
4088 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 &exception);
4090 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004091
4092 nfs4_label_release_security(label);
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 return err;
4095}
4096
4097static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4098 struct nfs_fsstat *fsstat)
4099{
4100 struct nfs4_statfs_arg args = {
4101 .fh = fhandle,
4102 .bitmask = server->attr_bitmask,
4103 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004104 struct nfs4_statfs_res res = {
4105 .fsstat = fsstat,
4106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 struct rpc_message msg = {
4108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4109 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004110 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 };
4112
Trond Myklebust0e574af2005-10-27 22:12:38 -04004113 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004114 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
4117static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4118{
4119 struct nfs4_exception exception = { };
4120 int err;
4121 do {
4122 err = nfs4_handle_exception(server,
4123 _nfs4_proc_statfs(server, fhandle, fsstat),
4124 &exception);
4125 } while (exception.retry);
4126 return err;
4127}
4128
4129static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4130 struct nfs_fsinfo *fsinfo)
4131{
4132 struct nfs4_fsinfo_arg args = {
4133 .fh = fhandle,
4134 .bitmask = server->attr_bitmask,
4135 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004136 struct nfs4_fsinfo_res res = {
4137 .fsinfo = fsinfo,
4138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4141 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004142 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 };
4144
Bryan Schumaker7c513052011-03-24 17:12:24 +00004145 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
4148static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4149{
4150 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004151 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 int err;
4153
4154 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004155 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004156 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004157 if (err == 0) {
4158 struct nfs_client *clp = server->nfs_client;
4159
4160 spin_lock(&clp->cl_lock);
4161 clp->cl_lease_time = fsinfo->lease_time * HZ;
4162 clp->cl_last_renewal = now;
4163 spin_unlock(&clp->cl_lock);
4164 break;
4165 }
4166 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 } while (exception.retry);
4168 return err;
4169}
4170
4171static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4172{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004173 int error;
4174
Trond Myklebust0e574af2005-10-27 22:12:38 -04004175 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004176 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004177 if (error == 0) {
4178 /* block layout checks this! */
4179 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004180 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004181 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004182
4183 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
4186static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4187 struct nfs_pathconf *pathconf)
4188{
4189 struct nfs4_pathconf_arg args = {
4190 .fh = fhandle,
4191 .bitmask = server->attr_bitmask,
4192 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004193 struct nfs4_pathconf_res res = {
4194 .pathconf = pathconf,
4195 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct rpc_message msg = {
4197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4198 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004199 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 };
4201
4202 /* None of the pathconf attributes are mandatory to implement */
4203 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4204 memset(pathconf, 0, sizeof(*pathconf));
4205 return 0;
4206 }
4207
Trond Myklebust0e574af2005-10-27 22:12:38 -04004208 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004209 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
4212static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4213 struct nfs_pathconf *pathconf)
4214{
4215 struct nfs4_exception exception = { };
4216 int err;
4217
4218 do {
4219 err = nfs4_handle_exception(server,
4220 _nfs4_proc_pathconf(server, fhandle, pathconf),
4221 &exception);
4222 } while (exception.retry);
4223 return err;
4224}
4225
Trond Myklebust5521abf2013-03-16 20:54:34 -04004226int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004227 const struct nfs_open_context *ctx,
4228 const struct nfs_lock_context *l_ctx,
4229 fmode_t fmode)
4230{
4231 const struct nfs_lockowner *lockowner = NULL;
4232
4233 if (l_ctx != NULL)
4234 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004235 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004236}
4237EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4238
Trond Myklebust5521abf2013-03-16 20:54:34 -04004239static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4240 const struct nfs_open_context *ctx,
4241 const struct nfs_lock_context *l_ctx,
4242 fmode_t fmode)
4243{
4244 nfs4_stateid current_stateid;
4245
Trond Myklebuste1253be2014-03-05 08:44:23 -05004246 /* If the current stateid represents a lost lock, then exit */
4247 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4248 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004249 return nfs4_stateid_match(stateid, &current_stateid);
4250}
4251
4252static bool nfs4_error_stateid_expired(int err)
4253{
4254 switch (err) {
4255 case -NFS4ERR_DELEG_REVOKED:
4256 case -NFS4ERR_ADMIN_REVOKED:
4257 case -NFS4ERR_BAD_STATEID:
4258 case -NFS4ERR_STALE_STATEID:
4259 case -NFS4ERR_OLD_STATEID:
4260 case -NFS4ERR_OPENMODE:
4261 case -NFS4ERR_EXPIRED:
4262 return true;
4263 }
4264 return false;
4265}
4266
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004267void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004268{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004269 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004270}
4271
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004272static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004274 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004276 trace_nfs4_read(hdr, task->tk_status);
4277 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004278 hdr->args.context->state,
4279 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004280 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004281 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004283
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004284 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004286 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Trond Myklebust5521abf2013-03-16 20:54:34 -04004290static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004291 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004292{
4293
4294 if (!nfs4_error_stateid_expired(task->tk_status) ||
4295 nfs4_stateid_is_current(&args->stateid,
4296 args->context,
4297 args->lock_context,
4298 FMODE_READ))
4299 return false;
4300 rpc_restart_call_prepare(task);
4301 return true;
4302}
4303
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004304static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004305{
4306
4307 dprintk("--> %s\n", __func__);
4308
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004309 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004310 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004311 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004312 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004313 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4314 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004315}
4316
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004317static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4318 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004320 hdr->timestamp = jiffies;
4321 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004322 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004323 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004326static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4327 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004328{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004329 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4330 &hdr->args.seq_args,
4331 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004332 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004333 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004334 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4335 hdr->args.lock_context,
4336 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004337 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004338 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004339 return -EIO;
4340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004343static int nfs4_write_done_cb(struct rpc_task *task,
4344 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004346 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004347
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004348 trace_nfs4_write(hdr, task->tk_status);
4349 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004350 hdr->args.context->state,
4351 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004352 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004353 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004355 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004356 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004357 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004358 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Trond Myklebust5521abf2013-03-16 20:54:34 -04004362static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004363 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004364{
4365
4366 if (!nfs4_error_stateid_expired(task->tk_status) ||
4367 nfs4_stateid_is_current(&args->stateid,
4368 args->context,
4369 args->lock_context,
4370 FMODE_WRITE))
4371 return false;
4372 rpc_restart_call_prepare(task);
4373 return true;
4374}
4375
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004376static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004377{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004378 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004379 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004380 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004381 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004382 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4383 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004384}
4385
Trond Myklebust5a37f852012-04-28 14:55:16 -04004386static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004387bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004388{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004389 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004390 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004391 return false;
4392 /* Otherwise, request attributes if and only if we don't hold
4393 * a delegation
4394 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004395 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004396}
Fred Isamana69aef12011-03-03 15:13:47 +00004397
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004398static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4399 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004401 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004402
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004403 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4404 hdr->args.bitmask = NULL;
4405 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004406 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004407 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004408
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004409 if (!hdr->pgio_done_cb)
4410 hdr->pgio_done_cb = nfs4_write_done_cb;
4411 hdr->res.server = server;
4412 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004414 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004415 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
Fred Isaman0b7c0152012-04-20 14:47:39 -04004418static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4419{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004420 nfs4_setup_sequence(NFS_SERVER(data->inode),
4421 &data->args.seq_args,
4422 &data->res.seq_res,
4423 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
Fred Isaman0b7c0152012-04-20 14:47:39 -04004426static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004429
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004430 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004431 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4432 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004433 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004434 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437}
4438
Fred Isaman0b7c0152012-04-20 14:47:39 -04004439static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004440{
4441 if (!nfs4_sequence_done(task, &data->res.seq_res))
4442 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004443 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004444}
4445
Fred Isaman0b7c0152012-04-20 14:47:39 -04004446static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004448 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004449
Fred Isaman0b7c0152012-04-20 14:47:39 -04004450 if (data->commit_done_cb == NULL)
4451 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004452 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004453 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004454 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004457struct nfs4_renewdata {
4458 struct nfs_client *client;
4459 unsigned long timestamp;
4460};
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462/*
4463 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4464 * standalone procedure for queueing an asynchronous RENEW.
4465 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004466static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004467{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004468 struct nfs4_renewdata *data = calldata;
4469 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004470
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004471 if (atomic_read(&clp->cl_count) > 1)
4472 nfs4_schedule_state_renewal(clp);
4473 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004474 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004475}
4476
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004477static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004479 struct nfs4_renewdata *data = calldata;
4480 struct nfs_client *clp = data->client;
4481 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004483 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004484 switch (task->tk_status) {
4485 case 0:
4486 break;
4487 case -NFS4ERR_LEASE_MOVED:
4488 nfs4_schedule_lease_moved_recovery(clp);
4489 break;
4490 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004491 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004492 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4493 return;
4494 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004495 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004496 return;
4497 }
4498 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004500 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004503static const struct rpc_call_ops nfs4_renew_ops = {
4504 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004505 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004506};
4507
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004508static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
4510 struct rpc_message msg = {
4511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4512 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004513 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004515 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004517 if (renew_flags == 0)
4518 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004519 if (!atomic_inc_not_zero(&clp->cl_count))
4520 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004521 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004522 if (data == NULL)
4523 return -ENOMEM;
4524 data->client = clp;
4525 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004526 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004527 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004530static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4534 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004535 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 };
4537 unsigned long now = jiffies;
4538 int status;
4539
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004540 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 if (status < 0)
4542 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004543 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return 0;
4545}
4546
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004547static inline int nfs4_server_supports_acls(struct nfs_server *server)
4548{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004549 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004550}
4551
Trond Myklebust21f498c2012-08-24 10:59:25 -04004552/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4553 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004554 * the stack.
4555 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004556#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004557
Neil Hormane9e3d722011-03-04 19:26:03 -05004558static int buf_to_pages_noslab(const void *buf, size_t buflen,
4559 struct page **pages, unsigned int *pgbase)
4560{
4561 struct page *newpage, **spages;
4562 int rc = 0;
4563 size_t len;
4564 spages = pages;
4565
4566 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004567 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004568 newpage = alloc_page(GFP_KERNEL);
4569
4570 if (newpage == NULL)
4571 goto unwind;
4572 memcpy(page_address(newpage), buf, len);
4573 buf += len;
4574 buflen -= len;
4575 *pages++ = newpage;
4576 rc++;
4577 } while (buflen != 0);
4578
4579 return rc;
4580
4581unwind:
4582 for(; rc > 0; rc--)
4583 __free_page(spages[rc-1]);
4584 return -ENOMEM;
4585}
4586
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004587struct nfs4_cached_acl {
4588 int cached;
4589 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004590 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004591};
4592
4593static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004594{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004595 struct nfs_inode *nfsi = NFS_I(inode);
4596
4597 spin_lock(&inode->i_lock);
4598 kfree(nfsi->nfs4_acl);
4599 nfsi->nfs4_acl = acl;
4600 spin_unlock(&inode->i_lock);
4601}
4602
4603static void nfs4_zap_acl_attr(struct inode *inode)
4604{
4605 nfs4_set_cached_acl(inode, NULL);
4606}
4607
4608static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4609{
4610 struct nfs_inode *nfsi = NFS_I(inode);
4611 struct nfs4_cached_acl *acl;
4612 int ret = -ENOENT;
4613
4614 spin_lock(&inode->i_lock);
4615 acl = nfsi->nfs4_acl;
4616 if (acl == NULL)
4617 goto out;
4618 if (buf == NULL) /* user is just asking for length */
4619 goto out_len;
4620 if (acl->cached == 0)
4621 goto out;
4622 ret = -ERANGE; /* see getxattr(2) man page */
4623 if (acl->len > buflen)
4624 goto out;
4625 memcpy(buf, acl->data, acl->len);
4626out_len:
4627 ret = acl->len;
4628out:
4629 spin_unlock(&inode->i_lock);
4630 return ret;
4631}
4632
Sachin Prabhu5794d212012-04-17 14:36:40 +01004633static 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 +00004634{
4635 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004636 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004637
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004638 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004639 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004640 if (acl == NULL)
4641 goto out;
4642 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004643 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004644 } else {
4645 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4646 if (acl == NULL)
4647 goto out;
4648 acl->cached = 0;
4649 }
4650 acl->len = acl_len;
4651out:
4652 nfs4_set_cached_acl(inode, acl);
4653}
4654
Andy Adamsonbf118a32011-12-07 11:55:27 -05004655/*
4656 * The getxattr API returns the required buffer length when called with a
4657 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4658 * the required buf. On a NULL buf, we send a page of data to the server
4659 * guessing that the ACL request can be serviced by a page. If so, we cache
4660 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4661 * the cache. If not so, we throw away the page, and cache the required
4662 * length. The next getxattr call will then produce another round trip to
4663 * the server, this time with the input buf of the required size.
4664 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004665static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004666{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004667 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004668 struct nfs_getaclargs args = {
4669 .fh = NFS_FH(inode),
4670 .acl_pages = pages,
4671 .acl_len = buflen,
4672 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004673 struct nfs_getaclres res = {
4674 .acl_len = buflen,
4675 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004676 struct rpc_message msg = {
4677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4678 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004679 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004680 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004681 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4682 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004683
Andy Adamsonbf118a32011-12-07 11:55:27 -05004684 /* As long as we're doing a round trip to the server anyway,
4685 * let's be prepared for a page of acl data. */
4686 if (npages == 0)
4687 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004688 if (npages > ARRAY_SIZE(pages))
4689 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004690
Andy Adamsonbf118a32011-12-07 11:55:27 -05004691 for (i = 0; i < npages; i++) {
4692 pages[i] = alloc_page(GFP_KERNEL);
4693 if (!pages[i])
4694 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004695 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004696
4697 /* for decoding across pages */
4698 res.acl_scratch = alloc_page(GFP_KERNEL);
4699 if (!res.acl_scratch)
4700 goto out_free;
4701
Andy Adamsonbf118a32011-12-07 11:55:27 -05004702 args.acl_len = npages * PAGE_SIZE;
4703 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004704
Peng Taode040be2012-01-10 22:42:47 +08004705 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004706 __func__, buf, buflen, npages, args.acl_len);
4707 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4708 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004709 if (ret)
4710 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004711
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004712 /* Handle the case where the passed-in buffer is too short */
4713 if (res.acl_flags & NFS4_ACL_TRUNC) {
4714 /* Did the user only issue a request for the acl length? */
4715 if (buf == NULL)
4716 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004717 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004718 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004719 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004720 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004721 if (buf) {
4722 if (res.acl_len > buflen) {
4723 ret = -ERANGE;
4724 goto out_free;
4725 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004726 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004727 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004728out_ok:
4729 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004730out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004731 for (i = 0; i < npages; i++)
4732 if (pages[i])
4733 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004734 if (res.acl_scratch)
4735 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004736 return ret;
4737}
4738
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004739static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4740{
4741 struct nfs4_exception exception = { };
4742 ssize_t ret;
4743 do {
4744 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004745 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004746 if (ret >= 0)
4747 break;
4748 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4749 } while (exception.retry);
4750 return ret;
4751}
4752
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004753static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4754{
4755 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004756 int ret;
4757
4758 if (!nfs4_server_supports_acls(server))
4759 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004760 ret = nfs_revalidate_inode(server, inode);
4761 if (ret < 0)
4762 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004763 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4764 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004765 ret = nfs4_read_cached_acl(inode, buf, buflen);
4766 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004767 /* -ENOENT is returned if there is no ACL or if there is an ACL
4768 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004769 return ret;
4770 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004771}
4772
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004773static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004774{
4775 struct nfs_server *server = NFS_SERVER(inode);
4776 struct page *pages[NFS4ACL_MAXPAGES];
4777 struct nfs_setaclargs arg = {
4778 .fh = NFS_FH(inode),
4779 .acl_pages = pages,
4780 .acl_len = buflen,
4781 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004782 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004783 struct rpc_message msg = {
4784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4785 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004786 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004787 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004788 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004789 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004790
4791 if (!nfs4_server_supports_acls(server))
4792 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004793 if (npages > ARRAY_SIZE(pages))
4794 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004795 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4796 if (i < 0)
4797 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004798 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004799 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004800
4801 /*
4802 * Free each page after tx, so the only ref left is
4803 * held by the network stack
4804 */
4805 for (; i > 0; i--)
4806 put_page(pages[i-1]);
4807
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004808 /*
4809 * Acl update can result in inode attribute update.
4810 * so mark the attribute cache invalid.
4811 */
4812 spin_lock(&inode->i_lock);
4813 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4814 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004815 nfs_access_zap_cache(inode);
4816 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004817 return ret;
4818}
4819
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004820static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4821{
4822 struct nfs4_exception exception = { };
4823 int err;
4824 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004825 err = __nfs4_proc_set_acl(inode, buf, buflen);
4826 trace_nfs4_set_acl(inode, err);
4827 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004828 &exception);
4829 } while (exception.retry);
4830 return err;
4831}
4832
David Quigleyaa9c2662013-05-22 12:50:44 -04004833#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4834static int _nfs4_get_security_label(struct inode *inode, void *buf,
4835 size_t buflen)
4836{
4837 struct nfs_server *server = NFS_SERVER(inode);
4838 struct nfs_fattr fattr;
4839 struct nfs4_label label = {0, 0, buflen, buf};
4840
4841 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004842 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004843 .fh = NFS_FH(inode),
4844 .bitmask = bitmask,
4845 };
4846 struct nfs4_getattr_res res = {
4847 .fattr = &fattr,
4848 .label = &label,
4849 .server = server,
4850 };
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004853 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004854 .rpc_resp = &res,
4855 };
4856 int ret;
4857
4858 nfs_fattr_init(&fattr);
4859
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004860 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004861 if (ret)
4862 return ret;
4863 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4864 return -ENOENT;
4865 if (buflen < label.len)
4866 return -ERANGE;
4867 return 0;
4868}
4869
4870static int nfs4_get_security_label(struct inode *inode, void *buf,
4871 size_t buflen)
4872{
4873 struct nfs4_exception exception = { };
4874 int err;
4875
4876 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4877 return -EOPNOTSUPP;
4878
4879 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004880 err = _nfs4_get_security_label(inode, buf, buflen);
4881 trace_nfs4_get_security_label(inode, err);
4882 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004883 &exception);
4884 } while (exception.retry);
4885 return err;
4886}
4887
4888static int _nfs4_do_set_security_label(struct inode *inode,
4889 struct nfs4_label *ilabel,
4890 struct nfs_fattr *fattr,
4891 struct nfs4_label *olabel)
4892{
4893
4894 struct iattr sattr = {0};
4895 struct nfs_server *server = NFS_SERVER(inode);
4896 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004897 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004898 .fh = NFS_FH(inode),
4899 .iap = &sattr,
4900 .server = server,
4901 .bitmask = bitmask,
4902 .label = ilabel,
4903 };
4904 struct nfs_setattrres res = {
4905 .fattr = fattr,
4906 .label = olabel,
4907 .server = server,
4908 };
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004911 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004912 .rpc_resp = &res,
4913 };
4914 int status;
4915
Jeff Layton12207f62013-11-01 10:49:32 -04004916 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004917
Jeff Layton12207f62013-11-01 10:49:32 -04004918 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004919 if (status)
4920 dprintk("%s failed: %d\n", __func__, status);
4921
4922 return status;
4923}
4924
4925static int nfs4_do_set_security_label(struct inode *inode,
4926 struct nfs4_label *ilabel,
4927 struct nfs_fattr *fattr,
4928 struct nfs4_label *olabel)
4929{
4930 struct nfs4_exception exception = { };
4931 int err;
4932
4933 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004934 err = _nfs4_do_set_security_label(inode, ilabel,
4935 fattr, olabel);
4936 trace_nfs4_set_security_label(inode, err);
4937 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004938 &exception);
4939 } while (exception.retry);
4940 return err;
4941}
4942
4943static int
4944nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4945{
4946 struct nfs4_label ilabel, *olabel = NULL;
4947 struct nfs_fattr fattr;
4948 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00004949 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004950 int status;
4951
4952 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4953 return -EOPNOTSUPP;
4954
4955 nfs_fattr_init(&fattr);
4956
4957 ilabel.pi = 0;
4958 ilabel.lfs = 0;
4959 ilabel.label = (char *)buf;
4960 ilabel.len = buflen;
4961
4962 cred = rpc_lookup_cred();
4963 if (IS_ERR(cred))
4964 return PTR_ERR(cred);
4965
4966 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4967 if (IS_ERR(olabel)) {
4968 status = -PTR_ERR(olabel);
4969 goto out;
4970 }
4971
4972 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4973 if (status == 0)
4974 nfs_setsecurity(inode, &fattr, olabel);
4975
4976 nfs4_label_free(olabel);
4977out:
4978 put_rpccred(cred);
4979 return status;
4980}
4981#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4982
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984static int
Trond Myklebust516285eb2015-09-20 16:15:24 -04004985nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004986 struct nfs4_state *state, long *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004988 struct nfs_client *clp = server->nfs_client;
4989
4990 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 return 0;
4992 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004993 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004994 case -NFS4ERR_ADMIN_REVOKED:
4995 case -NFS4ERR_BAD_STATEID:
4996 case -NFS4ERR_OPENMODE:
4997 if (state == NULL)
4998 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004999 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04005000 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005001 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04005002 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04005003 if (state != NULL) {
5004 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04005005 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04005006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005008 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005009 nfs4_schedule_lease_recovery(clp);
5010 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04005011 case -NFS4ERR_MOVED:
5012 if (nfs4_schedule_migration_recovery(server) < 0)
5013 goto recovery_failed;
5014 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04005015 case -NFS4ERR_LEASE_MOVED:
5016 nfs4_schedule_lease_moved_recovery(clp);
5017 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005018#if defined(CONFIG_NFS_V4_1)
5019 case -NFS4ERR_BADSESSION:
5020 case -NFS4ERR_BADSLOT:
5021 case -NFS4ERR_BAD_HIGH_SLOT:
5022 case -NFS4ERR_DEADSESSION:
5023 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5024 case -NFS4ERR_SEQ_FALSE_RETRY:
5025 case -NFS4ERR_SEQ_MISORDERED:
5026 dprintk("%s ERROR %d, Reset session\n", __func__,
5027 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04005028 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05005029 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04005030#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005032 nfs_inc_server_stats(server, NFSIOS_DELAY);
NeilBrown8478eaa2014-09-18 16:09:27 +10005033 rpc_delay(task, nfs4_update_delay(timeout));
5034 goto restart_call;
Chuck Lever006ea732006-03-20 13:44:14 -05005035 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005037 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04005039 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 }
5041 task->tk_status = nfs4_map_errors(task->tk_status);
5042 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04005043recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04005044 task->tk_status = -EIO;
5045 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005046wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005047 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005048 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
5049 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04005050 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
5051 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04005052restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005053 task->tk_status = 0;
5054 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055}
5056
Chuck Leverf0920752012-05-21 22:45:41 -04005057static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5058 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005059{
5060 __be32 verf[2];
5061
Chuck Lever2c820d92012-05-21 22:45:33 -04005062 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5063 /* An impossible timestamp guarantees this value
5064 * will never match a generated boot time. */
5065 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005066 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005067 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005068 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005069 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5070 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005071 }
Chuck Levercd937102012-03-02 17:14:31 -05005072 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5073}
5074
Jeff Laytona3192682015-06-09 19:43:59 -04005075static int
5076nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005077{
Jeff Laytona3192682015-06-09 19:43:59 -04005078 int result;
5079 size_t len;
5080 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005081
Trond Myklebustceb3a162015-01-03 15:16:04 -05005082 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005083 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005084
Jeff Laytona3192682015-06-09 19:43:59 -04005085 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005086 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005087 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5088 1 +
5089 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5090 1;
5091 rcu_read_unlock();
5092
5093 if (len > NFS4_OPAQUE_LIMIT + 1)
5094 return -EINVAL;
5095
5096 /*
5097 * Since this string is allocated at mount time, and held until the
5098 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5099 * about a memory-reclaim deadlock.
5100 */
5101 str = kmalloc(len, GFP_KERNEL);
5102 if (!str)
5103 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005104
Chuck Levere984a552012-09-14 17:24:21 -04005105 rcu_read_lock();
Jeff Laytona3192682015-06-09 19:43:59 -04005106 result = scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5107 clp->cl_ipaddr,
5108 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5109 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005110 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005111
Jeff Laytona3192682015-06-09 19:43:59 -04005112 clp->cl_owner_id = str;
5113 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005114}
5115
Jeff Layton873e3852015-06-09 19:44:00 -04005116static int
5117nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005118{
Jeff Layton873e3852015-06-09 19:44:00 -04005119 int result;
5120 size_t len;
5121 char *str;
5122
5123 len = 10 + 10 + 1 + 10 + 1 +
5124 strlen(nfs4_client_id_uniquifier) + 1 +
5125 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5126
5127 if (len > NFS4_OPAQUE_LIMIT + 1)
5128 return -EINVAL;
5129
5130 /*
5131 * Since this string is allocated at mount time, and held until the
5132 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5133 * about a memory-reclaim deadlock.
5134 */
5135 str = kmalloc(len, GFP_KERNEL);
5136 if (!str)
5137 return -ENOMEM;
5138
5139 result = scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5140 clp->rpc_ops->version, clp->cl_minorversion,
5141 nfs4_client_id_uniquifier,
5142 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005143 clp->cl_owner_id = str;
5144 return 0;
5145}
5146
5147static int
5148nfs4_init_uniform_client_string(struct nfs_client *clp)
5149{
5150 int result;
5151 size_t len;
5152 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005153
5154 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005155 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005156
5157 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005158 return nfs4_init_uniquifier_client_string(clp);
5159
5160 len = 10 + 10 + 1 + 10 + 1 +
5161 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5162
5163 if (len > NFS4_OPAQUE_LIMIT + 1)
5164 return -EINVAL;
5165
5166 /*
5167 * Since this string is allocated at mount time, and held until the
5168 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5169 * about a memory-reclaim deadlock.
5170 */
5171 str = kmalloc(len, GFP_KERNEL);
5172 if (!str)
5173 return -ENOMEM;
5174
5175 result = scnprintf(str, len, "Linux NFSv%u.%u %s",
5176 clp->rpc_ops->version, clp->cl_minorversion,
5177 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005178 clp->cl_owner_id = str;
5179 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005180}
5181
Chuck Lever706cb8d2014-03-12 12:51:47 -04005182/*
5183 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5184 * services. Advertise one based on the address family of the
5185 * clientaddr.
5186 */
5187static unsigned int
5188nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5189{
5190 if (strchr(clp->cl_ipaddr, ':') != NULL)
5191 return scnprintf(buf, len, "tcp6");
5192 else
5193 return scnprintf(buf, len, "tcp");
5194}
5195
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005196static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5197{
5198 struct nfs4_setclientid *sc = calldata;
5199
5200 if (task->tk_status == 0)
5201 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5202}
5203
5204static const struct rpc_call_ops nfs4_setclientid_ops = {
5205 .rpc_call_done = nfs4_setclientid_done,
5206};
5207
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005208/**
5209 * nfs4_proc_setclientid - Negotiate client ID
5210 * @clp: state data structure
5211 * @program: RPC program for NFSv4 callback service
5212 * @port: IP port number for NFS4 callback service
5213 * @cred: RPC credential to use for this call
5214 * @res: where to place the result
5215 *
5216 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5217 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005218int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5219 unsigned short port, struct rpc_cred *cred,
5220 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
5222 nfs4_verifier sc_verifier;
5223 struct nfs4_setclientid setclientid = {
5224 .sc_verifier = &sc_verifier,
5225 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005226 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 };
5228 struct rpc_message msg = {
5229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5230 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005231 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005232 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005234 struct rpc_task *task;
5235 struct rpc_task_setup task_setup_data = {
5236 .rpc_client = clp->cl_rpcclient,
5237 .rpc_message = &msg,
5238 .callback_ops = &nfs4_setclientid_ops,
5239 .callback_data = &setclientid,
5240 .flags = RPC_TASK_TIMEOUT,
5241 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005242 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
Chuck Leverde734832012-07-11 16:30:50 -04005244 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005245 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005246
5247 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5248 status = nfs4_init_uniform_client_string(clp);
5249 else
Jeff Laytona3192682015-06-09 19:43:59 -04005250 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005251
5252 if (status)
5253 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005254
Chuck Leverde734832012-07-11 16:30:50 -04005255 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005256 setclientid.sc_netid_len =
5257 nfs4_init_callback_netid(clp,
5258 setclientid.sc_netid,
5259 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005260 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005261 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 clp->cl_ipaddr, port >> 8, port & 255);
5263
Jeff Layton3a6bb732015-06-09 19:43:57 -04005264 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005265 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005266 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005267 task = rpc_run_task(&task_setup_data);
5268 if (IS_ERR(task)) {
5269 status = PTR_ERR(task);
5270 goto out;
5271 }
5272 status = task->tk_status;
5273 if (setclientid.sc_cred) {
5274 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5275 put_rpccred(setclientid.sc_cred);
5276 }
5277 rpc_put_task(task);
5278out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005279 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005280 dprintk("NFS reply setclientid: %d\n", status);
5281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005284/**
5285 * nfs4_proc_setclientid_confirm - Confirm client ID
5286 * @clp: state data structure
5287 * @res: result of a previous SETCLIENTID
5288 * @cred: RPC credential to use for this call
5289 *
5290 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5291 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005292int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005293 struct nfs4_setclientid_res *arg,
5294 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 struct rpc_message msg = {
5297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005298 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005299 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 int status;
5302
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005303 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5304 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5305 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005306 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005307 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005308 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 return status;
5310}
5311
Trond Myklebustfe650402006-01-03 09:55:18 +01005312struct nfs4_delegreturndata {
5313 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005314 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005315 struct nfs_fh fh;
5316 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005317 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005318 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005319 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005320 struct inode *inode;
5321 bool roc;
5322 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005323};
5324
Trond Myklebustfe650402006-01-03 09:55:18 +01005325static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5326{
5327 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005328
Trond Myklebust14516c32010-07-31 14:29:06 -04005329 if (!nfs4_sequence_done(task, &data->res.seq_res))
5330 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005331
Trond Myklebustca8acf82013-08-13 10:36:56 -04005332 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005333 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005334 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005335 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005336 case -NFS4ERR_ADMIN_REVOKED:
5337 case -NFS4ERR_DELEG_REVOKED:
5338 case -NFS4ERR_BAD_STATEID:
5339 case -NFS4ERR_OLD_STATEID:
5340 case -NFS4ERR_STALE_STATEID:
5341 case -NFS4ERR_EXPIRED:
5342 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005343 if (data->roc)
5344 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005345 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005346 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005347 if (nfs4_async_handle_error(task, data->res.server,
5348 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005349 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005350 return;
5351 }
5352 }
5353 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005354}
5355
5356static void nfs4_delegreturn_release(void *calldata)
5357{
Peng Tao039b7562014-07-03 13:05:02 +08005358 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005359 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005360
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005361 if (inode) {
5362 if (data->roc)
5363 pnfs_roc_release(inode);
5364 nfs_iput_and_deactive(inode);
5365 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005366 kfree(calldata);
5367}
5368
Andy Adamson938e1012009-04-01 09:22:28 -04005369static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5370{
5371 struct nfs4_delegreturndata *d_data;
5372
5373 d_data = (struct nfs4_delegreturndata *)data;
5374
Peng Tao500d7012015-09-22 11:35:22 +08005375 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5376 return;
5377
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005378 if (d_data->roc)
5379 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005380
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005381 nfs4_setup_sequence(d_data->res.server,
5382 &d_data->args.seq_args,
5383 &d_data->res.seq_res,
5384 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005385}
Andy Adamson938e1012009-04-01 09:22:28 -04005386
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005387static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005388 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005389 .rpc_call_done = nfs4_delegreturn_done,
5390 .rpc_release = nfs4_delegreturn_release,
5391};
5392
Trond Myklebuste6f81072008-01-24 18:14:34 -05005393static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005394{
5395 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005396 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005397 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005398 struct rpc_message msg = {
5399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5400 .rpc_cred = cred,
5401 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005402 struct rpc_task_setup task_setup_data = {
5403 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005404 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005405 .callback_ops = &nfs4_delegreturn_ops,
5406 .flags = RPC_TASK_ASYNC,
5407 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005408 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005409
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005410 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005411 if (data == NULL)
5412 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005413 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005414 data->args.fhandle = &data->fh;
5415 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005416 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005417 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005418 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005419 data->res.fattr = &data->fattr;
5420 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005421 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005422 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005423 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005424 data->inode = nfs_igrab_and_active(inode);
5425 if (data->inode)
5426 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005427
Trond Myklebustc970aa82007-07-14 15:39:59 -04005428 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005429 msg.rpc_argp = &data->args;
5430 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005431 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005432 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005433 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005434 if (!issync)
5435 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005436 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005437 if (status != 0)
5438 goto out;
5439 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005440 if (status == 0)
5441 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5442 else
5443 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005444out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005445 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005446 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447}
5448
Trond Myklebuste6f81072008-01-24 18:14:34 -05005449int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
5451 struct nfs_server *server = NFS_SERVER(inode);
5452 struct nfs4_exception exception = { };
5453 int err;
5454 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005455 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005456 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 switch (err) {
5458 case -NFS4ERR_STALE_STATEID:
5459 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 case 0:
5461 return 0;
5462 }
5463 err = nfs4_handle_exception(server, err, &exception);
5464 } while (exception.retry);
5465 return err;
5466}
5467
5468#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5469#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5470
5471/*
5472 * sleep, with exponential backoff, and retry the LOCK operation.
5473 */
5474static unsigned long
5475nfs4_set_lock_task_retry(unsigned long timeout)
5476{
Colin Cross416ad3c2013-05-06 23:50:06 +00005477 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 timeout <<= 1;
5479 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5480 return NFS4_LOCK_MAXTIMEOUT;
5481 return timeout;
5482}
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5485{
5486 struct inode *inode = state->inode;
5487 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005488 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005489 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005491 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005493 struct nfs_lockt_res res = {
5494 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 };
5496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5498 .rpc_argp = &arg,
5499 .rpc_resp = &res,
5500 .rpc_cred = state->owner->so_cred,
5501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 struct nfs4_lock_state *lsp;
5503 int status;
5504
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005505 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005506 status = nfs4_set_lock_state(state, request);
5507 if (status != 0)
5508 goto out;
5509 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005510 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005511 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005513 switch (status) {
5514 case 0:
5515 request->fl_type = F_UNLCK;
5516 break;
5517 case -NFS4ERR_DENIED:
5518 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005520 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005521 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return status;
5524}
5525
5526static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5527{
5528 struct nfs4_exception exception = { };
5529 int err;
5530
5531 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005532 err = _nfs4_proc_getlk(state, cmd, request);
5533 trace_nfs4_get_lock(request, state, cmd, err);
5534 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 &exception);
5536 } while (exception.retry);
5537 return err;
5538}
5539
Jeff Layton83bfff22015-07-11 06:43:03 -04005540static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
5542 int res = 0;
5543 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5544 case FL_POSIX:
Jeff Layton83bfff22015-07-11 06:43:03 -04005545 res = posix_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 break;
5547 case FL_FLOCK:
Jeff Layton83bfff22015-07-11 06:43:03 -04005548 res = flock_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 break;
5550 default:
5551 BUG();
5552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 return res;
5554}
5555
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005556struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005557 struct nfs_locku_args arg;
5558 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005559 struct nfs4_lock_state *lsp;
5560 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005561 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005562 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005563 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005564};
5565
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005566static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5567 struct nfs_open_context *ctx,
5568 struct nfs4_lock_state *lsp,
5569 struct nfs_seqid *seqid)
5570{
5571 struct nfs4_unlockdata *p;
5572 struct inode *inode = lsp->ls_state->inode;
5573
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005574 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005575 if (p == NULL)
5576 return NULL;
5577 p->arg.fh = NFS_FH(inode);
5578 p->arg.fl = &p->fl;
5579 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005580 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005581 p->lsp = lsp;
5582 atomic_inc(&lsp->ls_count);
5583 /* Ensure we don't close file until we're done freeing locks! */
5584 p->ctx = get_nfs_open_context(ctx);
5585 memcpy(&p->fl, fl, sizeof(p->fl));
5586 p->server = NFS_SERVER(inode);
5587 return p;
5588}
5589
Trond Myklebust06f814a2006-01-03 09:55:07 +01005590static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005591{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005592 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005593 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005594 nfs4_put_lock_state(calldata->lsp);
5595 put_nfs_open_context(calldata->ctx);
5596 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005597}
5598
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005599static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005601 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005602
Trond Myklebust14516c32010-07-31 14:29:06 -04005603 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5604 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605 switch (task->tk_status) {
5606 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005607 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005608 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005609 if (nfs4_update_lock_stateid(calldata->lsp,
5610 &calldata->res.stateid))
5611 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005612 case -NFS4ERR_BAD_STATEID:
5613 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005614 case -NFS4ERR_STALE_STATEID:
5615 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005616 if (!nfs4_stateid_match(&calldata->arg.stateid,
5617 &calldata->lsp->ls_stateid))
5618 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005619 break;
5620 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005621 if (nfs4_async_handle_error(task, calldata->server,
5622 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005623 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005625 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005626}
5627
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005628static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005629{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005630 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005632 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005633 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005634 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005635 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005636 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005637 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005638 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005639 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005640 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005641 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005642 &calldata->res.seq_res,
5643 task) != 0)
5644 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005645 return;
5646out_no_action:
5647 task->tk_action = NULL;
5648out_wait:
5649 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005652static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005653 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005654 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005655 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005656};
5657
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005658static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5659 struct nfs_open_context *ctx,
5660 struct nfs4_lock_state *lsp,
5661 struct nfs_seqid *seqid)
5662{
5663 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005664 struct rpc_message msg = {
5665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5666 .rpc_cred = ctx->cred,
5667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005668 struct rpc_task_setup task_setup_data = {
5669 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005671 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005672 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005673 .flags = RPC_TASK_ASYNC,
5674 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005675
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005676 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5677 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5678
Frank Filz137d6ac2007-07-09 15:32:29 -07005679 /* Ensure this is an unlock - when canceling a lock, the
5680 * canceled lock is passed in, and it won't be an unlock.
5681 */
5682 fl->fl_type = F_UNLCK;
5683
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005684 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5685 if (data == NULL) {
5686 nfs_free_seqid(seqid);
5687 return ERR_PTR(-ENOMEM);
5688 }
5689
Chuck Levera9c92d62013-08-09 12:48:18 -04005690 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005691 msg.rpc_argp = &data->arg;
5692 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 task_setup_data.callback_data = data;
5694 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005695}
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5698{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005699 struct inode *inode = state->inode;
5700 struct nfs4_state_owner *sp = state->owner;
5701 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005702 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005703 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005704 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005705 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005706 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005707 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Trond Myklebust9b073572006-06-29 16:38:34 -04005709 status = nfs4_set_lock_state(state, request);
5710 /* Unlock _before_ we do the RPC call */
5711 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005712 /* Exclude nfs_delegation_claim_locks() */
5713 mutex_lock(&sp->so_delegreturn_mutex);
5714 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005715 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005716 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005717 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005718 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005719 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005720 }
5721 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005722 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005723 if (status != 0)
5724 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005725 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005726 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005727 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5728 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005729 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5730 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005731 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005732 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005733 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005734 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005735 status = PTR_ERR(task);
5736 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005737 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005738 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005739 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005740out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005741 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005742 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005743 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744}
5745
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005746struct nfs4_lockdata {
5747 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005748 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005749 struct nfs4_lock_state *lsp;
5750 struct nfs_open_context *ctx;
5751 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005752 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005753 int rpc_status;
5754 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005755 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005756};
5757
5758static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005759 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5760 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005761{
5762 struct nfs4_lockdata *p;
5763 struct inode *inode = lsp->ls_state->inode;
5764 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005765 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005766
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005767 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005768 if (p == NULL)
5769 return NULL;
5770
5771 p->arg.fh = NFS_FH(inode);
5772 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005773 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005774 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005775 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005776 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5777 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005778 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005779 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005780 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005781 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005782 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005783 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005785 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005786 atomic_inc(&lsp->ls_count);
5787 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005788 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005789 memcpy(&p->fl, fl, sizeof(p->fl));
5790 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005791out_free_seqid:
5792 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005793out_free:
5794 kfree(p);
5795 return NULL;
5796}
5797
5798static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5799{
5800 struct nfs4_lockdata *data = calldata;
5801 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
Harvey Harrison3110ff82008-05-02 13:42:44 -07005803 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005804 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005805 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005806 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005807 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005808 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005809 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005810 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005811 nfs4_stateid_copy(&data->arg.open_stateid,
5812 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005813 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005814 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005815 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005816 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005817 nfs4_stateid_copy(&data->arg.lock_stateid,
5818 &data->lsp->ls_stateid);
5819 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005820 if (!nfs4_valid_open_stateid(state)) {
5821 data->rpc_status = -EBADF;
5822 task->tk_action = NULL;
5823 goto out_release_open_seqid;
5824 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005825 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005826 if (nfs4_setup_sequence(data->server,
5827 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005828 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005829 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005830 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005831out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005832 nfs_release_seqid(data->arg.open_seqid);
5833out_release_lock_seqid:
5834 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005835out_wait:
5836 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005837 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005838}
5839
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005840static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5841{
5842 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005843 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005844
Harvey Harrison3110ff82008-05-02 13:42:44 -07005845 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005846
Trond Myklebust14516c32010-07-31 14:29:06 -04005847 if (!nfs4_sequence_done(task, &data->res.seq_res))
5848 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005849
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005850 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005851 switch (task->tk_status) {
5852 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005853 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005854 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005855 if (data->arg.new_lock) {
5856 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005857 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005858 rpc_restart_call_prepare(task);
5859 break;
5860 }
5861 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005862 if (data->arg.new_lock_owner != 0) {
5863 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5864 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5865 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5866 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5867 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005868 break;
5869 case -NFS4ERR_BAD_STATEID:
5870 case -NFS4ERR_OLD_STATEID:
5871 case -NFS4ERR_STALE_STATEID:
5872 case -NFS4ERR_EXPIRED:
5873 if (data->arg.new_lock_owner != 0) {
5874 if (!nfs4_stateid_match(&data->arg.open_stateid,
5875 &lsp->ls_state->open_stateid))
5876 rpc_restart_call_prepare(task);
5877 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5878 &lsp->ls_stateid))
5879 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005880 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005881 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005882}
5883
5884static void nfs4_lock_release(void *calldata)
5885{
5886 struct nfs4_lockdata *data = calldata;
5887
Harvey Harrison3110ff82008-05-02 13:42:44 -07005888 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005889 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005890 if (data->cancelled != 0) {
5891 struct rpc_task *task;
5892 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5893 data->arg.lock_seqid);
5894 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005895 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005896 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005897 } else
5898 nfs_free_seqid(data->arg.lock_seqid);
5899 nfs4_put_lock_state(data->lsp);
5900 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005901 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005902 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005903 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005904}
5905
5906static const struct rpc_call_ops nfs4_lock_ops = {
5907 .rpc_call_prepare = nfs4_lock_prepare,
5908 .rpc_call_done = nfs4_lock_done,
5909 .rpc_release = nfs4_lock_release,
5910};
5911
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005912static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5913{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005914 switch (error) {
5915 case -NFS4ERR_ADMIN_REVOKED:
5916 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005917 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005918 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005919 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005920 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005921 break;
5922 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005923 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005924 case -NFS4ERR_EXPIRED:
5925 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005926 };
5927}
5928
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005929static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930{
5931 struct nfs4_lockdata *data;
5932 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005933 struct rpc_message msg = {
5934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5935 .rpc_cred = state->owner->so_cred,
5936 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005937 struct rpc_task_setup task_setup_data = {
5938 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005939 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005941 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005942 .flags = RPC_TASK_ASYNC,
5943 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005944 int ret;
5945
Harvey Harrison3110ff82008-05-02 13:42:44 -07005946 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005947 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005948 fl->fl_u.nfs4_fl.owner,
5949 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005950 if (data == NULL)
5951 return -ENOMEM;
5952 if (IS_SETLKW(cmd))
5953 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005954 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005955 msg.rpc_argp = &data->arg;
5956 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005957 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005958 if (recovery_type > NFS_LOCK_NEW) {
5959 if (recovery_type == NFS_LOCK_RECLAIM)
5960 data->arg.reclaim = NFS_LOCK_RECLAIM;
5961 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005962 } else
5963 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005964 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005965 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005966 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005967 ret = nfs4_wait_for_completion_rpc_task(task);
5968 if (ret == 0) {
5969 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005970 if (ret)
5971 nfs4_handle_setlk_error(data->server, data->lsp,
5972 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973 } else
5974 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005975 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005976 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978}
5979
5980static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5981{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005982 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005983 struct nfs4_exception exception = {
5984 .inode = state->inode,
5985 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005986 int err;
5987
5988 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005989 /* Cache the lock if possible... */
5990 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5991 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005992 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005993 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005994 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005995 break;
5996 nfs4_handle_exception(server, err, &exception);
5997 } while (exception.retry);
5998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
6001static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6002{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006003 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006004 struct nfs4_exception exception = {
6005 .inode = state->inode,
6006 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006007 int err;
6008
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006009 err = nfs4_set_lock_state(state, request);
6010 if (err != 0)
6011 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006012 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006013 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6014 return 0;
6015 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006016 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006017 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6018 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006019 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006020 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006021 switch (err) {
6022 default:
6023 goto out;
6024 case -NFS4ERR_GRACE:
6025 case -NFS4ERR_DELAY:
6026 nfs4_handle_exception(server, err, &exception);
6027 err = 0;
6028 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006029 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006030out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006034#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006035/**
6036 * nfs41_check_expired_locks - possibly free a lock stateid
6037 *
6038 * @state: NFSv4 state for an inode
6039 *
6040 * Returns NFS_OK if recovery for this stateid is now finished.
6041 * Otherwise a negative NFS4ERR value is returned.
6042 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006043static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006044{
Chuck Levereb64cf92012-07-11 16:30:05 -04006045 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006046 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006047 struct nfs_server *server = NFS_SERVER(state->inode);
6048
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006049 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006050 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006051 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6052
6053 status = nfs41_test_stateid(server,
6054 &lsp->ls_stateid,
6055 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006056 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006057 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006058 /* Free the stateid unless the server
6059 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006060 if (status != -NFS4ERR_BAD_STATEID)
6061 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006062 &lsp->ls_stateid,
6063 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006064 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006065 ret = status;
6066 }
6067 }
6068 };
6069
6070 return ret;
6071}
6072
6073static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6074{
6075 int status = NFS_OK;
6076
6077 if (test_bit(LK_STATE_IN_USE, &state->flags))
6078 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006079 if (status != NFS_OK)
6080 status = nfs4_lock_expired(state, request);
6081 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006082}
6083#endif
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6086{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006087 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006088 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006089 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006091 if ((fl_flags & FL_POSIX) &&
6092 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6093 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006094 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006095 status = nfs4_set_lock_state(state, request);
6096 if (status != 0)
6097 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006098 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006099 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006100 if (status < 0)
6101 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006102 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006104 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006105 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006106 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006107 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006108 up_read(&nfsi->rwsem);
6109 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006110 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006111 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006112 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006113out:
6114 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return status;
6116}
6117
6118static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6119{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006120 struct nfs4_exception exception = {
6121 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006122 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 int err;
6125
6126 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006127 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006128 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07006129 if (err == -NFS4ERR_DENIED)
6130 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006132 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 } while (exception.retry);
6134 return err;
6135}
6136
6137static int
6138nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6139{
6140 struct nfs_open_context *ctx;
6141 struct nfs4_state *state;
6142 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6143 int status;
6144
6145 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006146 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 state = ctx->state;
6148
6149 if (request->fl_start < 0 || request->fl_end < 0)
6150 return -EINVAL;
6151
Trond Myklebustd9531262009-07-21 19:22:38 -04006152 if (IS_GETLK(cmd)) {
6153 if (state != NULL)
6154 return nfs4_proc_getlk(state, F_GETLK, request);
6155 return 0;
6156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
6158 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6159 return -EINVAL;
6160
Trond Myklebustd9531262009-07-21 19:22:38 -04006161 if (request->fl_type == F_UNLCK) {
6162 if (state != NULL)
6163 return nfs4_proc_unlck(state, cmd, request);
6164 return 0;
6165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Trond Myklebustd9531262009-07-21 19:22:38 -04006167 if (state == NULL)
6168 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006169 /*
6170 * Don't rely on the VFS having checked the file open mode,
6171 * since it won't do this for flock() locks.
6172 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006173 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006174 case F_RDLCK:
6175 if (!(filp->f_mode & FMODE_READ))
6176 return -EBADF;
6177 break;
6178 case F_WRLCK:
6179 if (!(filp->f_mode & FMODE_WRITE))
6180 return -EBADF;
6181 }
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 do {
6184 status = nfs4_proc_setlk(state, cmd, request);
6185 if ((status != -EAGAIN) || IS_SETLK(cmd))
6186 break;
6187 timeout = nfs4_set_lock_task_retry(timeout);
6188 status = -ERESTARTSYS;
6189 if (signalled())
6190 break;
6191 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 return status;
6193}
6194
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006195int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006196{
6197 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006198 int err;
6199
6200 err = nfs4_set_lock_state(state, fl);
6201 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006202 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006203 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006204 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006205}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006206
Trond Myklebustcf470c32012-03-07 13:49:12 -05006207struct nfs_release_lockowner_data {
6208 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006209 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006210 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006211 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006212 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006213};
6214
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006215static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6216{
6217 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006218 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006219 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6220 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006221 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006222 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006223}
6224
6225static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6226{
6227 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006228 struct nfs_server *server = data->server;
6229
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006230 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006231
6232 switch (task->tk_status) {
6233 case 0:
6234 renew_lease(server, data->timestamp);
6235 break;
6236 case -NFS4ERR_STALE_CLIENTID:
6237 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006238 nfs4_schedule_lease_recovery(server->nfs_client);
6239 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006240 case -NFS4ERR_LEASE_MOVED:
6241 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006242 if (nfs4_async_handle_error(task, server,
6243 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006244 rpc_restart_call_prepare(task);
6245 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006246}
6247
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006248static void nfs4_release_lockowner_release(void *calldata)
6249{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006250 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006251 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006252 kfree(calldata);
6253}
6254
Trond Myklebust17280172012-03-11 13:11:00 -04006255static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006256 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6257 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006258 .rpc_release = nfs4_release_lockowner_release,
6259};
6260
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006261static void
6262nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006263{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006264 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006265 struct rpc_message msg = {
6266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6267 };
6268
6269 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006270 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006271
Trond Myklebustcf470c32012-03-07 13:49:12 -05006272 data = kmalloc(sizeof(*data), GFP_NOFS);
6273 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006274 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006275 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006276 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006277 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6278 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6279 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006280
Trond Myklebustcf470c32012-03-07 13:49:12 -05006281 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006282 msg.rpc_resp = &data->res;
6283 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006284 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006285}
6286
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006287#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6288
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006289static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
6290 const void *buf, size_t buflen,
6291 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006292{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006293 if (strcmp(key, "") != 0)
6294 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006295
David Howells2b0143b2015-03-17 22:25:59 +00006296 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006297}
6298
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006299static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
6300 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006301{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006302 if (strcmp(key, "") != 0)
6303 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006304
David Howells2b0143b2015-03-17 22:25:59 +00006305 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006306}
6307
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006308static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
6309 size_t list_len, const char *name,
6310 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006311{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006312 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006313
David Howells2b0143b2015-03-17 22:25:59 +00006314 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))))
J. Bruce Fields096455a2006-03-20 23:23:42 -05006315 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006316
6317 if (list && len <= list_len)
6318 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006319 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006320}
6321
David Quigleyc9bccef2013-05-22 12:50:45 -04006322#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6323static inline int nfs4_server_supports_labels(struct nfs_server *server)
6324{
6325 return server->caps & NFS_CAP_SECURITY_LABEL;
6326}
6327
6328static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
6329 const void *buf, size_t buflen,
6330 int flags, int type)
6331{
6332 if (security_ismaclabel(key))
6333 return nfs4_set_security_label(dentry, buf, buflen);
6334
6335 return -EOPNOTSUPP;
6336}
6337
6338static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
6339 void *buf, size_t buflen, int type)
6340{
6341 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006342 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006343 return -EOPNOTSUPP;
6344}
6345
6346static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6347 size_t list_len, const char *name,
6348 size_t name_len, int type)
6349{
6350 size_t len = 0;
6351
David Howells2b0143b2015-03-17 22:25:59 +00006352 if (nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
6353 len = security_inode_listsecurity(d_inode(dentry), NULL, 0);
David Quigleyc9bccef2013-05-22 12:50:45 -04006354 if (list && len <= list_len)
David Howells2b0143b2015-03-17 22:25:59 +00006355 security_inode_listsecurity(d_inode(dentry), list, len);
David Quigleyc9bccef2013-05-22 12:50:45 -04006356 }
6357 return len;
6358}
6359
6360static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6361 .prefix = XATTR_SECURITY_PREFIX,
6362 .list = nfs4_xattr_list_nfs4_label,
6363 .get = nfs4_xattr_get_nfs4_label,
6364 .set = nfs4_xattr_set_nfs4_label,
6365};
6366#endif
6367
6368
Andy Adamson533eb462011-06-13 18:25:56 -04006369/*
6370 * nfs_fhget will use either the mounted_on_fileid or the fileid
6371 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006372static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6373{
Andy Adamson533eb462011-06-13 18:25:56 -04006374 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6375 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6376 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006377 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006378 return;
6379
6380 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006381 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006382 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6383 fattr->nlink = 2;
6384}
6385
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006386static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6387 const struct qstr *name,
6388 struct nfs4_fs_locations *fs_locations,
6389 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006390{
6391 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006392 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006393 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006394 };
6395 struct nfs4_fs_locations_arg args = {
6396 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006397 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006398 .page = page,
6399 .bitmask = bitmask,
6400 };
Benny Halevy22958462009-04-01 09:22:02 -04006401 struct nfs4_fs_locations_res res = {
6402 .fs_locations = fs_locations,
6403 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006404 struct rpc_message msg = {
6405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6406 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006407 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006408 };
6409 int status;
6410
Harvey Harrison3110ff82008-05-02 13:42:44 -07006411 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006412
6413 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6414 * is not supported */
6415 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6416 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6417 else
6418 bitmask[0] |= FATTR4_WORD0_FILEID;
6419
Trond Myklebustc228fd32007-01-13 02:28:11 -05006420 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006421 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006422 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006423 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006424 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006425 return status;
6426}
6427
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006428int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6429 const struct qstr *name,
6430 struct nfs4_fs_locations *fs_locations,
6431 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006432{
6433 struct nfs4_exception exception = { };
6434 int err;
6435 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006436 err = _nfs4_proc_fs_locations(client, dir, name,
6437 fs_locations, page);
6438 trace_nfs4_get_fs_locations(dir, name, err);
6439 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006440 &exception);
6441 } while (exception.retry);
6442 return err;
6443}
6444
Chuck Leverb03d7352013-10-17 14:12:50 -04006445/*
6446 * This operation also signals the server that this client is
6447 * performing migration recovery. The server can stop returning
6448 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6449 * appended to this compound to identify the client ID which is
6450 * performing recovery.
6451 */
6452static int _nfs40_proc_get_locations(struct inode *inode,
6453 struct nfs4_fs_locations *locations,
6454 struct page *page, struct rpc_cred *cred)
6455{
6456 struct nfs_server *server = NFS_SERVER(inode);
6457 struct rpc_clnt *clnt = server->client;
6458 u32 bitmask[2] = {
6459 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6460 };
6461 struct nfs4_fs_locations_arg args = {
6462 .clientid = server->nfs_client->cl_clientid,
6463 .fh = NFS_FH(inode),
6464 .page = page,
6465 .bitmask = bitmask,
6466 .migration = 1, /* skip LOOKUP */
6467 .renew = 1, /* append RENEW */
6468 };
6469 struct nfs4_fs_locations_res res = {
6470 .fs_locations = locations,
6471 .migration = 1,
6472 .renew = 1,
6473 };
6474 struct rpc_message msg = {
6475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6476 .rpc_argp = &args,
6477 .rpc_resp = &res,
6478 .rpc_cred = cred,
6479 };
6480 unsigned long now = jiffies;
6481 int status;
6482
6483 nfs_fattr_init(&locations->fattr);
6484 locations->server = server;
6485 locations->nlocations = 0;
6486
6487 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6488 nfs4_set_sequence_privileged(&args.seq_args);
6489 status = nfs4_call_sync_sequence(clnt, server, &msg,
6490 &args.seq_args, &res.seq_res);
6491 if (status)
6492 return status;
6493
6494 renew_lease(server, now);
6495 return 0;
6496}
6497
6498#ifdef CONFIG_NFS_V4_1
6499
6500/*
6501 * This operation also signals the server that this client is
6502 * performing migration recovery. The server can stop asserting
6503 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6504 * performing this operation is identified in the SEQUENCE
6505 * operation in this compound.
6506 *
6507 * When the client supports GETATTR(fs_locations_info), it can
6508 * be plumbed in here.
6509 */
6510static int _nfs41_proc_get_locations(struct inode *inode,
6511 struct nfs4_fs_locations *locations,
6512 struct page *page, struct rpc_cred *cred)
6513{
6514 struct nfs_server *server = NFS_SERVER(inode);
6515 struct rpc_clnt *clnt = server->client;
6516 u32 bitmask[2] = {
6517 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6518 };
6519 struct nfs4_fs_locations_arg args = {
6520 .fh = NFS_FH(inode),
6521 .page = page,
6522 .bitmask = bitmask,
6523 .migration = 1, /* skip LOOKUP */
6524 };
6525 struct nfs4_fs_locations_res res = {
6526 .fs_locations = locations,
6527 .migration = 1,
6528 };
6529 struct rpc_message msg = {
6530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6531 .rpc_argp = &args,
6532 .rpc_resp = &res,
6533 .rpc_cred = cred,
6534 };
6535 int status;
6536
6537 nfs_fattr_init(&locations->fattr);
6538 locations->server = server;
6539 locations->nlocations = 0;
6540
6541 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6542 nfs4_set_sequence_privileged(&args.seq_args);
6543 status = nfs4_call_sync_sequence(clnt, server, &msg,
6544 &args.seq_args, &res.seq_res);
6545 if (status == NFS4_OK &&
6546 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6547 status = -NFS4ERR_LEASE_MOVED;
6548 return status;
6549}
6550
6551#endif /* CONFIG_NFS_V4_1 */
6552
6553/**
6554 * nfs4_proc_get_locations - discover locations for a migrated FSID
6555 * @inode: inode on FSID that is migrating
6556 * @locations: result of query
6557 * @page: buffer
6558 * @cred: credential to use for this operation
6559 *
6560 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6561 * operation failed, or a negative errno if a local error occurred.
6562 *
6563 * On success, "locations" is filled in, but if the server has
6564 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6565 * asserted.
6566 *
6567 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6568 * from this client that require migration recovery.
6569 */
6570int nfs4_proc_get_locations(struct inode *inode,
6571 struct nfs4_fs_locations *locations,
6572 struct page *page, struct rpc_cred *cred)
6573{
6574 struct nfs_server *server = NFS_SERVER(inode);
6575 struct nfs_client *clp = server->nfs_client;
6576 const struct nfs4_mig_recovery_ops *ops =
6577 clp->cl_mvops->mig_recovery_ops;
6578 struct nfs4_exception exception = { };
6579 int status;
6580
6581 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6582 (unsigned long long)server->fsid.major,
6583 (unsigned long long)server->fsid.minor,
6584 clp->cl_hostname);
6585 nfs_display_fhandle(NFS_FH(inode), __func__);
6586
6587 do {
6588 status = ops->get_locations(inode, locations, page, cred);
6589 if (status != -NFS4ERR_DELAY)
6590 break;
6591 nfs4_handle_exception(server, status, &exception);
6592 } while (exception.retry);
6593 return status;
6594}
6595
Chuck Lever44c99932013-10-17 14:13:30 -04006596/*
6597 * This operation also signals the server that this client is
6598 * performing "lease moved" recovery. The server can stop
6599 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6600 * is appended to this compound to identify the client ID which is
6601 * performing recovery.
6602 */
6603static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6604{
6605 struct nfs_server *server = NFS_SERVER(inode);
6606 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6607 struct rpc_clnt *clnt = server->client;
6608 struct nfs4_fsid_present_arg args = {
6609 .fh = NFS_FH(inode),
6610 .clientid = clp->cl_clientid,
6611 .renew = 1, /* append RENEW */
6612 };
6613 struct nfs4_fsid_present_res res = {
6614 .renew = 1,
6615 };
6616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6618 .rpc_argp = &args,
6619 .rpc_resp = &res,
6620 .rpc_cred = cred,
6621 };
6622 unsigned long now = jiffies;
6623 int status;
6624
6625 res.fh = nfs_alloc_fhandle();
6626 if (res.fh == NULL)
6627 return -ENOMEM;
6628
6629 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6630 nfs4_set_sequence_privileged(&args.seq_args);
6631 status = nfs4_call_sync_sequence(clnt, server, &msg,
6632 &args.seq_args, &res.seq_res);
6633 nfs_free_fhandle(res.fh);
6634 if (status)
6635 return status;
6636
6637 do_renew_lease(clp, now);
6638 return 0;
6639}
6640
6641#ifdef CONFIG_NFS_V4_1
6642
6643/*
6644 * This operation also signals the server that this client is
6645 * performing "lease moved" recovery. The server can stop asserting
6646 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6647 * this operation is identified in the SEQUENCE operation in this
6648 * compound.
6649 */
6650static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6651{
6652 struct nfs_server *server = NFS_SERVER(inode);
6653 struct rpc_clnt *clnt = server->client;
6654 struct nfs4_fsid_present_arg args = {
6655 .fh = NFS_FH(inode),
6656 };
6657 struct nfs4_fsid_present_res res = {
6658 };
6659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6661 .rpc_argp = &args,
6662 .rpc_resp = &res,
6663 .rpc_cred = cred,
6664 };
6665 int status;
6666
6667 res.fh = nfs_alloc_fhandle();
6668 if (res.fh == NULL)
6669 return -ENOMEM;
6670
6671 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6672 nfs4_set_sequence_privileged(&args.seq_args);
6673 status = nfs4_call_sync_sequence(clnt, server, &msg,
6674 &args.seq_args, &res.seq_res);
6675 nfs_free_fhandle(res.fh);
6676 if (status == NFS4_OK &&
6677 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6678 status = -NFS4ERR_LEASE_MOVED;
6679 return status;
6680}
6681
6682#endif /* CONFIG_NFS_V4_1 */
6683
6684/**
6685 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6686 * @inode: inode on FSID to check
6687 * @cred: credential to use for this operation
6688 *
6689 * Server indicates whether the FSID is present, moved, or not
6690 * recognized. This operation is necessary to clear a LEASE_MOVED
6691 * condition for this client ID.
6692 *
6693 * Returns NFS4_OK if the FSID is present on this server,
6694 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6695 * NFS4ERR code if some error occurred on the server, or a
6696 * negative errno if a local failure occurred.
6697 */
6698int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6699{
6700 struct nfs_server *server = NFS_SERVER(inode);
6701 struct nfs_client *clp = server->nfs_client;
6702 const struct nfs4_mig_recovery_ops *ops =
6703 clp->cl_mvops->mig_recovery_ops;
6704 struct nfs4_exception exception = { };
6705 int status;
6706
6707 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6708 (unsigned long long)server->fsid.major,
6709 (unsigned long long)server->fsid.minor,
6710 clp->cl_hostname);
6711 nfs_display_fhandle(NFS_FH(inode), __func__);
6712
6713 do {
6714 status = ops->fsid_present(inode, cred);
6715 if (status != -NFS4ERR_DELAY)
6716 break;
6717 nfs4_handle_exception(server, status, &exception);
6718 } while (exception.retry);
6719 return status;
6720}
6721
Andy Adamson5ec16a82013-08-08 10:57:55 -04006722/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006723 * If 'use_integrity' is true and the state managment nfs_client
6724 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6725 * and the machine credential as per RFC3530bis and RFC5661 Security
6726 * Considerations sections. Otherwise, just use the user cred with the
6727 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006728 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006729static 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 +00006730{
6731 int status;
6732 struct nfs4_secinfo_arg args = {
6733 .dir_fh = NFS_FH(dir),
6734 .name = name,
6735 };
6736 struct nfs4_secinfo_res res = {
6737 .flavors = flavors,
6738 };
6739 struct rpc_message msg = {
6740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6741 .rpc_argp = &args,
6742 .rpc_resp = &res,
6743 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006744 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006745 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006746
6747 if (use_integrity) {
6748 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006749 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6750 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006751 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006752
6753 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006754
6755 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6756 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6757
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006758 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6759 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006760 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006761
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006762 if (cred)
6763 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006764
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006765 return status;
6766}
6767
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006768int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6769 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006770{
6771 struct nfs4_exception exception = { };
6772 int err;
6773 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006774 err = -NFS4ERR_WRONGSEC;
6775
6776 /* try to use integrity protection with machine cred */
6777 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6778 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6779
6780 /*
6781 * if unable to use integrity protection, or SECINFO with
6782 * integrity protection returns NFS4ERR_WRONGSEC (which is
6783 * disallowed by spec, but exists in deployed servers) use
6784 * the current filesystem's rpc_client and the user cred.
6785 */
6786 if (err == -NFS4ERR_WRONGSEC)
6787 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6788
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006789 trace_nfs4_secinfo(dir, name, err);
6790 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006791 &exception);
6792 } while (exception.retry);
6793 return err;
6794}
6795
Andy Adamson557134a2009-04-01 09:21:53 -04006796#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006797/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006798 * Check the exchange flags returned by the server for invalid flags, having
6799 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6800 * DS flags set.
6801 */
6802static int nfs4_check_cl_exchange_flags(u32 flags)
6803{
6804 if (flags & ~EXCHGID4_FLAG_MASK_R)
6805 goto out_inval;
6806 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6807 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6808 goto out_inval;
6809 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6810 goto out_inval;
6811 return NFS_OK;
6812out_inval:
6813 return -NFS4ERR_INVAL;
6814}
6815
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006816static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006817nfs41_same_server_scope(struct nfs41_server_scope *a,
6818 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006819{
6820 if (a->server_scope_sz == b->server_scope_sz &&
6821 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6822 return true;
6823
6824 return false;
6825}
6826
Andy Adamson357f54d2010-12-14 10:11:57 -05006827/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006828 * nfs4_proc_bind_conn_to_session()
6829 *
6830 * The 4.1 client currently uses the same TCP connection for the
6831 * fore and backchannel.
6832 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006833int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006834{
6835 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006836 struct nfs41_bind_conn_to_session_args args = {
6837 .client = clp,
6838 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6839 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006840 struct nfs41_bind_conn_to_session_res res;
6841 struct rpc_message msg = {
6842 .rpc_proc =
6843 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006844 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006845 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006846 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006847 };
6848
6849 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006850
Trond Myklebust71a097c2015-02-18 09:27:18 -08006851 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6852 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6853 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006854
6855 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006856 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006857 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006858 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006859 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6860 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6861 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006862 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006863 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006864 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006865 dprintk("NFS: %s: Unexpected direction from server\n",
6866 __func__);
6867 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006868 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006869 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006870 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006871 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6872 __func__);
6873 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006874 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006875 }
6876 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006877out:
6878 dprintk("<-- %s status= %d\n", __func__, status);
6879 return status;
6880}
6881
6882/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006883 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6884 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006885 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006886static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6887 .how = SP4_MACH_CRED,
6888 .enforce.u.words = {
6889 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6890 1 << (OP_EXCHANGE_ID - 32) |
6891 1 << (OP_CREATE_SESSION - 32) |
6892 1 << (OP_DESTROY_SESSION - 32) |
6893 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006894 },
6895 .allow.u.words = {
6896 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006897 1 << (OP_LOCKU) |
6898 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006899 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006900 1 << (OP_SECINFO_NO_NAME - 32) |
6901 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006902 1 << (OP_FREE_STATEID - 32) |
6903 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006904 }
6905};
6906
6907/*
6908 * Select the state protection mode for client `clp' given the server results
6909 * from exchange_id in `sp'.
6910 *
6911 * Returns 0 on success, negative errno otherwise.
6912 */
6913static int nfs4_sp4_select_mode(struct nfs_client *clp,
6914 struct nfs41_state_protection *sp)
6915{
6916 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6917 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6918 1 << (OP_EXCHANGE_ID - 32) |
6919 1 << (OP_CREATE_SESSION - 32) |
6920 1 << (OP_DESTROY_SESSION - 32) |
6921 1 << (OP_DESTROY_CLIENTID - 32)
6922 };
6923 unsigned int i;
6924
6925 if (sp->how == SP4_MACH_CRED) {
6926 /* Print state protect result */
6927 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6928 for (i = 0; i <= LAST_NFS4_OP; i++) {
6929 if (test_bit(i, sp->enforce.u.longs))
6930 dfprintk(MOUNT, " enforce op %d\n", i);
6931 if (test_bit(i, sp->allow.u.longs))
6932 dfprintk(MOUNT, " allow op %d\n", i);
6933 }
6934
6935 /* make sure nothing is on enforce list that isn't supported */
6936 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6937 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6938 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6939 return -EINVAL;
6940 }
6941 }
6942
6943 /*
6944 * Minimal mode - state operations are allowed to use machine
6945 * credential. Note this already happens by default, so the
6946 * client doesn't have to do anything more than the negotiation.
6947 *
6948 * NOTE: we don't care if EXCHANGE_ID is in the list -
6949 * we're already using the machine cred for exchange_id
6950 * and will never use a different cred.
6951 */
6952 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6953 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6954 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6955 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6956 dfprintk(MOUNT, "sp4_mach_cred:\n");
6957 dfprintk(MOUNT, " minimal mode enabled\n");
6958 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6959 } else {
6960 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6961 return -EINVAL;
6962 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006963
6964 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6965 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6966 dfprintk(MOUNT, " cleanup mode enabled\n");
6967 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6968 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006969
6970 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6971 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6972 dfprintk(MOUNT, " secinfo mode enabled\n");
6973 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6974 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006975
6976 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6977 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6978 dfprintk(MOUNT, " stateid mode enabled\n");
6979 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6980 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006981
6982 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6983 dfprintk(MOUNT, " write mode enabled\n");
6984 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6985 }
6986
6987 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6988 dfprintk(MOUNT, " commit mode enabled\n");
6989 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6990 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006991 }
6992
6993 return 0;
6994}
6995
6996/*
6997 * _nfs4_proc_exchange_id()
6998 *
6999 * Wrapper for EXCHANGE_ID operation.
7000 */
7001static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7002 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007003{
7004 nfs4_verifier verifier;
7005 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007006 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007007 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007008#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007009 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007010 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7011 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7012#else
7013 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7014 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7015#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007016 };
7017 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007018 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007019 };
7020 int status;
7021 struct rpc_message msg = {
7022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7023 .rpc_argp = &args,
7024 .rpc_resp = &res,
7025 .rpc_cred = cred,
7026 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007027
Chuck Leverf0920752012-05-21 22:45:41 -04007028 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007029
7030 status = nfs4_init_uniform_client_string(clp);
7031 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007032 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007033
7034 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007035 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007036 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007037
Chuck Leveracdeb692012-05-21 22:46:16 -04007038 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007039 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007040 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007041 status = -ENOMEM;
7042 goto out;
7043 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007044
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007045 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007046 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007047 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007048 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007049 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007050 }
7051
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007052 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007053 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007054 status = -ENOMEM;
7055 goto out_server_scope;
7056 }
7057
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007058 switch (sp4_how) {
7059 case SP4_NONE:
7060 args.state_protect.how = SP4_NONE;
7061 break;
7062
7063 case SP4_MACH_CRED:
7064 args.state_protect = nfs4_sp4_mach_cred_request;
7065 break;
7066
7067 default:
7068 /* unsupported! */
7069 WARN_ON_ONCE(1);
7070 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007071 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007072 }
7073
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007074 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007075 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007076 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007077 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007078
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007079 if (status == 0)
7080 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7081
Chuck Lever177313f2012-05-21 22:44:58 -04007082 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007083 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007084 clp->cl_exchange_flags = res.flags;
7085 /* Client ID is not confirmed */
7086 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7087 clear_bit(NFS4_SESSION_ESTABLISHED,
7088 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007089 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007090 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007091
Chuck Leveracdeb692012-05-21 22:46:16 -04007092 kfree(clp->cl_serverowner);
7093 clp->cl_serverowner = res.server_owner;
7094 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007095
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007096 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007097 kfree(clp->cl_implid);
7098 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007099 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007100
Chuck Lever177313f2012-05-21 22:44:58 -04007101 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007102 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007103 res.server_scope)) {
7104 dprintk("%s: server_scope mismatch detected\n",
7105 __func__);
7106 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007107 kfree(clp->cl_serverscope);
7108 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007109 }
7110
Chuck Lever177313f2012-05-21 22:44:58 -04007111 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007112 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007113 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007114 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007115 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007116
Kinglong Mee6b559702015-07-01 11:54:53 +08007117out_impl_id:
7118 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007119out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007120 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007121out_server_owner:
7122 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007123out:
Chuck Lever177313f2012-05-21 22:44:58 -04007124 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007125 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007126 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007127 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007128 clp->cl_implid->date.seconds,
7129 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007130 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007131 return status;
7132}
7133
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007134/*
7135 * nfs4_proc_exchange_id()
7136 *
7137 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7138 *
7139 * Since the clientid has expired, all compounds using sessions
7140 * associated with the stale clientid will be returning
7141 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7142 * be in some phase of session reset.
7143 *
7144 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7145 */
7146int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7147{
7148 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7149 int status;
7150
7151 /* try SP4_MACH_CRED if krb5i/p */
7152 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7153 authflavor == RPC_AUTH_GSS_KRB5P) {
7154 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7155 if (!status)
7156 return 0;
7157 }
7158
7159 /* try SP4_NONE */
7160 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7161}
7162
Trond Myklebust66245532012-05-25 17:18:09 -04007163static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7164 struct rpc_cred *cred)
7165{
7166 struct rpc_message msg = {
7167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7168 .rpc_argp = clp,
7169 .rpc_cred = cred,
7170 };
7171 int status;
7172
7173 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007174 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007175 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007176 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007177 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7178 return status;
7179}
7180
7181static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7182 struct rpc_cred *cred)
7183{
7184 unsigned int loop;
7185 int ret;
7186
7187 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7188 ret = _nfs4_proc_destroy_clientid(clp, cred);
7189 switch (ret) {
7190 case -NFS4ERR_DELAY:
7191 case -NFS4ERR_CLIENTID_BUSY:
7192 ssleep(1);
7193 break;
7194 default:
7195 return ret;
7196 }
7197 }
7198 return 0;
7199}
7200
7201int nfs4_destroy_clientid(struct nfs_client *clp)
7202{
7203 struct rpc_cred *cred;
7204 int ret = 0;
7205
7206 if (clp->cl_mvops->minor_version < 1)
7207 goto out;
7208 if (clp->cl_exchange_flags == 0)
7209 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007210 if (clp->cl_preserve_clid)
7211 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007212 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007213 ret = nfs4_proc_destroy_clientid(clp, cred);
7214 if (cred)
7215 put_rpccred(cred);
7216 switch (ret) {
7217 case 0:
7218 case -NFS4ERR_STALE_CLIENTID:
7219 clp->cl_exchange_flags = 0;
7220 }
7221out:
7222 return ret;
7223}
7224
Andy Adamson2050f0c2009-04-01 09:22:30 -04007225struct nfs4_get_lease_time_data {
7226 struct nfs4_get_lease_time_args *args;
7227 struct nfs4_get_lease_time_res *res;
7228 struct nfs_client *clp;
7229};
7230
7231static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7232 void *calldata)
7233{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007234 struct nfs4_get_lease_time_data *data =
7235 (struct nfs4_get_lease_time_data *)calldata;
7236
7237 dprintk("--> %s\n", __func__);
7238 /* just setup sequence, do not trigger session recovery
7239 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007240 nfs41_setup_sequence(data->clp->cl_session,
7241 &data->args->la_seq_args,
7242 &data->res->lr_seq_res,
7243 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007244 dprintk("<-- %s\n", __func__);
7245}
7246
7247/*
7248 * Called from nfs4_state_manager thread for session setup, so don't recover
7249 * from sequence operation or clientid errors.
7250 */
7251static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7252{
7253 struct nfs4_get_lease_time_data *data =
7254 (struct nfs4_get_lease_time_data *)calldata;
7255
7256 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007257 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7258 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007259 switch (task->tk_status) {
7260 case -NFS4ERR_DELAY:
7261 case -NFS4ERR_GRACE:
7262 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7263 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7264 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007265 /* fall through */
7266 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007267 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007268 return;
7269 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007270 dprintk("<-- %s\n", __func__);
7271}
7272
Trond Myklebust17280172012-03-11 13:11:00 -04007273static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007274 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7275 .rpc_call_done = nfs4_get_lease_time_done,
7276};
7277
7278int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7279{
7280 struct rpc_task *task;
7281 struct nfs4_get_lease_time_args args;
7282 struct nfs4_get_lease_time_res res = {
7283 .lr_fsinfo = fsinfo,
7284 };
7285 struct nfs4_get_lease_time_data data = {
7286 .args = &args,
7287 .res = &res,
7288 .clp = clp,
7289 };
7290 struct rpc_message msg = {
7291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7292 .rpc_argp = &args,
7293 .rpc_resp = &res,
7294 };
7295 struct rpc_task_setup task_setup = {
7296 .rpc_client = clp->cl_rpcclient,
7297 .rpc_message = &msg,
7298 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007299 .callback_data = &data,
7300 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007301 };
7302 int status;
7303
Chuck Levera9c92d62013-08-09 12:48:18 -04007304 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007305 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007306 dprintk("--> %s\n", __func__);
7307 task = rpc_run_task(&task_setup);
7308
7309 if (IS_ERR(task))
7310 status = PTR_ERR(task);
7311 else {
7312 status = task->tk_status;
7313 rpc_put_task(task);
7314 }
7315 dprintk("<-- %s return %d\n", __func__, status);
7316
7317 return status;
7318}
7319
Andy Adamsonfc931582009-04-01 09:22:31 -04007320/*
7321 * Initialize the values to be used by the client in CREATE_SESSION
7322 * If nfs4_init_session set the fore channel request and response sizes,
7323 * use them.
7324 *
7325 * Set the back channel max_resp_sz_cached to zero to force the client to
7326 * always set csa_cachethis to FALSE because the current implementation
7327 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7328 */
7329static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7330{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007331 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007332
Andy Adamson18aad3d2013-06-26 12:21:49 -04007333 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7334 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7335
Andy Adamsonfc931582009-04-01 09:22:31 -04007336 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007337 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7338 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007339 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007340 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007341
7342 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007343 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007344 __func__,
7345 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007346 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007347
7348 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007349 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7350 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7351 args->bc_attrs.max_resp_sz_cached = 0;
7352 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7353 args->bc_attrs.max_reqs = 1;
7354
7355 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7356 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7357 __func__,
7358 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7359 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7360 args->bc_attrs.max_reqs);
7361}
7362
Trond Myklebust79969dd2015-02-18 11:30:18 -08007363static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7364 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007365{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007366 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007367 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007368
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007369 if (rcvd->max_resp_sz > sent->max_resp_sz)
7370 return -EINVAL;
7371 /*
7372 * Our requested max_ops is the minimum we need; we're not
7373 * prepared to break up compounds into smaller pieces than that.
7374 * So, no point even trying to continue if the server won't
7375 * cooperate:
7376 */
7377 if (rcvd->max_ops < sent->max_ops)
7378 return -EINVAL;
7379 if (rcvd->max_reqs == 0)
7380 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007381 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7382 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007383 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007384}
7385
Trond Myklebust79969dd2015-02-18 11:30:18 -08007386static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7387 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007388{
7389 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007390 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007391
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007392 if (!(res->flags & SESSION4_BACK_CHAN))
7393 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007394 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7395 return -EINVAL;
7396 if (rcvd->max_resp_sz < sent->max_resp_sz)
7397 return -EINVAL;
7398 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7399 return -EINVAL;
7400 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007401 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007402 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007403 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007404 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007405out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007406 return 0;
7407}
Andy Adamson8d353012009-04-01 09:22:32 -04007408
Andy Adamson8d353012009-04-01 09:22:32 -04007409static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007410 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007411{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007412 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007413
Trond Myklebust79969dd2015-02-18 11:30:18 -08007414 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007415 if (ret)
7416 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007417 return nfs4_verify_back_channel_attrs(args, res);
7418}
7419
7420static void nfs4_update_session(struct nfs4_session *session,
7421 struct nfs41_create_session_res *res)
7422{
7423 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007424 /* Mark client id and session as being confirmed */
7425 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7426 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007427 session->flags = res->flags;
7428 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007429 if (res->flags & SESSION4_BACK_CHAN)
7430 memcpy(&session->bc_attrs, &res->bc_attrs,
7431 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007432}
7433
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007434static int _nfs4_proc_create_session(struct nfs_client *clp,
7435 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007436{
7437 struct nfs4_session *session = clp->cl_session;
7438 struct nfs41_create_session_args args = {
7439 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007440 .clientid = clp->cl_clientid,
7441 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007442 .cb_program = NFS4_CALLBACK,
7443 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007444 struct nfs41_create_session_res res;
7445
Andy Adamsonfc931582009-04-01 09:22:31 -04007446 struct rpc_message msg = {
7447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7448 .rpc_argp = &args,
7449 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007450 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007451 };
7452 int status;
7453
7454 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007455 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007456
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007457 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007458 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007459
Trond Myklebust43095d32012-11-20 11:13:12 -05007460 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007461 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007462 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007463 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007464 if (clp->cl_seqid == res.seqid)
7465 clp->cl_seqid++;
7466 if (status)
7467 goto out;
7468 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007469 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007470out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007471 return status;
7472}
7473
7474/*
7475 * Issues a CREATE_SESSION operation to the server.
7476 * It is the responsibility of the caller to verify the session is
7477 * expired before calling this routine.
7478 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007479int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007480{
7481 int status;
7482 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007483 struct nfs4_session *session = clp->cl_session;
7484
7485 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7486
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007487 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007488 if (status)
7489 goto out;
7490
Andy Adamsonaacd5532011-11-09 13:58:21 -05007491 /* Init or reset the session slot tables */
7492 status = nfs4_setup_session_slot_tables(session);
7493 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007494 if (status)
7495 goto out;
7496
7497 ptr = (unsigned *)&session->sess_id.data[0];
7498 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7499 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007500out:
7501 dprintk("<-- %s\n", __func__);
7502 return status;
7503}
7504
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007505/*
7506 * Issue the over-the-wire RPC DESTROY_SESSION.
7507 * The caller must serialize access to this routine.
7508 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007509int nfs4_proc_destroy_session(struct nfs4_session *session,
7510 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007511{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007512 struct rpc_message msg = {
7513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7514 .rpc_argp = session,
7515 .rpc_cred = cred,
7516 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007517 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007518
7519 dprintk("--> nfs4_proc_destroy_session\n");
7520
7521 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007522 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7523 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007524
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007525 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007526 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007527
7528 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007529 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007530 "Session has been destroyed regardless...\n", status);
7531
7532 dprintk("<-- nfs4_proc_destroy_session\n");
7533 return status;
7534}
7535
Trond Myklebust7b38c362012-05-23 13:23:31 -04007536/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007537 * Renew the cl_session lease.
7538 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007539struct nfs4_sequence_data {
7540 struct nfs_client *clp;
7541 struct nfs4_sequence_args args;
7542 struct nfs4_sequence_res res;
7543};
7544
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007545static void nfs41_sequence_release(void *data)
7546{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007547 struct nfs4_sequence_data *calldata = data;
7548 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007549
Alexandros Batsakis71358402010-02-05 03:45:05 -08007550 if (atomic_read(&clp->cl_count) > 1)
7551 nfs4_schedule_state_renewal(clp);
7552 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007553 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007554}
7555
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007556static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7557{
7558 switch(task->tk_status) {
7559 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007560 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7561 return -EAGAIN;
7562 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007563 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007564 }
7565 return 0;
7566}
7567
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007568static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007569{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007570 struct nfs4_sequence_data *calldata = data;
7571 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007572
Trond Myklebust14516c32010-07-31 14:29:06 -04007573 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7574 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007575
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007576 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007577 if (task->tk_status < 0) {
7578 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007579 if (atomic_read(&clp->cl_count) == 1)
7580 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007581
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007582 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7583 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007584 return;
7585 }
7586 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007587 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007588out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007589 dprintk("<-- %s\n", __func__);
7590}
7591
7592static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7593{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007594 struct nfs4_sequence_data *calldata = data;
7595 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007596 struct nfs4_sequence_args *args;
7597 struct nfs4_sequence_res *res;
7598
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007599 args = task->tk_msg.rpc_argp;
7600 res = task->tk_msg.rpc_resp;
7601
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007602 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007603}
7604
7605static const struct rpc_call_ops nfs41_sequence_ops = {
7606 .rpc_call_done = nfs41_sequence_call_done,
7607 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007608 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007609};
7610
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007611static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7612 struct rpc_cred *cred,
7613 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007614{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007615 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007616 struct rpc_message msg = {
7617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7618 .rpc_cred = cred,
7619 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007620 struct rpc_task_setup task_setup_data = {
7621 .rpc_client = clp->cl_rpcclient,
7622 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007623 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007624 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007625 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007626
Alexandros Batsakis71358402010-02-05 03:45:05 -08007627 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007628 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007629 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007630 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007631 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007632 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007633 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007634 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007635 if (is_privileged)
7636 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007637 msg.rpc_argp = &calldata->args;
7638 msg.rpc_resp = &calldata->res;
7639 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007640 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007641
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007642 return rpc_run_task(&task_setup_data);
7643}
7644
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007645static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007646{
7647 struct rpc_task *task;
7648 int ret = 0;
7649
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007650 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007651 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007652 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007653 if (IS_ERR(task))
7654 ret = PTR_ERR(task);
7655 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007656 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007657 dprintk("<-- %s status=%d\n", __func__, ret);
7658 return ret;
7659}
7660
7661static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7662{
7663 struct rpc_task *task;
7664 int ret;
7665
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007666 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007667 if (IS_ERR(task)) {
7668 ret = PTR_ERR(task);
7669 goto out;
7670 }
7671 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007672 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007673 ret = task->tk_status;
7674 rpc_put_task(task);
7675out:
7676 dprintk("<-- %s status=%d\n", __func__, ret);
7677 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007678}
7679
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007680struct nfs4_reclaim_complete_data {
7681 struct nfs_client *clp;
7682 struct nfs41_reclaim_complete_args arg;
7683 struct nfs41_reclaim_complete_res res;
7684};
7685
7686static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7687{
7688 struct nfs4_reclaim_complete_data *calldata = data;
7689
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007690 nfs41_setup_sequence(calldata->clp->cl_session,
7691 &calldata->arg.seq_args,
7692 &calldata->res.seq_res,
7693 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007694}
7695
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007696static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7697{
7698 switch(task->tk_status) {
7699 case 0:
7700 case -NFS4ERR_COMPLETE_ALREADY:
7701 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7702 break;
7703 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007704 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007705 /* fall through */
7706 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007707 return -EAGAIN;
7708 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007709 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007710 }
7711 return 0;
7712}
7713
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007714static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7715{
7716 struct nfs4_reclaim_complete_data *calldata = data;
7717 struct nfs_client *clp = calldata->clp;
7718 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7719
7720 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007721 if (!nfs41_sequence_done(task, res))
7722 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007723
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007724 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007725 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7726 rpc_restart_call_prepare(task);
7727 return;
7728 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007729 dprintk("<-- %s\n", __func__);
7730}
7731
7732static void nfs4_free_reclaim_complete_data(void *data)
7733{
7734 struct nfs4_reclaim_complete_data *calldata = data;
7735
7736 kfree(calldata);
7737}
7738
7739static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7740 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7741 .rpc_call_done = nfs4_reclaim_complete_done,
7742 .rpc_release = nfs4_free_reclaim_complete_data,
7743};
7744
7745/*
7746 * Issue a global reclaim complete.
7747 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007748static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7749 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007750{
7751 struct nfs4_reclaim_complete_data *calldata;
7752 struct rpc_task *task;
7753 struct rpc_message msg = {
7754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007755 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007756 };
7757 struct rpc_task_setup task_setup_data = {
7758 .rpc_client = clp->cl_rpcclient,
7759 .rpc_message = &msg,
7760 .callback_ops = &nfs4_reclaim_complete_call_ops,
7761 .flags = RPC_TASK_ASYNC,
7762 };
7763 int status = -ENOMEM;
7764
7765 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007766 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007767 if (calldata == NULL)
7768 goto out;
7769 calldata->clp = clp;
7770 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007771
Chuck Levera9c92d62013-08-09 12:48:18 -04007772 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007773 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007774 msg.rpc_argp = &calldata->arg;
7775 msg.rpc_resp = &calldata->res;
7776 task_setup_data.callback_data = calldata;
7777 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007778 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007779 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007780 goto out;
7781 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007782 status = nfs4_wait_for_completion_rpc_task(task);
7783 if (status == 0)
7784 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007785 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007786 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007787out:
7788 dprintk("<-- %s status=%d\n", __func__, status);
7789 return status;
7790}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007791
7792static void
7793nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7794{
7795 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007796 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007797 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007798
7799 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007800 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7801 * right now covering the LAYOUTGET we are about to send.
7802 * However, that is not so catastrophic, and there seems
7803 * to be no way to prevent it completely.
7804 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007805 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007806 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007807 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007808 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7809 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007810 &lgp->args.range,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007811 lgp->args.ctx->state)) {
7812 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007813 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007814}
7815
7816static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7817{
7818 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007819 struct inode *inode = lgp->args.inode;
7820 struct nfs_server *server = NFS_SERVER(inode);
7821 struct pnfs_layout_hdr *lo;
7822 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007823 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007824
Boaz Harroshed7e5422014-01-22 20:34:54 +02007825 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007826
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007827 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007828 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007829
7830 switch (task->tk_status) {
7831 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007832 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007833 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007834 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7835 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7836 */
7837 case -NFS4ERR_BADLAYOUT:
7838 goto out_overflow;
7839 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007840 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007841 * (or clients) writing to the same RAID stripe except when
7842 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Boaz Harroshed7e5422014-01-22 20:34:54 +02007843 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007844 case -NFS4ERR_LAYOUTTRYLATER:
Trond Myklebust21b874c2015-08-31 01:19:22 -07007845 if (lgp->args.minlength == 0)
7846 goto out_overflow;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007847 /*
7848 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7849 * existing layout before getting a new one).
7850 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007851 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007852 timeo = rpc_get_timeout(task->tk_client);
7853 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007854 now = jiffies;
7855 if (time_after(giveup, now)) {
7856 unsigned long delay;
7857
7858 /* Delay for:
7859 * - Not less then NFS4_POLL_RETRY_MIN.
7860 * - One last time a jiffie before we give up
7861 * - exponential backoff (time_now minus start_attempt)
7862 */
7863 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7864 min((giveup - now - 1),
7865 now - lgp->args.timestamp));
7866
7867 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7868 __func__, delay);
7869 rpc_delay(task, delay);
Trond Myklebust2259f962015-09-20 13:30:30 -04007870 /* Do not call nfs4_async_handle_error() */
7871 goto out_restart;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007872 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007873 break;
7874 case -NFS4ERR_EXPIRED:
7875 case -NFS4ERR_BAD_STATEID:
7876 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007877 if (nfs4_stateid_match(&lgp->args.stateid,
7878 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007879 spin_unlock(&inode->i_lock);
7880 /* If the open stateid was bad, then recover it. */
7881 state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007882 break;
7883 }
7884 lo = NFS_I(inode)->layout;
7885 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7886 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007887 LIST_HEAD(head);
7888
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007889 /*
7890 * Mark the bad layout state as invalid, then retry
7891 * with the current stateid.
7892 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007893 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustee314c22012-10-01 17:25:48 -07007894 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7895 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007896 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007897 } else
7898 spin_unlock(&inode->i_lock);
7899 goto out_restart;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007900 }
NeilBrown8478eaa2014-09-18 16:09:27 +10007901 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
Trond Myklebust2259f962015-09-20 13:30:30 -04007902 goto out_restart;
Trond Myklebustee314c22012-10-01 17:25:48 -07007903out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007904 dprintk("<-- %s\n", __func__);
Trond Myklebust21b874c2015-08-31 01:19:22 -07007905 return;
Trond Myklebust2259f962015-09-20 13:30:30 -04007906out_restart:
7907 task->tk_status = 0;
7908 rpc_restart_call_prepare(task);
7909 return;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007910out_overflow:
7911 task->tk_status = -EOVERFLOW;
7912 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007913}
7914
Idan Kedar85541162012-08-02 11:47:10 +03007915static size_t max_response_pages(struct nfs_server *server)
7916{
7917 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7918 return nfs_page_array_len(0, max_resp_sz);
7919}
7920
7921static void nfs4_free_pages(struct page **pages, size_t size)
7922{
7923 int i;
7924
7925 if (!pages)
7926 return;
7927
7928 for (i = 0; i < size; i++) {
7929 if (!pages[i])
7930 break;
7931 __free_page(pages[i]);
7932 }
7933 kfree(pages);
7934}
7935
7936static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7937{
7938 struct page **pages;
7939 int i;
7940
7941 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7942 if (!pages) {
7943 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7944 return NULL;
7945 }
7946
7947 for (i = 0; i < size; i++) {
7948 pages[i] = alloc_page(gfp_flags);
7949 if (!pages[i]) {
7950 dprintk("%s: failed to allocate page\n", __func__);
7951 nfs4_free_pages(pages, size);
7952 return NULL;
7953 }
7954 }
7955
7956 return pages;
7957}
7958
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007959static void nfs4_layoutget_release(void *calldata)
7960{
7961 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007962 struct inode *inode = lgp->args.inode;
7963 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007964 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007965
7966 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007967 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007968 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007969 put_nfs_open_context(lgp->args.ctx);
7970 kfree(calldata);
7971 dprintk("<-- %s\n", __func__);
7972}
7973
7974static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7975 .rpc_call_prepare = nfs4_layoutget_prepare,
7976 .rpc_call_done = nfs4_layoutget_done,
7977 .rpc_release = nfs4_layoutget_release,
7978};
7979
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007980struct pnfs_layout_segment *
7981nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007982{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007983 struct inode *inode = lgp->args.inode;
7984 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007985 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007986 struct rpc_task *task;
7987 struct rpc_message msg = {
7988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7989 .rpc_argp = &lgp->args,
7990 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007991 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007992 };
7993 struct rpc_task_setup task_setup_data = {
7994 .rpc_client = server->client,
7995 .rpc_message = &msg,
7996 .callback_ops = &nfs4_layoutget_call_ops,
7997 .callback_data = lgp,
7998 .flags = RPC_TASK_ASYNC,
7999 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008000 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008001 int status = 0;
8002
8003 dprintk("--> %s\n", __func__);
8004
Peng Tao4bd5a982014-11-17 11:05:17 +08008005 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8006 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8007
Idan Kedar85541162012-08-02 11:47:10 +03008008 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8009 if (!lgp->args.layout.pages) {
8010 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008011 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008012 }
8013 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05008014 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03008015
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008016 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008017 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008018 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008019
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008020 task = rpc_run_task(&task_setup_data);
8021 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008022 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008023 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00008024 if (status == 0)
8025 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008026 trace_nfs4_layoutget(lgp->args.ctx,
8027 &lgp->args.range,
8028 &lgp->res.range,
8029 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008030 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8031 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008032 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008033 rpc_put_task(task);
8034 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008035 if (status)
8036 return ERR_PTR(status);
8037 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008038}
8039
Benny Halevycbe82602011-05-22 19:52:37 +03008040static void
8041nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8042{
8043 struct nfs4_layoutreturn *lrp = calldata;
8044
8045 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008046 nfs41_setup_sequence(lrp->clp->cl_session,
8047 &lrp->args.seq_args,
8048 &lrp->res.seq_res,
8049 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008050}
8051
8052static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8053{
8054 struct nfs4_layoutreturn *lrp = calldata;
8055 struct nfs_server *server;
8056
8057 dprintk("--> %s\n", __func__);
8058
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008059 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008060 return;
8061
8062 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008063 switch (task->tk_status) {
8064 default:
8065 task->tk_status = 0;
8066 case 0:
8067 break;
8068 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008069 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008070 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008071 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008072 return;
8073 }
Benny Halevycbe82602011-05-22 19:52:37 +03008074 dprintk("<-- %s\n", __func__);
8075}
8076
8077static void nfs4_layoutreturn_release(void *calldata)
8078{
8079 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008080 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008081 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008082
8083 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008084 spin_lock(&lo->plh_inode->i_lock);
8085 if (lrp->res.lrs_present)
8086 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008087 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
Tom Haynesd67ae822014-12-11 17:02:04 -05008088 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008089 lo->plh_block_lgets--;
8090 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008091 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008092 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008093 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008094 kfree(calldata);
8095 dprintk("<-- %s\n", __func__);
8096}
8097
8098static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8099 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8100 .rpc_call_done = nfs4_layoutreturn_done,
8101 .rpc_release = nfs4_layoutreturn_release,
8102};
8103
Peng Tao6c166052014-11-17 09:30:40 +08008104int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008105{
8106 struct rpc_task *task;
8107 struct rpc_message msg = {
8108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8109 .rpc_argp = &lrp->args,
8110 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008111 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008112 };
8113 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008114 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008115 .rpc_message = &msg,
8116 .callback_ops = &nfs4_layoutreturn_call_ops,
8117 .callback_data = lrp,
8118 };
Peng Tao6c166052014-11-17 09:30:40 +08008119 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008120
8121 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008122 if (!sync) {
8123 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8124 if (!lrp->inode) {
8125 nfs4_layoutreturn_release(lrp);
8126 return -EAGAIN;
8127 }
8128 task_setup_data.flags |= RPC_TASK_ASYNC;
8129 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008130 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008131 task = rpc_run_task(&task_setup_data);
8132 if (IS_ERR(task))
8133 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008134 if (sync)
8135 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008136 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008137 dprintk("<-- %s status=%d\n", __func__, status);
8138 rpc_put_task(task);
8139 return status;
8140}
8141
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008142static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008143_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8144 struct pnfs_device *pdev,
8145 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008146{
8147 struct nfs4_getdeviceinfo_args args = {
8148 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008149 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8150 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008151 };
8152 struct nfs4_getdeviceinfo_res res = {
8153 .pdev = pdev,
8154 };
8155 struct rpc_message msg = {
8156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8157 .rpc_argp = &args,
8158 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008159 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008160 };
8161 int status;
8162
8163 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008165 if (res.notification & ~args.notify_types)
8166 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008167 if (res.notification != args.notify_types)
8168 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008169
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008170 dprintk("<-- %s status=%d\n", __func__, status);
8171
8172 return status;
8173}
8174
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008175int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8176 struct pnfs_device *pdev,
8177 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008178{
8179 struct nfs4_exception exception = { };
8180 int err;
8181
8182 do {
8183 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008184 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008185 &exception);
8186 } while (exception.retry);
8187 return err;
8188}
8189EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8190
Andy Adamson863a3c62011-03-23 13:27:54 +00008191static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8192{
8193 struct nfs4_layoutcommit_data *data = calldata;
8194 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008195 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008196
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008197 nfs41_setup_sequence(session,
8198 &data->args.seq_args,
8199 &data->res.seq_res,
8200 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008201}
8202
8203static void
8204nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8205{
8206 struct nfs4_layoutcommit_data *data = calldata;
8207 struct nfs_server *server = NFS_SERVER(data->args.inode);
8208
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008209 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008210 return;
8211
8212 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008213 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8214 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8215 case -NFS4ERR_BADLAYOUT: /* no layout */
8216 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008217 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008218 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008219 break;
8220 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008221 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008222 rpc_restart_call_prepare(task);
8223 return;
8224 }
8225 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008226}
8227
8228static void nfs4_layoutcommit_release(void *calldata)
8229{
8230 struct nfs4_layoutcommit_data *data = calldata;
8231
Andy Adamsondb29c082011-07-30 20:52:38 -04008232 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008233 nfs_post_op_update_inode_force_wcc(data->args.inode,
8234 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008235 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008236 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008237 kfree(data);
8238}
8239
8240static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8241 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8242 .rpc_call_done = nfs4_layoutcommit_done,
8243 .rpc_release = nfs4_layoutcommit_release,
8244};
8245
8246int
Andy Adamsonef311532011-03-12 02:58:10 -05008247nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008248{
8249 struct rpc_message msg = {
8250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8251 .rpc_argp = &data->args,
8252 .rpc_resp = &data->res,
8253 .rpc_cred = data->cred,
8254 };
8255 struct rpc_task_setup task_setup_data = {
8256 .task = &data->task,
8257 .rpc_client = NFS_CLIENT(data->args.inode),
8258 .rpc_message = &msg,
8259 .callback_ops = &nfs4_layoutcommit_ops,
8260 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008261 };
8262 struct rpc_task *task;
8263 int status = 0;
8264
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008265 dprintk("NFS: initiating layoutcommit call. sync %d "
8266 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008267 data->args.lastbytewritten,
8268 data->args.inode->i_ino);
8269
Trond Myklebust472e2592015-02-05 16:50:30 -05008270 if (!sync) {
8271 data->inode = nfs_igrab_and_active(data->args.inode);
8272 if (data->inode == NULL) {
8273 nfs4_layoutcommit_release(data);
8274 return -EAGAIN;
8275 }
8276 task_setup_data.flags = RPC_TASK_ASYNC;
8277 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008278 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008279 task = rpc_run_task(&task_setup_data);
8280 if (IS_ERR(task))
8281 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008282 if (sync)
8283 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008284 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008285 dprintk("%s: status %d\n", __func__, status);
8286 rpc_put_task(task);
8287 return status;
8288}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008289
Andy Adamson97431202013-08-08 10:57:56 -04008290/**
8291 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8292 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8293 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008294static int
8295_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008296 struct nfs_fsinfo *info,
8297 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008298{
8299 struct nfs41_secinfo_no_name_args args = {
8300 .style = SECINFO_STYLE_CURRENT_FH,
8301 };
8302 struct nfs4_secinfo_res res = {
8303 .flavors = flavors,
8304 };
8305 struct rpc_message msg = {
8306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8307 .rpc_argp = &args,
8308 .rpc_resp = &res,
8309 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008310 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008311 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008312 int status;
8313
8314 if (use_integrity) {
8315 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008316 cred = nfs4_get_clid_cred(server->nfs_client);
8317 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008318 }
8319
8320 dprintk("--> %s\n", __func__);
8321 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8322 &res.seq_res, 0);
8323 dprintk("<-- %s status=%d\n", __func__, status);
8324
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008325 if (cred)
8326 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008327
8328 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008329}
8330
8331static int
8332nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8333 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8334{
8335 struct nfs4_exception exception = { };
8336 int err;
8337 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008338 /* first try using integrity protection */
8339 err = -NFS4ERR_WRONGSEC;
8340
8341 /* try to use integrity protection with machine cred */
8342 if (_nfs4_is_integrity_protected(server->nfs_client))
8343 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8344 flavors, true);
8345
8346 /*
8347 * if unable to use integrity protection, or SECINFO with
8348 * integrity protection returns NFS4ERR_WRONGSEC (which is
8349 * disallowed by spec, but exists in deployed servers) use
8350 * the current filesystem's rpc_client and the user cred.
8351 */
8352 if (err == -NFS4ERR_WRONGSEC)
8353 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8354 flavors, false);
8355
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008356 switch (err) {
8357 case 0:
8358 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008359 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008360 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008361 default:
8362 err = nfs4_handle_exception(server, err, &exception);
8363 }
8364 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008365out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008366 return err;
8367}
8368
8369static int
8370nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8371 struct nfs_fsinfo *info)
8372{
8373 int err;
8374 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008375 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008376 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008377 struct nfs4_secinfo4 *secinfo;
8378 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008379
8380 page = alloc_page(GFP_KERNEL);
8381 if (!page) {
8382 err = -ENOMEM;
8383 goto out;
8384 }
8385
8386 flavors = page_address(page);
8387 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8388
8389 /*
8390 * Fall back on "guess and check" method if
8391 * the server doesn't support SECINFO_NO_NAME
8392 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008393 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008394 err = nfs4_find_root_sec(server, fhandle, info);
8395 goto out_freepage;
8396 }
8397 if (err)
8398 goto out_freepage;
8399
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008400 for (i = 0; i < flavors->num_flavors; i++) {
8401 secinfo = &flavors->flavors[i];
8402
8403 switch (secinfo->flavor) {
8404 case RPC_AUTH_NULL:
8405 case RPC_AUTH_UNIX:
8406 case RPC_AUTH_GSS:
8407 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8408 &secinfo->flavor_info);
8409 break;
8410 default:
8411 flavor = RPC_AUTH_MAXFLAVOR;
8412 break;
8413 }
8414
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008415 if (!nfs_auth_info_match(&server->auth_info, flavor))
8416 flavor = RPC_AUTH_MAXFLAVOR;
8417
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008418 if (flavor != RPC_AUTH_MAXFLAVOR) {
8419 err = nfs4_lookup_root_sec(server, fhandle,
8420 info, flavor);
8421 if (!err)
8422 break;
8423 }
8424 }
8425
8426 if (flavor == RPC_AUTH_MAXFLAVOR)
8427 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008428
8429out_freepage:
8430 put_page(page);
8431 if (err == -EACCES)
8432 return -EPERM;
8433out:
8434 return err;
8435}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008436
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008437static int _nfs41_test_stateid(struct nfs_server *server,
8438 nfs4_stateid *stateid,
8439 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008440{
8441 int status;
8442 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008443 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008444 };
8445 struct nfs41_test_stateid_res res;
8446 struct rpc_message msg = {
8447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8448 .rpc_argp = &args,
8449 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008450 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008451 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008452 struct rpc_clnt *rpc_client = server->client;
8453
8454 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8455 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008456
Chuck Lever38527b12012-07-11 16:30:23 -04008457 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008458 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008459 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008460 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008461 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008462 if (status != NFS_OK) {
8463 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008464 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008465 }
8466 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008467 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008468}
8469
Chuck Lever38527b12012-07-11 16:30:23 -04008470/**
8471 * nfs41_test_stateid - perform a TEST_STATEID operation
8472 *
8473 * @server: server / transport on which to perform the operation
8474 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008475 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008476 *
8477 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8478 * Otherwise a negative NFS4ERR value is returned if the operation
8479 * failed or the state ID is not currently valid.
8480 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008481static int nfs41_test_stateid(struct nfs_server *server,
8482 nfs4_stateid *stateid,
8483 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008484{
8485 struct nfs4_exception exception = { };
8486 int err;
8487 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008488 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008489 if (err != -NFS4ERR_DELAY)
8490 break;
8491 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008492 } while (exception.retry);
8493 return err;
8494}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008495
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008496struct nfs_free_stateid_data {
8497 struct nfs_server *server;
8498 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008499 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008500};
8501
8502static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8503{
8504 struct nfs_free_stateid_data *data = calldata;
8505 nfs41_setup_sequence(nfs4_get_session(data->server),
8506 &data->args.seq_args,
8507 &data->res.seq_res,
8508 task);
8509}
8510
8511static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8512{
8513 struct nfs_free_stateid_data *data = calldata;
8514
8515 nfs41_sequence_done(task, &data->res.seq_res);
8516
8517 switch (task->tk_status) {
8518 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008519 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008520 rpc_restart_call_prepare(task);
8521 }
8522}
8523
8524static void nfs41_free_stateid_release(void *calldata)
8525{
8526 kfree(calldata);
8527}
8528
Trond Myklebust17f26b12013-08-21 15:48:42 -04008529static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008530 .rpc_call_prepare = nfs41_free_stateid_prepare,
8531 .rpc_call_done = nfs41_free_stateid_done,
8532 .rpc_release = nfs41_free_stateid_release,
8533};
8534
8535static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8536 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008537 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008538 bool privileged)
8539{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008540 struct rpc_message msg = {
8541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008542 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008543 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008544 struct rpc_task_setup task_setup = {
8545 .rpc_client = server->client,
8546 .rpc_message = &msg,
8547 .callback_ops = &nfs41_free_stateid_ops,
8548 .flags = RPC_TASK_ASYNC,
8549 };
8550 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008551
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008552 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8553 &task_setup.rpc_client, &msg);
8554
Chuck Lever38527b12012-07-11 16:30:23 -04008555 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008556 data = kmalloc(sizeof(*data), GFP_NOFS);
8557 if (!data)
8558 return ERR_PTR(-ENOMEM);
8559 data->server = server;
8560 nfs4_stateid_copy(&data->args.stateid, stateid);
8561
8562 task_setup.callback_data = data;
8563
8564 msg.rpc_argp = &data->args;
8565 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008566 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008567 if (privileged)
8568 nfs4_set_sequence_privileged(&data->args.seq_args);
8569
8570 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008571}
8572
Chuck Lever38527b12012-07-11 16:30:23 -04008573/**
8574 * nfs41_free_stateid - perform a FREE_STATEID operation
8575 *
8576 * @server: server / transport on which to perform the operation
8577 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008578 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008579 *
8580 * Returns NFS_OK if the server freed "stateid". Otherwise a
8581 * negative NFS4ERR value is returned.
8582 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008583static int nfs41_free_stateid(struct nfs_server *server,
8584 nfs4_stateid *stateid,
8585 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008586{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008587 struct rpc_task *task;
8588 int ret;
8589
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008590 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008591 if (IS_ERR(task))
8592 return PTR_ERR(task);
8593 ret = rpc_wait_for_completion_task(task);
8594 if (!ret)
8595 ret = task->tk_status;
8596 rpc_put_task(task);
8597 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008598}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008599
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008600static void
8601nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008602{
8603 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008604 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008605
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008606 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008607 nfs4_free_lock_state(server, lsp);
8608 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008609 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008610 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008611}
8612
Trond Myklebust36281ca2012-03-04 18:13:56 -05008613static bool nfs41_match_stateid(const nfs4_stateid *s1,
8614 const nfs4_stateid *s2)
8615{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008616 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008617 return false;
8618
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008619 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008620 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008621 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008622 return true;
8623
8624 return false;
8625}
8626
Andy Adamson557134a2009-04-01 09:21:53 -04008627#endif /* CONFIG_NFS_V4_1 */
8628
Trond Myklebust36281ca2012-03-04 18:13:56 -05008629static bool nfs4_match_stateid(const nfs4_stateid *s1,
8630 const nfs4_stateid *s2)
8631{
Trond Myklebustf597c532012-03-04 18:13:56 -05008632 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008633}
8634
8635
Trond Myklebust17280172012-03-11 13:11:00 -04008636static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008637 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008638 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 .recover_open = nfs4_open_reclaim,
8640 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008641 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008642 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643};
8644
Andy Adamson591d71c2009-04-01 09:22:47 -04008645#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008646static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008647 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8648 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8649 .recover_open = nfs4_open_reclaim,
8650 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008651 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008652 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008653 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008654};
8655#endif /* CONFIG_NFS_V4_1 */
8656
Trond Myklebust17280172012-03-11 13:11:00 -04008657static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008658 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008659 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008660 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008662 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663};
8664
Andy Adamson591d71c2009-04-01 09:22:47 -04008665#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008666static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008667 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8668 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008669 .recover_open = nfs41_open_expired,
8670 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008671 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008672};
8673#endif /* CONFIG_NFS_V4_1 */
8674
Trond Myklebust17280172012-03-11 13:11:00 -04008675static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008676 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008677 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008678 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008679};
8680
8681#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008682static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008683 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008684 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008685 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008686};
8687#endif
8688
Chuck Leverec011fe2013-10-17 14:12:39 -04008689static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008690 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008691 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008692};
8693
8694#if defined(CONFIG_NFS_V4_1)
8695static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008696 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008697 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008698};
8699#endif /* CONFIG_NFS_V4_1 */
8700
Trond Myklebust97dc1352010-06-16 09:52:26 -04008701static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8702 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008703 .init_caps = NFS_CAP_READDIRPLUS
8704 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008705 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008706 .init_client = nfs40_init_client,
8707 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008708 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008709 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008710 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008711 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008712 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008713 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8714 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8715 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008716 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008717};
8718
8719#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008720static struct nfs_seqid *
8721nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8722{
8723 return NULL;
8724}
8725
Trond Myklebust97dc1352010-06-16 09:52:26 -04008726static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8727 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008728 .init_caps = NFS_CAP_READDIRPLUS
8729 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008730 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008731 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008732 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008733 .init_client = nfs41_init_client,
8734 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008735 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008736 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008737 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008738 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008739 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008740 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8741 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8742 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008743 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008744};
8745#endif
8746
Steve Dickson42c2c422013-05-22 12:50:38 -04008747#if defined(CONFIG_NFS_V4_2)
8748static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8749 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008750 .init_caps = NFS_CAP_READDIRPLUS
8751 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008752 | NFS_CAP_POSIX_LOCK
8753 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008754 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008755 | NFS_CAP_ALLOCATE
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008756 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008757 | NFS_CAP_SEEK
8758 | NFS_CAP_LAYOUTSTATS,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008759 .init_client = nfs41_init_client,
8760 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008761 .match_stateid = nfs41_match_stateid,
8762 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008763 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008764 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008765 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008766 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8767 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8768 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008769 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008770};
8771#endif
8772
Trond Myklebust97dc1352010-06-16 09:52:26 -04008773const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8774 [0] = &nfs_v4_0_minor_ops,
8775#if defined(CONFIG_NFS_V4_1)
8776 [1] = &nfs_v4_1_minor_ops,
8777#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008778#if defined(CONFIG_NFS_V4_2)
8779 [2] = &nfs_v4_2_minor_ops,
8780#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008781};
8782
Trond Myklebust17f26b12013-08-21 15:48:42 -04008783static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008784 .create = nfs_create,
8785 .lookup = nfs_lookup,
8786 .atomic_open = nfs_atomic_open,
8787 .link = nfs_link,
8788 .unlink = nfs_unlink,
8789 .symlink = nfs_symlink,
8790 .mkdir = nfs_mkdir,
8791 .rmdir = nfs_rmdir,
8792 .mknod = nfs_mknod,
8793 .rename = nfs_rename,
8794 .permission = nfs_permission,
8795 .getattr = nfs_getattr,
8796 .setattr = nfs_setattr,
8797 .getxattr = generic_getxattr,
8798 .setxattr = generic_setxattr,
8799 .listxattr = generic_listxattr,
8800 .removexattr = generic_removexattr,
8801};
8802
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008803static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008804 .permission = nfs_permission,
8805 .getattr = nfs_getattr,
8806 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008807 .getxattr = generic_getxattr,
8808 .setxattr = generic_setxattr,
8809 .listxattr = generic_listxattr,
8810 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008811};
8812
David Howells509de812006-08-22 20:06:11 -04008813const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814 .version = 4, /* protocol version */
8815 .dentry_ops = &nfs4_dentry_operations,
8816 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008817 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008818 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008820 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008821 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822 .getattr = nfs4_proc_getattr,
8823 .setattr = nfs4_proc_setattr,
8824 .lookup = nfs4_proc_lookup,
8825 .access = nfs4_proc_access,
8826 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 .create = nfs4_proc_create,
8828 .remove = nfs4_proc_remove,
8829 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008830 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008832 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008833 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008834 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 .link = nfs4_proc_link,
8836 .symlink = nfs4_proc_symlink,
8837 .mkdir = nfs4_proc_mkdir,
8838 .rmdir = nfs4_proc_remove,
8839 .readdir = nfs4_proc_readdir,
8840 .mknod = nfs4_proc_mknod,
8841 .statfs = nfs4_proc_statfs,
8842 .fsinfo = nfs4_proc_fsinfo,
8843 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008844 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008846 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008848 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008850 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008852 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008853 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008855 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008856 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008857 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008858 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008859 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008860 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008861 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008862 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008863 .create_server = nfs4_create_server,
8864 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865};
8866
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008867static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8868 .prefix = XATTR_NAME_NFSV4_ACL,
8869 .list = nfs4_xattr_list_nfs4_acl,
8870 .get = nfs4_xattr_get_nfs4_acl,
8871 .set = nfs4_xattr_set_nfs4_acl,
8872};
8873
8874const struct xattr_handler *nfs4_xattr_handlers[] = {
8875 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008876#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8877 &nfs4_xattr_nfs4_label_handler,
8878#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008879 NULL
8880};
8881
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882/*
8883 * Local variables:
8884 * c-basic-offset: 8
8885 * End:
8886 */