blob: aaf2c1324be7ded0c6115538e0db380c254ea936 [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>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.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"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
87 struct rpc_cred *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
89 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050098 case -NFS4ERR_LAYOUTTRYLATER:
99 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000101 case -NFS4ERR_WRONGSEC:
102 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
105 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400106 case -NFS4ERR_SHARE_DENIED:
107 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400108 case -NFS4ERR_MINOR_VERS_MISMATCH:
109 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400110 case -NFS4ERR_ACCESS:
111 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400112 case -NFS4ERR_FILE_OPEN:
113 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400114 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700116 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400119 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122/*
123 * This is our standard bitmap for GETATTR requests.
124 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400125const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 FATTR4_WORD0_TYPE
127 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_SIZE
129 | FATTR4_WORD0_FSID
130 | FATTR4_WORD0_FILEID,
131 FATTR4_WORD1_MODE
132 | FATTR4_WORD1_NUMLINKS
133 | FATTR4_WORD1_OWNER
134 | FATTR4_WORD1_OWNER_GROUP
135 | FATTR4_WORD1_RAWDEV
136 | FATTR4_WORD1_SPACE_USED
137 | FATTR4_WORD1_TIME_ACCESS
138 | FATTR4_WORD1_TIME_METADATA
139 | FATTR4_WORD1_TIME_MODIFY
140};
141
Trond Myklebust1549210f2012-06-05 09:16:47 -0400142static const u32 nfs4_pnfs_open_bitmap[3] = {
143 FATTR4_WORD0_TYPE
144 | FATTR4_WORD0_CHANGE
145 | FATTR4_WORD0_SIZE
146 | FATTR4_WORD0_FSID
147 | FATTR4_WORD0_FILEID,
148 FATTR4_WORD1_MODE
149 | FATTR4_WORD1_NUMLINKS
150 | FATTR4_WORD1_OWNER
151 | FATTR4_WORD1_OWNER_GROUP
152 | FATTR4_WORD1_RAWDEV
153 | FATTR4_WORD1_SPACE_USED
154 | FATTR4_WORD1_TIME_ACCESS
155 | FATTR4_WORD1_TIME_METADATA
156 | FATTR4_WORD1_TIME_MODIFY,
157 FATTR4_WORD2_MDSTHRESHOLD
158};
159
Andy Adamsone23008e2012-10-02 21:07:32 -0400160static const u32 nfs4_open_noattr_bitmap[3] = {
161 FATTR4_WORD0_TYPE
162 | FATTR4_WORD0_CHANGE
163 | FATTR4_WORD0_FILEID,
164};
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166const u32 nfs4_statfs_bitmap[2] = {
167 FATTR4_WORD0_FILES_AVAIL
168 | FATTR4_WORD0_FILES_FREE
169 | FATTR4_WORD0_FILES_TOTAL,
170 FATTR4_WORD1_SPACE_AVAIL
171 | FATTR4_WORD1_SPACE_FREE
172 | FATTR4_WORD1_SPACE_TOTAL
173};
174
Trond Myklebust4ce79712005-06-22 17:16:21 +0000175const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 FATTR4_WORD0_MAXLINK
177 | FATTR4_WORD0_MAXNAME,
178 0
179};
180
Fred Isamandae100c2011-07-30 20:52:37 -0400181const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 | FATTR4_WORD0_MAXREAD
183 | FATTR4_WORD0_MAXWRITE
184 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700185 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400186 | FATTR4_WORD1_FS_LAYOUT_TYPES,
187 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Manoj Naik830b8e32006-06-09 09:34:25 -0400190const u32 nfs4_fs_locations_bitmap[2] = {
191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID
196 | FATTR4_WORD0_FS_LOCATIONS,
197 FATTR4_WORD1_MODE
198 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER
200 | FATTR4_WORD1_OWNER_GROUP
201 | FATTR4_WORD1_RAWDEV
202 | FATTR4_WORD1_SPACE_USED
203 | FATTR4_WORD1_TIME_ACCESS
204 | FATTR4_WORD1_TIME_METADATA
205 | FATTR4_WORD1_TIME_MODIFY
206 | FATTR4_WORD1_MOUNTED_ON_FILEID
207};
208
Al Virobc4785c2006-10-19 23:28:51 -0700209static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct nfs4_readdir_arg *readdir)
211{
Al Viro0dbb4c62006-10-19 23:28:49 -0700212 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000215 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
217 return;
218 }
219
220 readdir->cookie = 0;
221 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
222 if (cookie == 2)
223 return;
224
225 /*
226 * NFSv4 servers do not return entries for '.' and '..'
227 * Therefore, we fake these entries here. We let '.'
228 * have cookie 0 and '..' have cookie 1. Note that
229 * when talking to the server, we always send cookie 0
230 * instead of 1 or 2.
231 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800232 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (cookie == 0) {
235 *p++ = xdr_one; /* next */
236 *p++ = xdr_zero; /* cookie, first word */
237 *p++ = xdr_one; /* cookie, second word */
238 *p++ = xdr_one; /* entry len */
239 memcpy(p, ".\0\0\0", 4); /* entry */
240 p++;
241 *p++ = xdr_one; /* bitmap length */
242 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
243 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400244 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 *p++ = xdr_one; /* next */
248 *p++ = xdr_zero; /* cookie, first word */
249 *p++ = xdr_two; /* cookie, second word */
250 *p++ = xdr_two; /* entry len */
251 memcpy(p, "..\0\0", 4); /* entry */
252 p++;
253 *p++ = xdr_one; /* bitmap length */
254 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
255 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400256 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 readdir->pgbase = (char *)p - (char *)start;
259 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800260 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Trond Myklebust65de8722008-12-23 15:21:44 -0500263static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
264{
265 int res = 0;
266
267 might_sleep();
268
269 if (*timeout <= 0)
270 *timeout = NFS4_POLL_RETRY_MIN;
271 if (*timeout > NFS4_POLL_RETRY_MAX)
272 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100273 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500274 if (fatal_signal_pending(current))
275 res = -ERESTARTSYS;
276 *timeout <<= 1;
277 return res;
278}
279
280/* This is the error handling routine for processes that are allowed
281 * to sleep.
282 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800283static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500284{
285 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500286 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500287 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500288 int ret = errorcode;
289
290 exception->retry = 0;
291 switch(errorcode) {
292 case 0:
293 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500294 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400295 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400296 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500297 exception->retry = 1;
298 return 0;
299 }
300 if (state == NULL)
301 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400302 ret = nfs4_schedule_stateid_recovery(server, state);
303 if (ret < 0)
304 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500305 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500306 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500307 case -NFS4ERR_ADMIN_REVOKED:
308 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400309 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
310 nfs_remove_bad_delegation(inode);
311 exception->retry = 1;
312 break;
313 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500314 if (state == NULL)
315 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400316 ret = nfs4_schedule_stateid_recovery(server, state);
317 if (ret < 0)
318 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500319 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400320 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400321 if (state != NULL) {
322 ret = nfs4_schedule_stateid_recovery(server, state);
323 if (ret < 0)
324 break;
325 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500327 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500330#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
339 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400341 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500342#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
346 * fail
347 */
348 ret = -EBUSY;
349 break;
350 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_GRACE:
352 case -NFS4ERR_DELAY:
353 ret = nfs4_delay(server->client, &exception->timeout);
354 if (ret != 0)
355 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400356 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500357 case -NFS4ERR_OLD_STATEID:
358 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800359 break;
360 case -NFS4ERR_BADOWNER:
361 /* The following works around a Linux server bug! */
362 case -NFS4ERR_BADNAME:
363 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
364 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
365 exception->retry = 1;
366 printk(KERN_WARNING "NFS: v4 server %s "
367 "does not accept raw "
368 "uid/gids. "
369 "Reenabling the idmapper.\n",
370 server->nfs_client->cl_hostname);
371 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500372 }
373 /* We failed to handle the error */
374 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500376 ret = nfs4_wait_clnt_recover(clp);
377 if (ret == 0)
378 exception->retry = 1;
379 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380}
381
382
Trond Myklebust452e9352010-07-31 14:29:06 -0400383static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spin_lock(&clp->cl_lock);
386 if (time_before(clp->cl_last_renewal,timestamp))
387 clp->cl_last_renewal = timestamp;
388 spin_unlock(&clp->cl_lock);
389}
390
Trond Myklebust452e9352010-07-31 14:29:06 -0400391static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
392{
393 do_renew_lease(server->nfs_client, timestamp);
394}
395
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400396#if defined(CONFIG_NFS_V4_1)
397
Trond Myklebustd185a332010-06-16 09:52:25 -0400398static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400399{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500400 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400401 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500402 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400403
Benny Halevydfb4f3092010-09-24 09:17:01 -0400404 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400405 /* just wake up the next guy waiting since
406 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500407 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500408 return;
Andy Adamson13615872009-04-01 09:22:17 -0400409 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500410 tbl = res->sr_slot->table;
411 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500412
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500413 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500414 /* Be nice to the server: try to ensure that the last transmitted
415 * value for highest_user_slotid <= target_highest_slotid
416 */
417 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
418 send_new_highest_used_slotid = true;
419
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500420 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
421 send_new_highest_used_slotid = false;
422 goto out_unlock;
423 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500424 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500425
426 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
427 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500428out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400430 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500431 if (send_new_highest_used_slotid)
432 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400433}
434
Trond Myklebust14516c32010-07-31 14:29:06 -0400435static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500437 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500438 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500440 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500441 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442
Bryan Schumaker468f8612011-04-18 15:57:32 -0400443 /* don't increment the sequence number if the task wasn't sent */
444 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445 goto out;
446
Trond Myklebust933602e2012-11-16 12:12:38 -0500447 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500448 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500449
Trond Myklebustac20d162012-12-15 15:36:07 -0500450 if (slot->interrupted) {
451 slot->interrupted = 0;
452 interrupted = true;
453 }
454
Andy Adamson691daf32009-12-04 15:55:39 -0500455 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 switch (res->sr_status) {
457 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400458 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500459 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500460 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500461 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500462 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500463 if (res->sr_status_flags != 0)
464 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500465 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400466 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500467 case 1:
468 /*
469 * sr_status remains 1 if an RPC level error occurred.
470 * The server may or may not have processed the sequence
471 * operation..
472 * Mark the slot as having hosted an interrupted RPC call.
473 */
474 slot->interrupted = 1;
475 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 case -NFS4ERR_DELAY:
477 /* The server detected a resend of the RPC call and
478 * returned NFS4ERR_DELAY as per Section 2.10.6.2
479 * of RFC5661.
480 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500481 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500483 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500484 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500486 case -NFS4ERR_BADSLOT:
487 /*
488 * The slot id we used was probably retired. Try again
489 * using a different slot id.
490 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500491 goto retry_nowait;
492 case -NFS4ERR_SEQ_MISORDERED:
493 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500494 * Was the last operation on this sequence interrupted?
495 * If so, retry after bumping the sequence number.
496 */
497 if (interrupted) {
498 ++slot->seq_nr;
499 goto retry_nowait;
500 }
501 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500502 * Could this slot have been previously retired?
503 * If so, then the server may be expecting seq_nr = 1!
504 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500505 if (slot->seq_nr != 1) {
506 slot->seq_nr = 1;
507 goto retry_nowait;
508 }
509 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500510 case -NFS4ERR_SEQ_FALSE_RETRY:
511 ++slot->seq_nr;
512 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 default:
514 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500515 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400516 }
517out:
518 /* The session may be reset by one of the error handlers. */
519 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400520 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500521 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500522retry_nowait:
523 if (rpc_restart_call_prepare(task)) {
524 task->tk_status = 0;
525 ret = 0;
526 }
527 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400528out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400529 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 goto out;
531 rpc_delay(task, NFS4_POLL_RETRY_MAX);
532 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533}
534
Trond Myklebust14516c32010-07-31 14:29:06 -0400535static int nfs4_sequence_done(struct rpc_task *task,
536 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400537{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500538 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400539 return 1;
540 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400541}
542
Trond Myklebust9d12b212012-01-17 22:04:25 -0500543static void nfs41_init_sequence(struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res, int cache_reply)
545{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500546 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500547 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400548 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500549 if (cache_reply)
550 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500551 res->sr_slot = NULL;
552}
553
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400554static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
555{
556 args->sa_privileged = 1;
557}
558
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000559int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct nfs4_sequence_args *args,
561 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 struct rpc_task *task)
563{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564 struct nfs4_slot *slot;
565 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566
567 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400569 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400570 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 tbl = &session->fc_slot_table;
573
Trond Myklebust69d206b2012-11-22 13:21:02 -0500574 task->tk_timeout = 0;
575
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400577 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400578 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500579 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500580 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400581 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400582 }
583
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500584 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500585 if (IS_ERR(slot)) {
586 /* If out of memory, try again in 1/4 second */
587 if (slot == ERR_PTR(-ENOMEM))
588 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400590 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 }
592 spin_unlock(&tbl->slot_tbl_lock);
593
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500594 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500596 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
597 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
Benny Halevydfb4f3092010-09-24 09:17:01 -0400599 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500600 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400601 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 /*
603 * sr_status is only set in decode_sequence, and so will remain
604 * set to 1 if an rpc level failure occurs.
605 */
606 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400607out_success:
608 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400610out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400611 /* Privileged tasks are queued with top priority */
612 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400613 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
614 NULL, RPC_PRIORITY_PRIVILEGED);
615 else
616 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400617 spin_unlock(&tbl->slot_tbl_lock);
618 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000620EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621
Trond Myklebust035168a2010-06-16 09:52:26 -0400622int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 struct nfs4_sequence_args *args,
624 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 struct rpc_task *task)
626{
Trond Myklebust035168a2010-06-16 09:52:26 -0400627 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 int ret = 0;
629
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400630 if (session == NULL) {
631 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400633 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400634
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400636 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400638
Trond Myklebust9d12b212012-01-17 22:04:25 -0500639 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640out:
641 dprintk("<-- %s status=%d\n", __func__, ret);
642 return ret;
643}
644
645struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400646 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 struct nfs4_sequence_args *seq_args;
648 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649};
650
651static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
652{
653 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400654 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655
Trond Myklebust035168a2010-06-16 09:52:26 -0400656 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
657
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400658 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659}
660
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
662{
663 struct nfs41_call_sync_data *data = calldata;
664
Trond Myklebust14516c32010-07-31 14:29:06 -0400665 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400666}
667
Trond Myklebust17280172012-03-11 13:11:00 -0400668static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400670 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671};
672
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
674 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct rpc_message *msg,
676 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400677 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678{
679 int ret;
680 struct rpc_task *task;
681 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400682 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 .seq_args = args,
684 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 };
686 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000687 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 .rpc_message = msg,
689 .callback_ops = &nfs41_call_sync_ops,
690 .callback_data = &data
691 };
692
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 task = rpc_run_task(&task_setup);
694 if (IS_ERR(task))
695 ret = PTR_ERR(task);
696 else {
697 ret = task->tk_status;
698 rpc_put_task(task);
699 }
700 return ret;
701}
702
Trond Myklebustdf896452010-06-16 09:52:26 -0400703#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400704static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500705void nfs41_init_sequence(struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res, int cache_reply)
707{
708}
709
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400710static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
711{
712}
713
714
Trond Myklebust14516c32010-07-31 14:29:06 -0400715static int nfs4_sequence_done(struct rpc_task *task,
716 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400717{
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400719}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400720#endif /* CONFIG_NFS_V4_1 */
721
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400722static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400727 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000729 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400730}
731
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400732static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000733int nfs4_call_sync(struct rpc_clnt *clnt,
734 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000735 struct rpc_message *msg,
736 struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res,
738 int cache_reply)
739{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400740 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000741 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400742 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000743}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400744
Trond Myklebust38478b22006-05-25 01:40:57 -0400745static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400750 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400752 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700753 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000754 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400755 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_openargs o_arg;
761 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100762 struct nfs_open_confirmargs c_arg;
763 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500764 struct nfs4_string owner_name;
765 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400768 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400770 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100772 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400773 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400774 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400779static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
780 int err, struct nfs4_exception *exception)
781{
782 if (err != -EINVAL)
783 return false;
784 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
785 return false;
786 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
787 exception->retry = 1;
788 return true;
789}
790
791static enum open_claim_type4
792nfs4_map_atomic_open_claim(struct nfs_server *server,
793 enum open_claim_type4 claim)
794{
795 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
796 return claim;
797 switch (claim) {
798 default:
799 return claim;
800 case NFS4_OPEN_CLAIM_FH:
801 return NFS4_OPEN_CLAIM_NULL;
802 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
804 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
805 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
806 }
807}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400808
809static void nfs4_init_opendata_res(struct nfs4_opendata *p)
810{
811 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400812 p->o_res.seqid = p->o_arg.seqid;
813 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400815 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500817 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400818}
819
Al Viro82a2c1b2011-06-22 18:30:55 -0400820static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400822 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400823 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825{
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 struct inode *dir = parent->d_inode;
828 struct nfs_server *server = NFS_SERVER(dir);
829 struct nfs4_opendata *p;
830
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400831 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 if (p == NULL)
833 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400834 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 if (p->o_arg.seqid == NULL)
836 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400837 nfs_sb_active(dentry->d_sb);
838 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839 p->dir = parent;
840 p->owner = sp;
841 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 p->o_arg.open_flags = flags;
843 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700844 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
845 * will return permission denied for all bits until close */
846 if (!(flags & O_EXCL)) {
847 /* ask server to check for all possible rights as results
848 * are cached */
849 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
850 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700851 }
David Howells7539bba2006-08-22 20:06:09 -0400852 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400853 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
854 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 p->o_arg.server = server;
857 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400858 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400859 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
860 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400861 case NFS4_OPEN_CLAIM_NULL:
862 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
863 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
864 p->o_arg.fh = NFS_FH(dir);
865 break;
866 case NFS4_OPEN_CLAIM_PREVIOUS:
867 case NFS4_OPEN_CLAIM_FH:
868 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
869 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
870 p->o_arg.fh = NFS_FH(dentry->d_inode);
871 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500872 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500873 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400874
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 p->o_arg.u.attrs = &p->attrs;
876 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500877
878 verf[0] = jiffies;
879 verf[1] = current->pid;
880 memcpy(p->o_arg.u.verifier.data, verf,
881 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100882 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100883 p->c_arg.fh = &p->o_res.fh;
884 p->c_arg.stateid = &p->o_res.stateid;
885 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400886 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400887 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 return p;
889err_free:
890 kfree(p);
891err:
892 dput(parent);
893 return NULL;
894}
895
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400896static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 struct nfs4_opendata *p = container_of(kref,
899 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400900 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400901
902 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 if (p->state != NULL)
904 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400905 nfs4_put_state_owner(p->owner);
906 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400907 dput(p->dentry);
908 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500909 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400910 kfree(p);
911}
912
913static void nfs4_opendata_put(struct nfs4_opendata *p)
914{
915 if (p != NULL)
916 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100917}
918
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
920{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 int ret;
922
Trond Myklebust06f814a2006-01-03 09:55:07 +0100923 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100924 return ret;
925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400928{
929 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930
Trond Myklebust536e43d2012-01-17 22:04:26 -0500931 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 goto out;
933 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500935 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
936 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400937 break;
938 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500939 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
940 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941 break;
942 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500943 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
944 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400947 return ret;
948}
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500952 if (delegation == NULL)
953 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500956 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400957 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500958 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
959 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500960 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 return 1;
962}
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100965{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100967 case FMODE_WRITE:
968 state->n_wronly++;
969 break;
970 case FMODE_READ:
971 state->n_rdonly++;
972 break;
973 case FMODE_READ|FMODE_WRITE:
974 state->n_rdwr++;
975 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100977}
978
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400980{
981 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500982 nfs4_stateid_copy(&state->stateid, stateid);
983 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400984 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 case FMODE_READ:
987 set_bit(NFS_O_RDONLY_STATE, &state->flags);
988 break;
989 case FMODE_WRITE:
990 set_bit(NFS_O_WRONLY_STATE, &state->flags);
991 break;
992 case FMODE_READ|FMODE_WRITE:
993 set_bit(NFS_O_RDWR_STATE, &state->flags);
994 }
995}
996
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400998{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001001 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001002}
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static 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 -07001005{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001006 /*
1007 * Protect the call to nfs4_state_set_mode_locked and
1008 * serialise the stateid update
1009 */
1010 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001012 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001013 set_bit(NFS_DELEGATED_STATE, &state->flags);
1014 }
1015 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001017 write_sequnlock(&state->seqlock);
1018 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001020 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023static 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 -05001024{
1025 struct nfs_inode *nfsi = NFS_I(state->inode);
1026 struct nfs_delegation *deleg_cur;
1027 int ret = 0;
1028
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001030
1031 rcu_read_lock();
1032 deleg_cur = rcu_dereference(nfsi->delegation);
1033 if (deleg_cur == NULL)
1034 goto no_delegation;
1035
1036 spin_lock(&deleg_cur->lock);
1037 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001038 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001040 goto no_delegation_unlock;
1041
1042 if (delegation == NULL)
1043 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001044 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001045 goto no_delegation_unlock;
1046
Trond Myklebustb7391f42008-12-23 15:21:52 -05001047 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050no_delegation_unlock:
1051 spin_unlock(&deleg_cur->lock);
1052no_delegation:
1053 rcu_read_unlock();
1054
1055 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001057 ret = 1;
1058 }
1059
1060 return ret;
1061}
1062
1063
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065{
1066 struct nfs_delegation *delegation;
1067
1068 rcu_read_lock();
1069 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 rcu_read_unlock();
1072 return;
1073 }
1074 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001075 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076}
1077
Trond Myklebust6ee41262007-07-08 14:11:36 -04001078static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079{
1080 struct nfs4_state *state = opendata->state;
1081 struct nfs_inode *nfsi = NFS_I(state->inode);
1082 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001083 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 nfs4_stateid stateid;
1086 int ret = -EAGAIN;
1087
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091 if (can_open_cached(state, fmode, open_mode)) {
1092 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 goto out_return_state;
1095 }
1096 spin_unlock(&state->owner->so_lock);
1097 }
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_lock();
1099 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001100 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001101 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001103 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001105 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001107 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001108 if (!opendata->is_recover) {
1109 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1110 if (ret != 0)
1111 goto out;
1112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001114
1115 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001117 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001119out:
1120 return ERR_PTR(ret);
1121out_return_state:
1122 atomic_inc(&state->count);
1123 return state;
1124}
1125
Andy Adamsone23008e2012-10-02 21:07:32 -04001126static void
1127nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1128{
1129 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1130 struct nfs_delegation *delegation;
1131 int delegation_flags = 0;
1132
1133 rcu_read_lock();
1134 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1135 if (delegation)
1136 delegation_flags = delegation->flags;
1137 rcu_read_unlock();
1138 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1139 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1140 "returning a delegation for "
1141 "OPEN(CLAIM_DELEGATE_CUR)\n",
1142 clp->cl_hostname);
1143 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1144 nfs_inode_set_delegation(state->inode,
1145 data->owner->so_cred,
1146 &data->o_res);
1147 else
1148 nfs_inode_reclaim_delegation(state->inode,
1149 data->owner->so_cred,
1150 &data->o_res);
1151}
1152
1153/*
1154 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1155 * and update the nfs4_state.
1156 */
1157static struct nfs4_state *
1158_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1159{
1160 struct inode *inode = data->state->inode;
1161 struct nfs4_state *state = data->state;
1162 int ret;
1163
1164 if (!data->rpc_done) {
1165 ret = data->rpc_status;
1166 goto err;
1167 }
1168
1169 ret = -ESTALE;
1170 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1171 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1172 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1173 goto err;
1174
1175 ret = -ENOMEM;
1176 state = nfs4_get_open_state(inode, data->owner);
1177 if (state == NULL)
1178 goto err;
1179
1180 ret = nfs_refresh_inode(inode, &data->f_attr);
1181 if (ret)
1182 goto err;
1183
1184 if (data->o_res.delegation_type != 0)
1185 nfs4_opendata_check_deleg(data, state);
1186 update_open_stateid(state, &data->o_res.stateid, NULL,
1187 data->o_arg.fmode);
1188
1189 return state;
1190err:
1191 return ERR_PTR(ret);
1192
1193}
1194
1195static struct nfs4_state *
1196_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001197{
1198 struct inode *inode;
1199 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001201
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001203 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001204 goto out;
1205 }
1206
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001207 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001209 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001210 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001212 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001213 goto err;
1214 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001215 state = nfs4_get_open_state(inode, data->owner);
1216 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001217 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001218 if (data->o_res.delegation_type != 0)
1219 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001220 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001221 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001222 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001223out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001224 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001225 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001226err_put_inode:
1227 iput(inode);
1228err:
1229 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001230}
1231
Andy Adamsone23008e2012-10-02 21:07:32 -04001232static struct nfs4_state *
1233nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1234{
1235 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1236 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1237 return _nfs4_opendata_to_nfs4_state(data);
1238}
1239
Trond Myklebust864472e2006-01-03 09:55:15 +01001240static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1241{
1242 struct nfs_inode *nfsi = NFS_I(state->inode);
1243 struct nfs_open_context *ctx;
1244
1245 spin_lock(&state->inode->i_lock);
1246 list_for_each_entry(ctx, &nfsi->open_files, list) {
1247 if (ctx->state != state)
1248 continue;
1249 get_nfs_open_context(ctx);
1250 spin_unlock(&state->inode->i_lock);
1251 return ctx;
1252 }
1253 spin_unlock(&state->inode->i_lock);
1254 return ERR_PTR(-ENOENT);
1255}
1256
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001257static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1258 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001259{
1260 struct nfs4_opendata *opendata;
1261
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001262 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1263 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001264 if (opendata == NULL)
1265 return ERR_PTR(-ENOMEM);
1266 opendata->state = state;
1267 atomic_inc(&state->count);
1268 return opendata;
1269}
1270
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001271static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001272{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001273 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001274 int ret;
1275
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001276 opendata->o_arg.open_flags = 0;
1277 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001278 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1279 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1280 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001281 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 if (ret != 0)
1283 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001284 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001285 if (IS_ERR(newstate))
1286 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001287 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001288 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 return 0;
1290}
1291
1292static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1293{
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 int ret;
1296
1297 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001298 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001299 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 smp_rmb();
1301 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001302 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001303 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 if (ret != 0)
1305 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001306 if (newstate != state)
1307 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 }
1309 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001310 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 if (ret != 0)
1313 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001314 if (newstate != state)
1315 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 }
1317 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001318 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001319 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001320 if (ret != 0)
1321 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001322 if (newstate != state)
1323 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001325 /*
1326 * We may have performed cached opens for all three recoveries.
1327 * Check if we need to update the current stateid.
1328 */
1329 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001330 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001331 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001332 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001333 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001334 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001335 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001336 return 0;
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/*
1340 * OPEN_RECLAIM:
1341 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001343static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001345 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001346 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int status;
1349
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001350 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1351 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001352 if (IS_ERR(opendata))
1353 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001354 rcu_read_lock();
1355 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001356 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001357 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001358 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001359 opendata->o_arg.u.delegation_type = delegation_type;
1360 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001361 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return status;
1363}
1364
Trond Myklebust539cd032007-06-05 11:46:42 -04001365static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct nfs_server *server = NFS_SERVER(state->inode);
1368 struct nfs4_exception exception = { };
1369 int err;
1370 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001371 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001372 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1373 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001374 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001375 break;
1376 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 } while (exception.retry);
1378 return err;
1379}
1380
Trond Myklebust864472e2006-01-03 09:55:15 +01001381static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1382{
1383 struct nfs_open_context *ctx;
1384 int ret;
1385
1386 ctx = nfs4_state_find_open_context(state);
1387 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001388 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001389 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001390 put_nfs_open_context(ctx);
1391 return ret;
1392}
1393
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001394static 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 -07001395{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001396 switch (err) {
1397 default:
1398 printk(KERN_ERR "NFS: %s: unhandled error "
1399 "%d.\n", __func__, err);
1400 case 0:
1401 case -ENOENT:
1402 case -ESTALE:
1403 break;
1404 case -NFS4ERR_BADSESSION:
1405 case -NFS4ERR_BADSLOT:
1406 case -NFS4ERR_BAD_HIGH_SLOT:
1407 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1408 case -NFS4ERR_DEADSESSION:
1409 set_bit(NFS_DELEGATED_STATE, &state->flags);
1410 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1411 return -EAGAIN;
1412 case -NFS4ERR_STALE_CLIENTID:
1413 case -NFS4ERR_STALE_STATEID:
1414 set_bit(NFS_DELEGATED_STATE, &state->flags);
1415 case -NFS4ERR_EXPIRED:
1416 /* Don't recall a delegation if it was lost */
1417 nfs4_schedule_lease_recovery(server->nfs_client);
1418 return -EAGAIN;
1419 case -NFS4ERR_DELEG_REVOKED:
1420 case -NFS4ERR_ADMIN_REVOKED:
1421 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001422 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001423 nfs_inode_find_state_and_recover(state->inode,
1424 stateid);
1425 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001426 return 0;
1427 case -NFS4ERR_DELAY:
1428 case -NFS4ERR_GRACE:
1429 set_bit(NFS_DELEGATED_STATE, &state->flags);
1430 ssleep(1);
1431 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001432 case -ENOMEM:
1433 case -NFS4ERR_DENIED:
1434 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1435 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return err;
1438}
1439
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001440int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1441{
1442 struct nfs_server *server = NFS_SERVER(state->inode);
1443 struct nfs4_opendata *opendata;
1444 int err;
1445
1446 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1447 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1448 if (IS_ERR(opendata))
1449 return PTR_ERR(opendata);
1450 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1451 err = nfs4_open_recover(opendata, state);
1452 nfs4_opendata_put(opendata);
1453 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1454}
1455
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001456static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1457{
1458 struct nfs4_opendata *data = calldata;
1459
1460 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001461 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001462 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001463 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001464 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001466 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467}
1468
1469static void nfs4_open_confirm_release(void *calldata)
1470{
1471 struct nfs4_opendata *data = calldata;
1472 struct nfs4_state *state = NULL;
1473
1474 /* If this request hasn't been cancelled, do nothing */
1475 if (data->cancelled == 0)
1476 goto out_free;
1477 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001478 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001479 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001480 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001481 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001482 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001483out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001484 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001485}
1486
1487static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001488 .rpc_call_done = nfs4_open_confirm_done,
1489 .rpc_release = nfs4_open_confirm_release,
1490};
1491
1492/*
1493 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1494 */
1495static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1496{
1497 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1498 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001499 struct rpc_message msg = {
1500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1501 .rpc_argp = &data->c_arg,
1502 .rpc_resp = &data->c_res,
1503 .rpc_cred = data->owner->so_cred,
1504 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001505 struct rpc_task_setup task_setup_data = {
1506 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001507 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001508 .callback_ops = &nfs4_open_confirm_ops,
1509 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001510 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001511 .flags = RPC_TASK_ASYNC,
1512 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int status;
1514
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001515 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001516 data->rpc_done = 0;
1517 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001518 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001519 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001520 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001521 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001522 status = nfs4_wait_for_completion_rpc_task(task);
1523 if (status != 0) {
1524 data->cancelled = 1;
1525 smp_wmb();
1526 } else
1527 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001528 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return status;
1530}
1531
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 struct nfs4_opendata *data = calldata;
1535 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001536 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001539 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 /*
1541 * Check if we still need to send an OPEN call, or if we can use
1542 * a delegation instead.
1543 */
1544 if (data->state != NULL) {
1545 struct nfs_delegation *delegation;
1546
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001548 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001549 rcu_read_lock();
1550 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001551 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001552 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001553 can_open_delegated(delegation, data->o_arg.fmode))
1554 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001555 rcu_read_unlock();
1556 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001557 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001558 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001559 switch (data->o_arg.claim) {
1560 case NFS4_OPEN_CLAIM_PREVIOUS:
1561 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1562 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001563 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001564 case NFS4_OPEN_CLAIM_FH:
1565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001566 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1567 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001568 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001569 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001570 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001571 &data->o_res.seq_res,
1572 task) != 0)
1573 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001574
1575 /* Set the create mode (note dependency on the session type) */
1576 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1577 if (data->o_arg.open_flags & O_EXCL) {
1578 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1579 if (nfs4_has_persistent_session(clp))
1580 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1581 else if (clp->cl_mvops->minor_version > 0)
1582 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1583 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001584 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001585unlock_no_action:
1586 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001587out_no_action:
1588 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001589out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001590 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001591}
1592
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593static void nfs4_open_done(struct rpc_task *task, void *calldata)
1594{
1595 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001598
Trond Myklebust14516c32010-07-31 14:29:06 -04001599 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1600 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001601
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001603 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1604 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001605 case S_IFREG:
1606 break;
1607 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 break;
1610 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001612 break;
1613 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001615 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001616 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001617 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001618 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1619 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001620 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001621 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001623
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624static void nfs4_open_release(void *calldata)
1625{
1626 struct nfs4_opendata *data = calldata;
1627 struct nfs4_state *state = NULL;
1628
1629 /* If this request hasn't been cancelled, do nothing */
1630 if (data->cancelled == 0)
1631 goto out_free;
1632 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001633 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 goto out_free;
1635 /* In case we need an open_confirm, no cleanup! */
1636 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1637 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001640 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001642 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643}
1644
1645static const struct rpc_call_ops nfs4_open_ops = {
1646 .rpc_call_prepare = nfs4_open_prepare,
1647 .rpc_call_done = nfs4_open_done,
1648 .rpc_release = nfs4_open_release,
1649};
1650
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001651static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652{
1653 struct inode *dir = data->dir->d_inode;
1654 struct nfs_server *server = NFS_SERVER(dir);
1655 struct nfs_openargs *o_arg = &data->o_arg;
1656 struct nfs_openres *o_res = &data->o_res;
1657 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001658 struct rpc_message msg = {
1659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1660 .rpc_argp = o_arg,
1661 .rpc_resp = o_res,
1662 .rpc_cred = data->owner->so_cred,
1663 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001664 struct rpc_task_setup task_setup_data = {
1665 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001666 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001667 .callback_ops = &nfs4_open_ops,
1668 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001669 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001670 .flags = RPC_TASK_ASYNC,
1671 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001672 int status;
1673
Trond Myklebust9d12b212012-01-17 22:04:25 -05001674 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001675 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001676 data->rpc_done = 0;
1677 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001678 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001679 data->is_recover = 0;
1680 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001681 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001682 data->is_recover = 1;
1683 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001684 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001685 if (IS_ERR(task))
1686 return PTR_ERR(task);
1687 status = nfs4_wait_for_completion_rpc_task(task);
1688 if (status != 0) {
1689 data->cancelled = 1;
1690 smp_wmb();
1691 } else
1692 status = data->rpc_status;
1693 rpc_put_task(task);
1694
1695 return status;
1696}
1697
1698static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1699{
1700 struct inode *dir = data->dir->d_inode;
1701 struct nfs_openres *o_res = &data->o_res;
1702 int status;
1703
1704 status = nfs4_run_open_task(data, 1);
1705 if (status != 0 || !data->rpc_done)
1706 return status;
1707
Trond Myklebust6926afd2012-01-07 13:22:46 -05001708 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1709
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001710 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1711 status = _nfs4_proc_open_confirm(data);
1712 if (status != 0)
1713 return status;
1714 }
1715
1716 return status;
1717}
1718
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001719static int nfs4_opendata_access(struct rpc_cred *cred,
1720 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001721 struct nfs4_state *state, fmode_t fmode,
1722 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001723{
1724 struct nfs_access_entry cache;
1725 u32 mask;
1726
1727 /* access call failed or for some reason the server doesn't
1728 * support any access modes -- defer access call until later */
1729 if (opendata->o_res.access_supported == 0)
1730 return 0;
1731
1732 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001733 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001734 * write mode bits, but POSIX allows the creating process to write.
1735 * use openflags to check for exec, because fmode won't
1736 * always have FMODE_EXEC set when file open for exec. */
1737 if (openflags & __FMODE_EXEC) {
1738 /* ONLY check for exec rights */
1739 mask = MAY_EXEC;
1740 } else if (fmode & FMODE_READ)
1741 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001742
1743 cache.cred = cred;
1744 cache.jiffies = jiffies;
1745 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1746 nfs_access_add_cache(state->inode, &cache);
1747
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001748 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001749 return 0;
1750
1751 /* even though OPEN succeeded, access is denied. Close the file */
1752 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001753 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001754}
1755
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001756/*
1757 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1758 */
1759static int _nfs4_proc_open(struct nfs4_opendata *data)
1760{
1761 struct inode *dir = data->dir->d_inode;
1762 struct nfs_server *server = NFS_SERVER(dir);
1763 struct nfs_openargs *o_arg = &data->o_arg;
1764 struct nfs_openres *o_res = &data->o_res;
1765 int status;
1766
1767 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001768 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001770 if (status != 0) {
1771 if (status == -NFS4ERR_BADNAME &&
1772 !(o_arg->open_flags & O_CREAT))
1773 return -ENOENT;
1774 return status;
1775 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776
Trond Myklebust6926afd2012-01-07 13:22:46 -05001777 nfs_fattr_map_and_free_names(server, &data->f_attr);
1778
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001779 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001780 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001781 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1782 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001784 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001789 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Andy Adamsond83217c2011-03-01 01:34:17 +00001793static int nfs4_recover_expired_lease(struct nfs_server *server)
1794{
1795 return nfs4_client_recover_expired_lease(server->nfs_client);
1796}
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/*
1799 * OPEN_EXPIRED:
1800 * reclaim state on the server after a network partition.
1801 * Assumes caller holds the appropriate lock
1802 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001803static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001805 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001806 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001808 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001809 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001810 if (IS_ERR(opendata))
1811 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001812 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001813 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001814 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001815 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001819static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001820{
Trond Myklebust539cd032007-06-05 11:46:42 -04001821 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001822 struct nfs4_exception exception = { };
1823 int err;
1824
1825 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001826 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001827 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1828 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001829 switch (err) {
1830 default:
1831 goto out;
1832 case -NFS4ERR_GRACE:
1833 case -NFS4ERR_DELAY:
1834 nfs4_handle_exception(server, err, &exception);
1835 err = 0;
1836 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001837 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001838out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001839 return err;
1840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Trond Myklebust864472e2006-01-03 09:55:15 +01001847 ctx = nfs4_state_find_open_context(state);
1848 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001849 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001850 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 put_nfs_open_context(ctx);
1852 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001855#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001856static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001857{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001858 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001859 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001860 struct nfs_delegation *delegation;
1861 struct rpc_cred *cred = NULL;
1862 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001863
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001864 /* If a state reset has been done, test_stateid is unneeded */
1865 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1866 return;
1867
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001868 /* Get the delegation credential for use by test/free_stateid */
1869 rcu_read_lock();
1870 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1871 if (delegation != NULL &&
1872 nfs4_stateid_match(&delegation->stateid, stateid)) {
1873 cred = get_rpccred(delegation->cred);
1874 rcu_read_unlock();
1875 status = nfs41_test_stateid(server, stateid, cred);
1876 } else
1877 rcu_read_unlock();
1878
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001879 if (status != NFS_OK) {
1880 /* Free the stateid unless the server explicitly
1881 * informs us the stateid is unrecognized. */
1882 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001883 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001884 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001885
Bryan Schumaker69388672012-09-26 15:25:52 -04001886 write_seqlock(&state->seqlock);
1887 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1888 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001889 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1890 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001891
1892 if (cred != NULL)
1893 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001894}
1895
1896/**
1897 * nfs41_check_open_stateid - possibly free an open stateid
1898 *
1899 * @state: NFSv4 state for an inode
1900 *
1901 * Returns NFS_OK if recovery for this stateid is now finished.
1902 * Otherwise a negative NFS4ERR value is returned.
1903 */
1904static int nfs41_check_open_stateid(struct nfs4_state *state)
1905{
1906 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001907 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001908 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001909 int status;
1910
1911 /* If a state reset has been done, test_stateid is unneeded */
1912 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1913 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1914 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1915 return -NFS4ERR_BAD_STATEID;
1916
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001917 status = nfs41_test_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001918 if (status != NFS_OK) {
1919 /* Free the stateid unless the server explicitly
1920 * informs us the stateid is unrecognized. */
1921 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001922 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001923
1924 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1925 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1926 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001927 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001928 }
1929 return status;
1930}
1931
1932static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1933{
Chuck Levereb64cf92012-07-11 16:30:05 -04001934 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001935
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 nfs41_clear_delegation_stateid(state);
1937 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001938 if (status != NFS_OK)
1939 status = nfs4_open_expired(sp, state);
1940 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001941}
1942#endif
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001945 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1946 * fields corresponding to attributes that were used to store the verifier.
1947 * Make sure we clobber those fields in the later setattr call
1948 */
1949static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1950{
1951 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1952 !(sattr->ia_valid & ATTR_ATIME_SET))
1953 sattr->ia_valid |= ATTR_ATIME;
1954
1955 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1956 !(sattr->ia_valid & ATTR_MTIME_SET))
1957 sattr->ia_valid |= ATTR_MTIME;
1958}
1959
Trond Myklebustc21443c2013-02-07 14:26:21 -05001960static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1961 fmode_t fmode,
1962 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04001963 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05001964{
1965 struct nfs4_state_owner *sp = opendata->owner;
1966 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04001967 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05001968 struct nfs4_state *state;
1969 unsigned int seq;
1970 int ret;
1971
1972 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1973
1974 ret = _nfs4_proc_open(opendata);
1975 if (ret != 0)
1976 goto out;
1977
1978 state = nfs4_opendata_to_nfs4_state(opendata);
1979 ret = PTR_ERR(state);
1980 if (IS_ERR(state))
1981 goto out;
1982 if (server->caps & NFS_CAP_POSIX_LOCK)
1983 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1984
Trond Myklebust275bb302013-05-29 13:11:28 -04001985 dentry = opendata->dentry;
1986 if (dentry->d_inode == NULL) {
1987 /* FIXME: Is this d_drop() ever needed? */
1988 d_drop(dentry);
1989 dentry = d_add_unique(dentry, igrab(state->inode));
1990 if (dentry == NULL) {
1991 dentry = opendata->dentry;
1992 } else if (dentry != ctx->dentry) {
1993 dput(ctx->dentry);
1994 ctx->dentry = dget(dentry);
1995 }
1996 nfs_set_verifier(dentry,
1997 nfs_save_change_attribute(opendata->dir->d_inode));
1998 }
1999
Trond Myklebustc21443c2013-02-07 14:26:21 -05002000 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2001 if (ret != 0)
2002 goto out;
2003
Trond Myklebust3efb9722013-05-29 13:17:04 -04002004 ctx->state = state;
Trond Myklebust91876b12013-03-28 14:01:33 -04002005 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05002006 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002007out:
2008 return ret;
2009}
2010
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002011/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002012 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 */
Andy Adamson82be4172012-05-23 05:02:35 -04002014static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002015 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002016 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002017 struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct nfs4_state_owner *sp;
2020 struct nfs4_state *state = NULL;
2021 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002022 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002023 struct dentry *dentry = ctx->dentry;
2024 struct rpc_cred *cred = ctx->cred;
2025 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2026 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002027 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002028 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002032 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2033 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2035 goto out_err;
2036 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002037 status = nfs4_recover_expired_lease(server);
2038 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002039 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002040 if (dentry->d_inode != NULL)
2041 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002042 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002043 if (dentry->d_inode)
2044 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002045 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002046 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002047 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002048 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Andy Adamson82be4172012-05-23 05:02:35 -04002050 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2051 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2052 if (!opendata->f_attr.mdsthreshold)
2053 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002054 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002055 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002056 if (dentry->d_inode != NULL)
2057 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002058
Trond Myklebust3efb9722013-05-29 13:17:04 -04002059 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002060 if (status != 0)
2061 goto err_opendata_put;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002062 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002063
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 if ((opendata->o_arg.open_flags & O_EXCL) &&
2065 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002066 nfs4_exclusive_attrset(opendata, sattr);
2067
2068 nfs_fattr_init(opendata->o_res.f_attr);
2069 status = nfs4_do_setattr(state->inode, cred,
2070 opendata->o_res.f_attr, sattr,
2071 state);
2072 if (status == 0)
2073 nfs_setattr_update_inode(state->inode, sattr);
2074 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2075 }
Andy Adamson82be4172012-05-23 05:02:35 -04002076
2077 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2078 *ctx_th = opendata->f_attr.mdsthreshold;
2079 else
2080 kfree(opendata->f_attr.mdsthreshold);
2081 opendata->f_attr.mdsthreshold = NULL;
2082
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002083 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002086err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002087 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002088 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002089err_put_state_owner:
2090 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return status;
2093}
2094
2095
Andy Adamson82be4172012-05-23 05:02:35 -04002096static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002097 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002098 int flags,
Trond Myklebust4197a052013-05-29 12:37:49 -04002099 struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002101 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs4_exception exception = { };
2103 struct nfs4_state *res;
2104 int status;
2105
2106 do {
Trond Myklebust3efb9722013-05-29 13:17:04 -04002107 status = _nfs4_do_open(dir, ctx, flags, sattr);
2108 res = ctx->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (status == 0)
2110 break;
2111 /* NOTE: BAD_SEQID means the server and client disagree about the
2112 * book-keeping w.r.t. state-changing operations
2113 * (OPEN/CLOSE/LOCK/LOCKU...)
2114 * It is actually a sign of a bug on the client or on the server.
2115 *
2116 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002117 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * have unhashed the old state_owner for us, and that we can
2119 * therefore safely retry using a new one. We should still warn
2120 * the user though...
2121 */
2122 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002123 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002124 " returned a bad sequence-id error!\n",
2125 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 exception.retry = 1;
2127 continue;
2128 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002129 /*
2130 * BAD_STATEID on OPEN means that the server cancelled our
2131 * state before it received the OPEN_CONFIRM.
2132 * Recover by retrying the request as per the discussion
2133 * on Page 181 of RFC3530.
2134 */
2135 if (status == -NFS4ERR_BAD_STATEID) {
2136 exception.retry = 1;
2137 continue;
2138 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002139 if (status == -EAGAIN) {
2140 /* We must have found a delegation */
2141 exception.retry = 1;
2142 continue;
2143 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002144 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2145 continue;
2146 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 status, &exception));
2148 } while (exception.retry);
2149 return res;
2150}
2151
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002152static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2153 struct nfs_fattr *fattr, struct iattr *sattr,
2154 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002156 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002158 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 .iap = sattr,
2160 .server = server,
2161 .bitmask = server->attr_bitmask,
2162 };
2163 struct nfs_setattrres res = {
2164 .fattr = fattr,
2165 .server = server,
2166 };
2167 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2169 .rpc_argp = &arg,
2170 .rpc_resp = &res,
2171 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002173 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002174 fmode_t fmode;
2175 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002176 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Trond Myklebust0e574af2005-10-27 22:12:38 -04002178 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Trond Myklebustee3ae842013-04-29 10:35:36 -04002180 /* Servers should only apply open mode checks for file size changes */
2181 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2182 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2183
2184 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2185 /* Use that stateid */
2186 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002187 struct nfs_lockowner lockowner = {
2188 .l_owner = current->files,
2189 .l_pid = current->tgid,
2190 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002191 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002192 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002193 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002194 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Bryan Schumaker7c513052011-03-24 17:12:24 +00002196 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002197 if (status == 0 && state != NULL)
2198 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002199 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002202static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2203 struct nfs_fattr *fattr, struct iattr *sattr,
2204 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002206 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002207 struct nfs4_exception exception = {
2208 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002209 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 int err;
2212 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002213 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2214 switch (err) {
2215 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002216 if (!(sattr->ia_valid & ATTR_SIZE)) {
2217 pr_warn_once("NFSv4: server %s is incorrectly "
2218 "applying open mode checks to "
2219 "a SETATTR that is not "
2220 "changing file size.\n",
2221 server->nfs_client->cl_hostname);
2222 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002223 if (state && !(state->state & FMODE_WRITE)) {
2224 err = -EBADF;
2225 if (sattr->ia_valid & ATTR_OPEN)
2226 err = -EACCES;
2227 goto out;
2228 }
2229 }
2230 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002232out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return err;
2234}
2235
2236struct nfs4_closedata {
2237 struct inode *inode;
2238 struct nfs4_state *state;
2239 struct nfs_closeargs arg;
2240 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002241 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002242 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002243 bool roc;
2244 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245};
2246
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002247static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002248{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002249 struct nfs4_closedata *calldata = data;
2250 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002251 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002252
Fred Isamanf7e89172011-01-06 11:36:32 +00002253 if (calldata->roc)
2254 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002255 nfs4_put_open_state(calldata->state);
2256 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002257 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002258 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002259 kfree(calldata);
2260}
2261
Trond Myklebust88069f72009-12-08 08:33:16 -05002262static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2263 fmode_t fmode)
2264{
2265 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002266 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002267 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2268 case FMODE_WRITE:
2269 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2270 break;
2271 case FMODE_READ:
2272 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2273 break;
2274 case 0:
2275 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2276 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2277 clear_bit(NFS_OPEN_STATE, &state->flags);
2278 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002279 spin_unlock(&state->owner->so_lock);
2280}
2281
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002282static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002284 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 struct nfs_server *server = NFS_SERVER(calldata->inode);
2287
Chuck Levera3ca5652012-03-01 17:00:40 -05002288 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002289 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 /* hmm. we are done with the inode, and in the process of freeing
2292 * the state_owner. we keep this around to process errors
2293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 switch (task->tk_status) {
2295 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002296 if (calldata->roc)
2297 pnfs_roc_set_barrier(state->inode,
2298 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002299 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002300 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002301 nfs4_close_clear_stateid_flags(state,
2302 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 break;
2304 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002305 case -NFS4ERR_OLD_STATEID:
2306 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002308 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002311 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2312 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002314 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002315 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002316 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002319static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002321 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002322 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002323 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002324 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002325
Chuck Levera3ca5652012-03-01 17:00:40 -05002326 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002327 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002328 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002329
Trond Myklebust88069f72009-12-08 08:33:16 -05002330 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2331 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002332 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002333 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002334 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002335 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002336 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2337 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2338 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002339 }
2340 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002341 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2342 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2343 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002344 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002345 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002346 if (!nfs4_valid_open_stateid(state))
2347 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002348 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002349
2350 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002351 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002352 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002353 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002354
Fred Isamanf7e89172011-01-06 11:36:32 +00002355 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002356 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002357 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002358 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2359 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002360 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002361 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002362 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002363
Trond Myklebust516a6af2005-10-27 22:12:41 -04002364 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002365 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002366 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002367 &calldata->arg.seq_args,
2368 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002369 task) != 0)
2370 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002371 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002372 return;
2373out_no_action:
2374 task->tk_action = NULL;
2375out_wait:
2376 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002379static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002380 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002381 .rpc_call_done = nfs4_close_done,
2382 .rpc_release = nfs4_free_closedata,
2383};
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385/*
2386 * It is possible for data to be read/written from a mem-mapped file
2387 * after the sys_close call (which hits the vfs layer as a flush).
2388 * This means that we can't safely call nfsv4 close on a file until
2389 * the inode is cleared. This in turn means that we are not good
2390 * NFSv4 citizens - we do not indicate to the server to update the file's
2391 * share state even when we are done with one of the three share
2392 * stateid's in the inode.
2393 *
2394 * NOTE: Caller must be holding the sp->so_owner semaphore!
2395 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002396int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002398 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002400 struct nfs4_state_owner *sp = state->owner;
2401 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002402 struct rpc_message msg = {
2403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2404 .rpc_cred = state->owner->so_cred,
2405 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002406 struct rpc_task_setup task_setup_data = {
2407 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002408 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002409 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002410 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002411 .flags = RPC_TASK_ASYNC,
2412 };
Trond Myklebust95121352005-10-18 14:20:12 -07002413 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002415 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002417 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002418 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002419 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002421 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002422 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002424 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002425 if (calldata->arg.seqid == NULL)
2426 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002427 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002428 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002429 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002430 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002431 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002432 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002433 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002434
Trond Myklebust1174dd12010-12-21 10:52:24 -05002435 msg.rpc_argp = &calldata->arg;
2436 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002437 task_setup_data.callback_data = calldata;
2438 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002439 if (IS_ERR(task))
2440 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002441 status = 0;
2442 if (wait)
2443 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002444 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002445 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002446out_free_calldata:
2447 kfree(calldata);
2448out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002449 nfs4_put_open_state(state);
2450 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002451 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Trond Myklebust2b484292010-09-17 10:56:51 -04002454static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002455nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct nfs4_state *state;
2458
Trond Myklebust565277f2007-10-15 18:17:53 -04002459 /* Protect against concurrent sillydeletes */
Trond Myklebust4197a052013-05-29 12:37:49 -04002460 state = nfs4_do_open(dir, ctx, open_flags, attr);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002461 if (IS_ERR(state))
2462 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002463 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Trond Myklebust1185a552009-12-03 15:54:02 -05002466static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002467{
2468 if (ctx->state == NULL)
2469 return;
2470 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002471 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002472 else
Al Viro643168c2011-06-22 18:20:23 -04002473 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2477{
Benny Halevy43652ad2009-04-01 09:21:54 -04002478 struct nfs4_server_caps_arg args = {
2479 .fhandle = fhandle,
2480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct nfs4_server_caps_res res = {};
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002484 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .rpc_resp = &res,
2486 };
2487 int status;
2488
Bryan Schumaker7c513052011-03-24 17:12:24 +00002489 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (status == 0) {
2491 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002492 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2493 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2494 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2495 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2496 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2498 server->caps |= NFS_CAP_ACLS;
2499 if (res.has_links != 0)
2500 server->caps |= NFS_CAP_HARDLINKS;
2501 if (res.has_symlinks != 0)
2502 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002503 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2504 server->caps |= NFS_CAP_FILEID;
2505 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2506 server->caps |= NFS_CAP_MODE;
2507 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2508 server->caps |= NFS_CAP_NLINK;
2509 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2510 server->caps |= NFS_CAP_OWNER;
2511 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2512 server->caps |= NFS_CAP_OWNER_GROUP;
2513 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2514 server->caps |= NFS_CAP_ATIME;
2515 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2516 server->caps |= NFS_CAP_CTIME;
2517 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2518 server->caps |= NFS_CAP_MTIME;
2519
Trond Myklebusta65318b2009-03-11 14:10:28 -04002520 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2521 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2522 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002524 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return status;
2528}
2529
Trond Myklebust55a97592006-06-09 09:34:19 -04002530int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
2535 err = nfs4_handle_exception(server,
2536 _nfs4_server_capabilities(server, fhandle),
2537 &exception);
2538 } while (exception.retry);
2539 return err;
2540}
2541
2542static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2543 struct nfs_fsinfo *info)
2544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs4_lookup_root_arg args = {
2546 .bitmask = nfs4_fattr_bitmap,
2547 };
2548 struct nfs4_lookup_res res = {
2549 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002550 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 .fh = fhandle,
2552 };
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2555 .rpc_argp = &args,
2556 .rpc_resp = &res,
2557 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002558
Trond Myklebust0e574af2005-10-27 22:12:38 -04002559 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002560 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2564 struct nfs_fsinfo *info)
2565{
2566 struct nfs4_exception exception = { };
2567 int err;
2568 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002569 err = _nfs4_lookup_root(server, fhandle, info);
2570 switch (err) {
2571 case 0:
2572 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002573 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002574 default:
2575 err = nfs4_handle_exception(server, err, &exception);
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002578out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return err;
2580}
2581
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002582static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2583 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2584{
2585 struct rpc_auth *auth;
2586 int ret;
2587
2588 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002589 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002590 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002591 goto out;
2592 }
2593 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002594out:
2595 return ret;
2596}
2597
Chuck Lever9a744ba2013-03-16 15:56:02 -04002598/*
2599 * Retry pseudoroot lookup with various security flavors. We do this when:
2600 *
2601 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2602 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2603 *
2604 * Returns zero on success, or a negative NFS4ERR value, or a
2605 * negative errno value.
2606 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002607static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002608 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002610 /* Per 3530bis 15.33.5 */
2611 static const rpc_authflavor_t flav_array[] = {
2612 RPC_AUTH_GSS_KRB5P,
2613 RPC_AUTH_GSS_KRB5I,
2614 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002615 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002616 RPC_AUTH_NULL,
2617 };
2618 int status = -EPERM;
2619 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Chuck Lever9a744ba2013-03-16 15:56:02 -04002621 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002622 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002623 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002624 continue;
2625 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002626 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002627
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002628 /*
2629 * -EACCESS could mean that the user doesn't have correct permissions
2630 * to access the mount. It could also mean that we tried to mount
2631 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2632 * existing mount programs don't handle -EACCES very well so it should
2633 * be mapped to -EPERM instead.
2634 */
2635 if (status == -EACCES)
2636 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002637 return status;
2638}
2639
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002640static int nfs4_do_find_root_sec(struct nfs_server *server,
2641 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2642{
2643 int mv = server->nfs_client->cl_minorversion;
2644 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2645}
2646
2647/**
2648 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2649 * @server: initialized nfs_server handle
2650 * @fhandle: we fill in the pseudo-fs root file handle
2651 * @info: we fill in an FSINFO struct
2652 *
2653 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002654 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002655int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2656 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002657{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002658 int status;
2659
2660 status = nfs4_lookup_root(server, fhandle, info);
2661 if ((status == -NFS4ERR_WRONGSEC) &&
2662 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2663 status = nfs4_do_find_root_sec(server, fhandle, info);
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (status == 0)
2666 status = nfs4_server_capabilities(server, fhandle);
2667 if (status == 0)
2668 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002669
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002670 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Bryan Schumakerbae36242012-05-10 15:07:31 -04002673static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2674 struct nfs_fsinfo *info)
2675{
2676 int error;
2677 struct nfs_fattr *fattr = info->fattr;
2678
2679 error = nfs4_server_capabilities(server, mntfh);
2680 if (error < 0) {
2681 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2682 return error;
2683 }
2684
2685 error = nfs4_proc_getattr(server, mntfh, fattr);
2686 if (error < 0) {
2687 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2688 return error;
2689 }
2690
2691 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2692 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2693 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2694
2695 return error;
2696}
2697
Manoj Naik6b97fd32006-06-09 09:34:29 -04002698/*
2699 * Get locations and (maybe) other attributes of a referral.
2700 * Note that we'll actually follow the referral later when
2701 * we detect fsid mismatch in inode revalidation
2702 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002703static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2704 const struct qstr *name, struct nfs_fattr *fattr,
2705 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002706{
2707 int status = -ENOMEM;
2708 struct page *page = NULL;
2709 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002710
2711 page = alloc_page(GFP_KERNEL);
2712 if (page == NULL)
2713 goto out;
2714 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2715 if (locations == NULL)
2716 goto out;
2717
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002718 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002719 if (status != 0)
2720 goto out;
2721 /* Make sure server returned a different fsid for the referral */
2722 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002723 dprintk("%s: server did not return a different fsid for"
2724 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002725 status = -EIO;
2726 goto out;
2727 }
Andy Adamson533eb462011-06-13 18:25:56 -04002728 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2729 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002730
Andy Adamson533eb462011-06-13 18:25:56 -04002731 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002732 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002733 memset(fhandle, 0, sizeof(struct nfs_fh));
2734out:
2735 if (page)
2736 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002737 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002738 return status;
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2742{
2743 struct nfs4_getattr_arg args = {
2744 .fh = fhandle,
2745 .bitmask = server->attr_bitmask,
2746 };
2747 struct nfs4_getattr_res res = {
2748 .fattr = fattr,
2749 .server = server,
2750 };
2751 struct rpc_message msg = {
2752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2753 .rpc_argp = &args,
2754 .rpc_resp = &res,
2755 };
2756
Trond Myklebust0e574af2005-10-27 22:12:38 -04002757 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002758 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
2761static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2762{
2763 struct nfs4_exception exception = { };
2764 int err;
2765 do {
2766 err = nfs4_handle_exception(server,
2767 _nfs4_proc_getattr(server, fhandle, fattr),
2768 &exception);
2769 } while (exception.retry);
2770 return err;
2771}
2772
2773/*
2774 * The file is not closed if it is opened due to the a request to change
2775 * the size of the file. The open call will not be needed once the
2776 * VFS layer lookup-intents are implemented.
2777 *
2778 * Close is called when the inode is destroyed.
2779 * If we haven't opened the file for O_WRONLY, we
2780 * need to in the size_change case to obtain a stateid.
2781 *
2782 * Got race?
2783 * Because OPEN is always done by name in nfsv4, it is
2784 * possible that we opened a different file by the same
2785 * name. We can recognize this race condition, but we
2786 * can't do anything about it besides returning an error.
2787 *
2788 * This will be fixed with VFS changes (lookup-intent).
2789 */
2790static int
2791nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2792 struct iattr *sattr)
2793{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002794 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002795 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002796 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 int status;
2798
Benny Halevy8a1636c2010-07-14 15:43:57 -04002799 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002800 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002801
Trond Myklebust0e574af2005-10-27 22:12:38 -04002802 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Andy Adamson26699402012-05-30 16:12:24 -04002804 /* Deal with open(O_TRUNC) */
2805 if (sattr->ia_valid & ATTR_OPEN)
2806 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2807
2808 /* Optimization: if the end result is no change, don't RPC */
2809 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2810 return 0;
2811
Trond Myklebustd5308382005-11-04 15:33:38 -05002812 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002813 if (sattr->ia_valid & ATTR_FILE) {
2814 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002815
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002816 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002817 if (ctx) {
2818 cred = ctx->cred;
2819 state = ctx->state;
2820 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002821 }
2822
2823 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002824 if (status == 0)
2825 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return status;
2827}
2828
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002829static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2830 const struct qstr *name, struct nfs_fh *fhandle,
2831 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002832{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002833 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002834 int status;
2835 struct nfs4_lookup_arg args = {
2836 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002837 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002838 .name = name,
2839 };
2840 struct nfs4_lookup_res res = {
2841 .server = server,
2842 .fattr = fattr,
2843 .fh = fhandle,
2844 };
2845 struct rpc_message msg = {
2846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2847 .rpc_argp = &args,
2848 .rpc_resp = &res,
2849 };
2850
2851 nfs_fattr_init(fattr);
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002854 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 dprintk("NFS reply lookup: %d\n", status);
2856 return status;
2857}
2858
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002859static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002860{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002861 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002862 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002863 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2864 fattr->nlink = 2;
2865}
2866
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002867static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2868 struct qstr *name, struct nfs_fh *fhandle,
2869 struct nfs_fattr *fattr)
2870{
2871 struct nfs4_exception exception = { };
2872 struct rpc_clnt *client = *clnt;
2873 int err;
2874 do {
2875 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2876 switch (err) {
2877 case -NFS4ERR_BADNAME:
2878 err = -ENOENT;
2879 goto out;
2880 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002881 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002882 goto out;
2883 case -NFS4ERR_WRONGSEC:
2884 err = -EPERM;
2885 if (client != *clnt)
2886 goto out;
2887
2888 client = nfs4_create_sec_client(client, dir, name);
2889 if (IS_ERR(client))
2890 return PTR_ERR(client);
2891
2892 exception.retry = 1;
2893 break;
2894 default:
2895 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2896 }
2897 } while (exception.retry);
2898
2899out:
2900 if (err == 0)
2901 *clnt = client;
2902 else if (client != *clnt)
2903 rpc_shutdown_client(client);
2904
2905 return err;
2906}
2907
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002908static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002909 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002911 int status;
2912 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002913
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002914 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2915 if (client != NFS_CLIENT(dir)) {
2916 rpc_shutdown_client(client);
2917 nfs_fixup_secinfo_attributes(fattr);
2918 }
2919 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002922struct rpc_clnt *
2923nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2924 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2925{
2926 int status;
2927 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2928
2929 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2930 if (status < 0) {
2931 rpc_shutdown_client(client);
2932 return ERR_PTR(status);
2933 }
2934 return client;
2935}
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2938{
Trond Myklebust76b32992007-08-10 17:45:11 -04002939 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct nfs4_accessargs args = {
2941 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002942 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002944 struct nfs4_accessres res = {
2945 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002946 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct rpc_message msg = {
2948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2949 .rpc_argp = &args,
2950 .rpc_resp = &res,
2951 .rpc_cred = entry->cred,
2952 };
2953 int mode = entry->mask;
2954 int status;
2955
2956 /*
2957 * Determine which access bits we want to ask for...
2958 */
2959 if (mode & MAY_READ)
2960 args.access |= NFS4_ACCESS_READ;
2961 if (S_ISDIR(inode->i_mode)) {
2962 if (mode & MAY_WRITE)
2963 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2964 if (mode & MAY_EXEC)
2965 args.access |= NFS4_ACCESS_LOOKUP;
2966 } else {
2967 if (mode & MAY_WRITE)
2968 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2969 if (mode & MAY_EXEC)
2970 args.access |= NFS4_ACCESS_EXECUTE;
2971 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002972
2973 res.fattr = nfs_alloc_fattr();
2974 if (res.fattr == NULL)
2975 return -ENOMEM;
2976
Bryan Schumaker7c513052011-03-24 17:12:24 +00002977 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002979 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002980 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002982 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return status;
2984}
2985
2986static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2987{
2988 struct nfs4_exception exception = { };
2989 int err;
2990 do {
2991 err = nfs4_handle_exception(NFS_SERVER(inode),
2992 _nfs4_proc_access(inode, entry),
2993 &exception);
2994 } while (exception.retry);
2995 return err;
2996}
2997
2998/*
2999 * TODO: For the time being, we don't try to get any attributes
3000 * along with any of the zero-copy operations READ, READDIR,
3001 * READLINK, WRITE.
3002 *
3003 * In the case of the first three, we want to put the GETATTR
3004 * after the read-type operation -- this is because it is hard
3005 * to predict the length of a GETATTR response in v4, and thus
3006 * align the READ data correctly. This means that the GETATTR
3007 * may end up partially falling into the page cache, and we should
3008 * shift it into the 'tail' of the xdr_buf before processing.
3009 * To do this efficiently, we need to know the total length
3010 * of data received, which doesn't seem to be available outside
3011 * of the RPC layer.
3012 *
3013 * In the case of WRITE, we also want to put the GETATTR after
3014 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003015 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 *
3017 * Both of these changes to the XDR layer would in fact be quite
3018 * minor, but I decided to leave them for a subsequent patch.
3019 */
3020static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3021 unsigned int pgbase, unsigned int pglen)
3022{
3023 struct nfs4_readlink args = {
3024 .fh = NFS_FH(inode),
3025 .pgbase = pgbase,
3026 .pglen = pglen,
3027 .pages = &page,
3028 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003029 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct rpc_message msg = {
3031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3032 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003033 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 };
3035
Bryan Schumaker7c513052011-03-24 17:12:24 +00003036 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 -07003037}
3038
3039static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3040 unsigned int pgbase, unsigned int pglen)
3041{
3042 struct nfs4_exception exception = { };
3043 int err;
3044 do {
3045 err = nfs4_handle_exception(NFS_SERVER(inode),
3046 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3047 &exception);
3048 } while (exception.retry);
3049 return err;
3050}
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003053 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055static int
3056nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003057 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003059 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 int status = 0;
3062
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003063 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3064 if (IS_ERR(ctx))
3065 return PTR_ERR(ctx);
3066
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003067 sattr->ia_mode &= ~current_umask();
Trond Myklebust4197a052013-05-29 12:37:49 -04003068 state = nfs4_do_open(dir, ctx, flags, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (IS_ERR(state)) {
3070 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003074 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return status;
3076}
3077
3078static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3079{
Trond Myklebust16e42952005-10-27 22:12:44 -04003080 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003081 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003083 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003085 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003086 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3090 .rpc_argp = &args,
3091 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003093 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003094
Bryan Schumaker7c513052011-03-24 17:12:24 +00003095 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003096 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003097 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return status;
3099}
3100
3101static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3102{
3103 struct nfs4_exception exception = { };
3104 int err;
3105 do {
3106 err = nfs4_handle_exception(NFS_SERVER(dir),
3107 _nfs4_proc_remove(dir, name),
3108 &exception);
3109 } while (exception.retry);
3110 return err;
3111}
3112
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003113static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003115 struct nfs_server *server = NFS_SERVER(dir);
3116 struct nfs_removeargs *args = msg->rpc_argp;
3117 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003119 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003121 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003124static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3125{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003126 nfs4_setup_sequence(NFS_SERVER(data->dir),
3127 &data->args.seq_args,
3128 &data->res.seq_res,
3129 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003132static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003134 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3135
Trond Myklebust14516c32010-07-31 14:29:06 -04003136 if (!nfs4_sequence_done(task, &res->seq_res))
3137 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003138 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003139 return 0;
3140 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003141 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Jeff Laytond3d41522010-09-17 17:31:57 -04003144static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3145{
3146 struct nfs_server *server = NFS_SERVER(dir);
3147 struct nfs_renameargs *arg = msg->rpc_argp;
3148 struct nfs_renameres *res = msg->rpc_resp;
3149
3150 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003151 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003152 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003153}
3154
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003155static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3156{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003157 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3158 &data->args.seq_args,
3159 &data->res.seq_res,
3160 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003161}
3162
3163static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3164 struct inode *new_dir)
3165{
3166 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3167
3168 if (!nfs4_sequence_done(task, &res->seq_res))
3169 return 0;
3170 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3171 return 0;
3172
3173 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003174 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003175 return 1;
3176}
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3179 struct inode *new_dir, struct qstr *new_name)
3180{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003181 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003182 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 .old_dir = NFS_FH(old_dir),
3184 .new_dir = NFS_FH(new_dir),
3185 .old_name = old_name,
3186 .new_name = new_name,
3187 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003188 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003189 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003190 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct rpc_message msg = {
3192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3193 .rpc_argp = &arg,
3194 .rpc_resp = &res,
3195 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003196 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Bryan Schumaker7c513052011-03-24 17:12:24 +00003198 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (!status) {
3200 update_changeattr(old_dir, &res.old_cinfo);
3201 update_changeattr(new_dir, &res.new_cinfo);
3202 }
3203 return status;
3204}
3205
3206static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3207 struct inode *new_dir, struct qstr *new_name)
3208{
3209 struct nfs4_exception exception = { };
3210 int err;
3211 do {
3212 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3213 _nfs4_proc_rename(old_dir, old_name,
3214 new_dir, new_name),
3215 &exception);
3216 } while (exception.retry);
3217 return err;
3218}
3219
3220static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3221{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003222 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct nfs4_link_arg arg = {
3224 .fh = NFS_FH(inode),
3225 .dir_fh = NFS_FH(dir),
3226 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003227 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003229 struct nfs4_link_res res = {
3230 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 struct rpc_message msg = {
3233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3234 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003235 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003237 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Trond Myklebust136f2622010-04-16 16:22:49 -04003239 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003240 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003241 goto out;
3242
Bryan Schumaker7c513052011-03-24 17:12:24 +00003243 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003244 if (!status) {
3245 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003246 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003247 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003248out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003249 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return status;
3251}
3252
3253static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3254{
3255 struct nfs4_exception exception = { };
3256 int err;
3257 do {
3258 err = nfs4_handle_exception(NFS_SERVER(inode),
3259 _nfs4_proc_link(inode, dir, name),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265struct nfs4_createdata {
3266 struct rpc_message msg;
3267 struct nfs4_create_arg arg;
3268 struct nfs4_create_res res;
3269 struct nfs_fh fh;
3270 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003271};
3272
3273static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3274 struct qstr *name, struct iattr *sattr, u32 ftype)
3275{
3276 struct nfs4_createdata *data;
3277
3278 data = kzalloc(sizeof(*data), GFP_KERNEL);
3279 if (data != NULL) {
3280 struct nfs_server *server = NFS_SERVER(dir);
3281
3282 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3283 data->msg.rpc_argp = &data->arg;
3284 data->msg.rpc_resp = &data->res;
3285 data->arg.dir_fh = NFS_FH(dir);
3286 data->arg.server = server;
3287 data->arg.name = name;
3288 data->arg.attrs = sattr;
3289 data->arg.ftype = ftype;
3290 data->arg.bitmask = server->attr_bitmask;
3291 data->res.server = server;
3292 data->res.fh = &data->fh;
3293 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003294 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003295 }
3296 return data;
3297}
3298
3299static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3300{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003301 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003302 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303 if (status == 0) {
3304 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3306 }
3307 return status;
3308}
3309
3310static void nfs4_free_createdata(struct nfs4_createdata *data)
3311{
3312 kfree(data);
3313}
3314
Chuck Lever4f390c12006-08-22 20:06:22 -04003315static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003316 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003318 struct nfs4_createdata *data;
3319 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Chuck Lever94a6d752006-08-22 20:06:23 -04003321 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003322 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003323
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003324 status = -ENOMEM;
3325 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3326 if (data == NULL)
3327 goto out;
3328
3329 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3330 data->arg.u.symlink.pages = &page;
3331 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003333 status = nfs4_do_create(dir, dentry, data);
3334
3335 nfs4_free_createdata(data);
3336out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return status;
3338}
3339
Chuck Lever4f390c12006-08-22 20:06:22 -04003340static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003341 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 struct nfs4_exception exception = { };
3344 int err;
3345 do {
3346 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003347 _nfs4_proc_symlink(dir, dentry, page,
3348 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 &exception);
3350 } while (exception.retry);
3351 return err;
3352}
3353
3354static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3355 struct iattr *sattr)
3356{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003357 struct nfs4_createdata *data;
3358 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003360 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3361 if (data == NULL)
3362 goto out;
3363
3364 status = nfs4_do_create(dir, dentry, data);
3365
3366 nfs4_free_createdata(data);
3367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return status;
3369}
3370
3371static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3372 struct iattr *sattr)
3373{
3374 struct nfs4_exception exception = { };
3375 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003376
3377 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 do {
3379 err = nfs4_handle_exception(NFS_SERVER(dir),
3380 _nfs4_proc_mkdir(dir, dentry, sattr),
3381 &exception);
3382 } while (exception.retry);
3383 return err;
3384}
3385
3386static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003387 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
3389 struct inode *dir = dentry->d_inode;
3390 struct nfs4_readdir_arg args = {
3391 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003392 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 .pgbase = 0,
3394 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003395 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003396 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 };
3398 struct nfs4_readdir_res res;
3399 struct rpc_message msg = {
3400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3401 .rpc_argp = &args,
3402 .rpc_resp = &res,
3403 .rpc_cred = cred,
3404 };
3405 int status;
3406
Harvey Harrison3110ff82008-05-02 13:42:44 -07003407 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003408 dentry->d_parent->d_name.name,
3409 dentry->d_name.name,
3410 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003411 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003413 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 -05003414 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003415 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003416 status += args.pgbase;
3417 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003418
3419 nfs_invalidate_atime(dir);
3420
Harvey Harrison3110ff82008-05-02 13:42:44 -07003421 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return status;
3423}
3424
3425static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003426 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct nfs4_exception exception = { };
3429 int err;
3430 do {
3431 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3432 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003433 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 &exception);
3435 } while (exception.retry);
3436 return err;
3437}
3438
3439static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3440 struct iattr *sattr, dev_t rdev)
3441{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003442 struct nfs4_createdata *data;
3443 int mode = sattr->ia_mode;
3444 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003446 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3447 if (data == NULL)
3448 goto out;
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003451 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003453 data->arg.ftype = NF4BLK;
3454 data->arg.u.device.specdata1 = MAJOR(rdev);
3455 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003458 data->arg.ftype = NF4CHR;
3459 data->arg.u.device.specdata1 = MAJOR(rdev);
3460 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003461 } else if (!S_ISSOCK(mode)) {
3462 status = -EINVAL;
3463 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003466 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003467out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003468 nfs4_free_createdata(data);
3469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return status;
3471}
3472
3473static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3474 struct iattr *sattr, dev_t rdev)
3475{
3476 struct nfs4_exception exception = { };
3477 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003478
3479 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 do {
3481 err = nfs4_handle_exception(NFS_SERVER(dir),
3482 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3483 &exception);
3484 } while (exception.retry);
3485 return err;
3486}
3487
3488static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3489 struct nfs_fsstat *fsstat)
3490{
3491 struct nfs4_statfs_arg args = {
3492 .fh = fhandle,
3493 .bitmask = server->attr_bitmask,
3494 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003495 struct nfs4_statfs_res res = {
3496 .fsstat = fsstat,
3497 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 struct rpc_message msg = {
3499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3500 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003501 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 };
3503
Trond Myklebust0e574af2005-10-27 22:12:38 -04003504 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
3508static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3509{
3510 struct nfs4_exception exception = { };
3511 int err;
3512 do {
3513 err = nfs4_handle_exception(server,
3514 _nfs4_proc_statfs(server, fhandle, fsstat),
3515 &exception);
3516 } while (exception.retry);
3517 return err;
3518}
3519
3520static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3521 struct nfs_fsinfo *fsinfo)
3522{
3523 struct nfs4_fsinfo_arg args = {
3524 .fh = fhandle,
3525 .bitmask = server->attr_bitmask,
3526 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003527 struct nfs4_fsinfo_res res = {
3528 .fsinfo = fsinfo,
3529 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 struct rpc_message msg = {
3531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3532 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003533 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 };
3535
Bryan Schumaker7c513052011-03-24 17:12:24 +00003536 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
3539static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3540{
3541 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003542 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 int err;
3544
3545 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003546 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3547 if (err == 0) {
3548 struct nfs_client *clp = server->nfs_client;
3549
3550 spin_lock(&clp->cl_lock);
3551 clp->cl_lease_time = fsinfo->lease_time * HZ;
3552 clp->cl_last_renewal = now;
3553 spin_unlock(&clp->cl_lock);
3554 break;
3555 }
3556 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 } while (exception.retry);
3558 return err;
3559}
3560
3561static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3562{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003563 int error;
3564
Trond Myklebust0e574af2005-10-27 22:12:38 -04003565 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003566 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003567 if (error == 0) {
3568 /* block layout checks this! */
3569 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003570 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003571 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003572
3573 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
3576static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3577 struct nfs_pathconf *pathconf)
3578{
3579 struct nfs4_pathconf_arg args = {
3580 .fh = fhandle,
3581 .bitmask = server->attr_bitmask,
3582 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003583 struct nfs4_pathconf_res res = {
3584 .pathconf = pathconf,
3585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 struct rpc_message msg = {
3587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3588 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003589 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 };
3591
3592 /* None of the pathconf attributes are mandatory to implement */
3593 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3594 memset(pathconf, 0, sizeof(*pathconf));
3595 return 0;
3596 }
3597
Trond Myklebust0e574af2005-10-27 22:12:38 -04003598 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003599 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
3602static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3603 struct nfs_pathconf *pathconf)
3604{
3605 struct nfs4_exception exception = { };
3606 int err;
3607
3608 do {
3609 err = nfs4_handle_exception(server,
3610 _nfs4_proc_pathconf(server, fhandle, pathconf),
3611 &exception);
3612 } while (exception.retry);
3613 return err;
3614}
3615
Trond Myklebust5521abf2013-03-16 20:54:34 -04003616int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003617 const struct nfs_open_context *ctx,
3618 const struct nfs_lock_context *l_ctx,
3619 fmode_t fmode)
3620{
3621 const struct nfs_lockowner *lockowner = NULL;
3622
3623 if (l_ctx != NULL)
3624 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003625 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003626}
3627EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3628
Trond Myklebust5521abf2013-03-16 20:54:34 -04003629static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3630 const struct nfs_open_context *ctx,
3631 const struct nfs_lock_context *l_ctx,
3632 fmode_t fmode)
3633{
3634 nfs4_stateid current_stateid;
3635
3636 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3637 return false;
3638 return nfs4_stateid_match(stateid, &current_stateid);
3639}
3640
3641static bool nfs4_error_stateid_expired(int err)
3642{
3643 switch (err) {
3644 case -NFS4ERR_DELEG_REVOKED:
3645 case -NFS4ERR_ADMIN_REVOKED:
3646 case -NFS4ERR_BAD_STATEID:
3647 case -NFS4ERR_STALE_STATEID:
3648 case -NFS4ERR_OLD_STATEID:
3649 case -NFS4ERR_OPENMODE:
3650 case -NFS4ERR_EXPIRED:
3651 return true;
3652 }
3653 return false;
3654}
3655
Benny Halevyd20581a2011-05-22 19:52:03 +03003656void __nfs4_read_done_cb(struct nfs_read_data *data)
3657{
Fred Isamancd841602012-04-20 14:47:44 -04003658 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003659}
3660
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003661static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Fred Isamancd841602012-04-20 14:47:44 -04003663 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003665 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003666 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003667 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003669
Benny Halevyd20581a2011-05-22 19:52:03 +03003670 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003672 renew_lease(server, data->timestamp);
3673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Trond Myklebust5521abf2013-03-16 20:54:34 -04003676static bool nfs4_read_stateid_changed(struct rpc_task *task,
3677 struct nfs_readargs *args)
3678{
3679
3680 if (!nfs4_error_stateid_expired(task->tk_status) ||
3681 nfs4_stateid_is_current(&args->stateid,
3682 args->context,
3683 args->lock_context,
3684 FMODE_READ))
3685 return false;
3686 rpc_restart_call_prepare(task);
3687 return true;
3688}
3689
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003690static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3691{
3692
3693 dprintk("--> %s\n", __func__);
3694
3695 if (!nfs4_sequence_done(task, &data->res.seq_res))
3696 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003697 if (nfs4_read_stateid_changed(task, &data->args))
3698 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003699 return data->read_done_cb ? data->read_done_cb(task, data) :
3700 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003701}
3702
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003703static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003706 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003707 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003708 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003711static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3712{
Trond Myklebust9b206142013-03-17 15:52:00 -04003713 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003714 &data->args.seq_args,
3715 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003716 task))
3717 return;
3718 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3719 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Fred Isamanb029bc92011-03-03 15:13:42 +00003722static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
Fred Isamancd841602012-04-20 14:47:44 -04003724 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003726 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003727 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003728 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003730 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003732 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003733 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Trond Myklebust5521abf2013-03-16 20:54:34 -04003737static bool nfs4_write_stateid_changed(struct rpc_task *task,
3738 struct nfs_writeargs *args)
3739{
3740
3741 if (!nfs4_error_stateid_expired(task->tk_status) ||
3742 nfs4_stateid_is_current(&args->stateid,
3743 args->context,
3744 args->lock_context,
3745 FMODE_WRITE))
3746 return false;
3747 rpc_restart_call_prepare(task);
3748 return true;
3749}
3750
Fred Isamanb029bc92011-03-03 15:13:42 +00003751static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3752{
3753 if (!nfs4_sequence_done(task, &data->res.seq_res))
3754 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003755 if (nfs4_write_stateid_changed(task, &data->args))
3756 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003757 return data->write_done_cb ? data->write_done_cb(task, data) :
3758 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003759}
3760
Trond Myklebust5a37f852012-04-28 14:55:16 -04003761static
3762bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003763{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003764 const struct nfs_pgio_header *hdr = data->header;
3765
3766 /* Don't request attributes for pNFS or O_DIRECT writes */
3767 if (data->ds_clp != NULL || hdr->dreq != NULL)
3768 return false;
3769 /* Otherwise, request attributes if and only if we don't hold
3770 * a delegation
3771 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003772 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003773}
Fred Isamana69aef12011-03-03 15:13:47 +00003774
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003775static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
Fred Isamancd841602012-04-20 14:47:44 -04003777 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003778
Trond Myklebust5a37f852012-04-28 14:55:16 -04003779 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003780 data->args.bitmask = NULL;
3781 data->res.fattr = NULL;
3782 } else
3783 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003784
Fred Isamanb029bc92011-03-03 15:13:42 +00003785 if (!data->write_done_cb)
3786 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003787 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 data->timestamp = jiffies;
3789
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003790 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003791 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003794static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3795{
Trond Myklebust9b206142013-03-17 15:52:00 -04003796 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003797 &data->args.seq_args,
3798 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003799 task))
3800 return;
3801 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3802 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
Fred Isaman0b7c0152012-04-20 14:47:39 -04003805static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3806{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003807 nfs4_setup_sequence(NFS_SERVER(data->inode),
3808 &data->args.seq_args,
3809 &data->res.seq_res,
3810 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Fred Isaman0b7c0152012-04-20 14:47:39 -04003813static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003816
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003817 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003818 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003819 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Fred Isaman0b7c0152012-04-20 14:47:39 -04003824static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003825{
3826 if (!nfs4_sequence_done(task, &data->res.seq_res))
3827 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003828 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003829}
3830
Fred Isaman0b7c0152012-04-20 14:47:39 -04003831static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003833 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003834
Fred Isaman0b7c0152012-04-20 14:47:39 -04003835 if (data->commit_done_cb == NULL)
3836 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003837 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003838 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003839 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003842struct nfs4_renewdata {
3843 struct nfs_client *client;
3844 unsigned long timestamp;
3845};
3846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847/*
3848 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3849 * standalone procedure for queueing an asynchronous RENEW.
3850 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003851static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003852{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003853 struct nfs4_renewdata *data = calldata;
3854 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003855
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003856 if (atomic_read(&clp->cl_count) > 1)
3857 nfs4_schedule_state_renewal(clp);
3858 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003859 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003860}
3861
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003862static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003864 struct nfs4_renewdata *data = calldata;
3865 struct nfs_client *clp = data->client;
3866 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003869 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003870 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3871 return;
3872 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003873 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003874 return;
3875 }
3876 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003878 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003881static const struct rpc_call_ops nfs4_renew_ops = {
3882 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003883 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884};
3885
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003886static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
3888 struct rpc_message msg = {
3889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3890 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003891 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003893 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003895 if (renew_flags == 0)
3896 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003897 if (!atomic_inc_not_zero(&clp->cl_count))
3898 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003899 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003900 if (data == NULL)
3901 return -ENOMEM;
3902 data->client = clp;
3903 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003904 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003905 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
3907
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003908static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
3910 struct rpc_message msg = {
3911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3912 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003913 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 };
3915 unsigned long now = jiffies;
3916 int status;
3917
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003918 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (status < 0)
3920 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003921 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return 0;
3923}
3924
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003925static inline int nfs4_server_supports_acls(struct nfs_server *server)
3926{
3927 return (server->caps & NFS_CAP_ACLS)
3928 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3929 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3930}
3931
Trond Myklebust21f498c2012-08-24 10:59:25 -04003932/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3933 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003934 * the stack.
3935 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003936#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003937
Neil Hormane9e3d722011-03-04 19:26:03 -05003938static int buf_to_pages_noslab(const void *buf, size_t buflen,
3939 struct page **pages, unsigned int *pgbase)
3940{
3941 struct page *newpage, **spages;
3942 int rc = 0;
3943 size_t len;
3944 spages = pages;
3945
3946 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003947 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003948 newpage = alloc_page(GFP_KERNEL);
3949
3950 if (newpage == NULL)
3951 goto unwind;
3952 memcpy(page_address(newpage), buf, len);
3953 buf += len;
3954 buflen -= len;
3955 *pages++ = newpage;
3956 rc++;
3957 } while (buflen != 0);
3958
3959 return rc;
3960
3961unwind:
3962 for(; rc > 0; rc--)
3963 __free_page(spages[rc-1]);
3964 return -ENOMEM;
3965}
3966
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003967struct nfs4_cached_acl {
3968 int cached;
3969 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003970 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003971};
3972
3973static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003974{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003975 struct nfs_inode *nfsi = NFS_I(inode);
3976
3977 spin_lock(&inode->i_lock);
3978 kfree(nfsi->nfs4_acl);
3979 nfsi->nfs4_acl = acl;
3980 spin_unlock(&inode->i_lock);
3981}
3982
3983static void nfs4_zap_acl_attr(struct inode *inode)
3984{
3985 nfs4_set_cached_acl(inode, NULL);
3986}
3987
3988static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3989{
3990 struct nfs_inode *nfsi = NFS_I(inode);
3991 struct nfs4_cached_acl *acl;
3992 int ret = -ENOENT;
3993
3994 spin_lock(&inode->i_lock);
3995 acl = nfsi->nfs4_acl;
3996 if (acl == NULL)
3997 goto out;
3998 if (buf == NULL) /* user is just asking for length */
3999 goto out_len;
4000 if (acl->cached == 0)
4001 goto out;
4002 ret = -ERANGE; /* see getxattr(2) man page */
4003 if (acl->len > buflen)
4004 goto out;
4005 memcpy(buf, acl->data, acl->len);
4006out_len:
4007 ret = acl->len;
4008out:
4009 spin_unlock(&inode->i_lock);
4010 return ret;
4011}
4012
Sachin Prabhu5794d212012-04-17 14:36:40 +01004013static 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 +00004014{
4015 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004016 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004017
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004018 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004019 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004020 if (acl == NULL)
4021 goto out;
4022 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004023 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004024 } else {
4025 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4026 if (acl == NULL)
4027 goto out;
4028 acl->cached = 0;
4029 }
4030 acl->len = acl_len;
4031out:
4032 nfs4_set_cached_acl(inode, acl);
4033}
4034
Andy Adamsonbf118a32011-12-07 11:55:27 -05004035/*
4036 * The getxattr API returns the required buffer length when called with a
4037 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4038 * the required buf. On a NULL buf, we send a page of data to the server
4039 * guessing that the ACL request can be serviced by a page. If so, we cache
4040 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4041 * the cache. If not so, we throw away the page, and cache the required
4042 * length. The next getxattr call will then produce another round trip to
4043 * the server, this time with the input buf of the required size.
4044 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004045static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004046{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004047 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004048 struct nfs_getaclargs args = {
4049 .fh = NFS_FH(inode),
4050 .acl_pages = pages,
4051 .acl_len = buflen,
4052 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004053 struct nfs_getaclres res = {
4054 .acl_len = buflen,
4055 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004056 struct rpc_message msg = {
4057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4058 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004059 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004060 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004061 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4062 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004063
Andy Adamsonbf118a32011-12-07 11:55:27 -05004064 /* As long as we're doing a round trip to the server anyway,
4065 * let's be prepared for a page of acl data. */
4066 if (npages == 0)
4067 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004068 if (npages > ARRAY_SIZE(pages))
4069 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004070
Andy Adamsonbf118a32011-12-07 11:55:27 -05004071 for (i = 0; i < npages; i++) {
4072 pages[i] = alloc_page(GFP_KERNEL);
4073 if (!pages[i])
4074 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004075 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004076
4077 /* for decoding across pages */
4078 res.acl_scratch = alloc_page(GFP_KERNEL);
4079 if (!res.acl_scratch)
4080 goto out_free;
4081
Andy Adamsonbf118a32011-12-07 11:55:27 -05004082 args.acl_len = npages * PAGE_SIZE;
4083 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004084
Peng Taode040be2012-01-10 22:42:47 +08004085 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004086 __func__, buf, buflen, npages, args.acl_len);
4087 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4088 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004089 if (ret)
4090 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004091
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004092 /* Handle the case where the passed-in buffer is too short */
4093 if (res.acl_flags & NFS4_ACL_TRUNC) {
4094 /* Did the user only issue a request for the acl length? */
4095 if (buf == NULL)
4096 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004097 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004098 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004099 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004100 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004101 if (buf) {
4102 if (res.acl_len > buflen) {
4103 ret = -ERANGE;
4104 goto out_free;
4105 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004106 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004107 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004108out_ok:
4109 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004110out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004111 for (i = 0; i < npages; i++)
4112 if (pages[i])
4113 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004114 if (res.acl_scratch)
4115 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004116 return ret;
4117}
4118
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004119static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4120{
4121 struct nfs4_exception exception = { };
4122 ssize_t ret;
4123 do {
4124 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4125 if (ret >= 0)
4126 break;
4127 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4128 } while (exception.retry);
4129 return ret;
4130}
4131
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004132static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4133{
4134 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004135 int ret;
4136
4137 if (!nfs4_server_supports_acls(server))
4138 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004139 ret = nfs_revalidate_inode(server, inode);
4140 if (ret < 0)
4141 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004142 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4143 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004144 ret = nfs4_read_cached_acl(inode, buf, buflen);
4145 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004146 /* -ENOENT is returned if there is no ACL or if there is an ACL
4147 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004148 return ret;
4149 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004150}
4151
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004152static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004153{
4154 struct nfs_server *server = NFS_SERVER(inode);
4155 struct page *pages[NFS4ACL_MAXPAGES];
4156 struct nfs_setaclargs arg = {
4157 .fh = NFS_FH(inode),
4158 .acl_pages = pages,
4159 .acl_len = buflen,
4160 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004161 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004162 struct rpc_message msg = {
4163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4164 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004165 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004166 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004167 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004168 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004169
4170 if (!nfs4_server_supports_acls(server))
4171 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004172 if (npages > ARRAY_SIZE(pages))
4173 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004174 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4175 if (i < 0)
4176 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004177 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004178 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004179
4180 /*
4181 * Free each page after tx, so the only ref left is
4182 * held by the network stack
4183 */
4184 for (; i > 0; i--)
4185 put_page(pages[i-1]);
4186
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004187 /*
4188 * Acl update can result in inode attribute update.
4189 * so mark the attribute cache invalid.
4190 */
4191 spin_lock(&inode->i_lock);
4192 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4193 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004194 nfs_access_zap_cache(inode);
4195 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004196 return ret;
4197}
4198
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004199static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4200{
4201 struct nfs4_exception exception = { };
4202 int err;
4203 do {
4204 err = nfs4_handle_exception(NFS_SERVER(inode),
4205 __nfs4_proc_set_acl(inode, buf, buflen),
4206 &exception);
4207 } while (exception.retry);
4208 return err;
4209}
4210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004212nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004214 struct nfs_client *clp = server->nfs_client;
4215
4216 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return 0;
4218 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004219 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004220 case -NFS4ERR_ADMIN_REVOKED:
4221 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004222 if (state == NULL)
4223 break;
4224 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004225 case -NFS4ERR_OPENMODE:
4226 if (state == NULL)
4227 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004228 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4229 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004230 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004231 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004232 if (state != NULL) {
4233 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4234 goto stateid_invalid;
4235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004237 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004238 nfs4_schedule_lease_recovery(clp);
4239 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004240#if defined(CONFIG_NFS_V4_1)
4241 case -NFS4ERR_BADSESSION:
4242 case -NFS4ERR_BADSLOT:
4243 case -NFS4ERR_BAD_HIGH_SLOT:
4244 case -NFS4ERR_DEADSESSION:
4245 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4246 case -NFS4ERR_SEQ_FALSE_RETRY:
4247 case -NFS4ERR_SEQ_MISORDERED:
4248 dprintk("%s ERROR %d, Reset session\n", __func__,
4249 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004250 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004251 task->tk_status = 0;
4252 return -EAGAIN;
4253#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004255 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004256 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4258 task->tk_status = 0;
4259 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004260 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 case -NFS4ERR_OLD_STATEID:
4262 task->tk_status = 0;
4263 return -EAGAIN;
4264 }
4265 task->tk_status = nfs4_map_errors(task->tk_status);
4266 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004267stateid_invalid:
4268 task->tk_status = -EIO;
4269 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004270wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004271 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004272 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4273 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4274 task->tk_status = 0;
4275 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Chuck Leverf0920752012-05-21 22:45:41 -04004278static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4279 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004280{
4281 __be32 verf[2];
4282
Chuck Lever2c820d92012-05-21 22:45:33 -04004283 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4284 /* An impossible timestamp guarantees this value
4285 * will never match a generated boot time. */
4286 verf[0] = 0;
4287 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4288 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004289 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4290 verf[0] = (__be32)nn->boot_time.tv_sec;
4291 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004292 }
Chuck Levercd937102012-03-02 17:14:31 -05004293 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4294}
4295
Chuck Levere984a552012-09-14 17:24:21 -04004296static unsigned int
4297nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4298 char *buf, size_t len)
4299{
4300 unsigned int result;
4301
4302 rcu_read_lock();
4303 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4304 clp->cl_ipaddr,
4305 rpc_peeraddr2str(clp->cl_rpcclient,
4306 RPC_DISPLAY_ADDR),
4307 rpc_peeraddr2str(clp->cl_rpcclient,
4308 RPC_DISPLAY_PROTO));
4309 rcu_read_unlock();
4310 return result;
4311}
4312
4313static unsigned int
4314nfs4_init_uniform_client_string(const struct nfs_client *clp,
4315 char *buf, size_t len)
4316{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004317 char *nodename = clp->cl_rpcclient->cl_nodename;
4318
4319 if (nfs4_client_id_uniquifier[0] != '\0')
4320 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004321 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4322 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004323 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004324}
4325
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004326/**
4327 * nfs4_proc_setclientid - Negotiate client ID
4328 * @clp: state data structure
4329 * @program: RPC program for NFSv4 callback service
4330 * @port: IP port number for NFS4 callback service
4331 * @cred: RPC credential to use for this call
4332 * @res: where to place the result
4333 *
4334 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4335 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004336int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4337 unsigned short port, struct rpc_cred *cred,
4338 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
4340 nfs4_verifier sc_verifier;
4341 struct nfs4_setclientid setclientid = {
4342 .sc_verifier = &sc_verifier,
4343 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004344 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 };
4346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4348 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004349 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004350 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004352 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Chuck Leverde734832012-07-11 16:30:50 -04004354 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004355 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004356 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4357 setclientid.sc_name_len =
4358 nfs4_init_uniform_client_string(clp,
4359 setclientid.sc_name,
4360 sizeof(setclientid.sc_name));
4361 else
4362 setclientid.sc_name_len =
4363 nfs4_init_nonuniform_client_string(clp,
4364 setclientid.sc_name,
4365 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004366 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004367 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004368 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004369 sizeof(setclientid.sc_netid),
4370 rpc_peeraddr2str(clp->cl_rpcclient,
4371 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004372 rcu_read_unlock();
4373 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004374 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 clp->cl_ipaddr, port >> 8, port & 255);
4376
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004377 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4378 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4379 setclientid.sc_name_len, setclientid.sc_name);
4380 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4381 dprintk("NFS reply setclientid: %d\n", status);
4382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004385/**
4386 * nfs4_proc_setclientid_confirm - Confirm client ID
4387 * @clp: state data structure
4388 * @res: result of a previous SETCLIENTID
4389 * @cred: RPC credential to use for this call
4390 *
4391 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4392 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004393int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004394 struct nfs4_setclientid_res *arg,
4395 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004399 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004400 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 int status;
4403
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004404 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4405 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4406 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004407 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004408 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 return status;
4410}
4411
Trond Myklebustfe650402006-01-03 09:55:18 +01004412struct nfs4_delegreturndata {
4413 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004414 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004415 struct nfs_fh fh;
4416 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004417 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004418 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004419 int rpc_status;
4420};
4421
Trond Myklebustfe650402006-01-03 09:55:18 +01004422static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4423{
4424 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004425
Trond Myklebust14516c32010-07-31 14:29:06 -04004426 if (!nfs4_sequence_done(task, &data->res.seq_res))
4427 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004428
Ricardo Labiaga79708862009-12-07 09:23:21 -05004429 switch (task->tk_status) {
4430 case -NFS4ERR_STALE_STATEID:
4431 case -NFS4ERR_EXPIRED:
4432 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004433 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004434 break;
4435 default:
4436 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4437 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004438 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004439 return;
4440 }
4441 }
4442 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004443}
4444
4445static void nfs4_delegreturn_release(void *calldata)
4446{
Trond Myklebustfe650402006-01-03 09:55:18 +01004447 kfree(calldata);
4448}
4449
Andy Adamson938e1012009-04-01 09:22:28 -04004450#if defined(CONFIG_NFS_V4_1)
4451static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4452{
4453 struct nfs4_delegreturndata *d_data;
4454
4455 d_data = (struct nfs4_delegreturndata *)data;
4456
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004457 nfs4_setup_sequence(d_data->res.server,
4458 &d_data->args.seq_args,
4459 &d_data->res.seq_res,
4460 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004461}
4462#endif /* CONFIG_NFS_V4_1 */
4463
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004464static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004465#if defined(CONFIG_NFS_V4_1)
4466 .rpc_call_prepare = nfs4_delegreturn_prepare,
4467#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004468 .rpc_call_done = nfs4_delegreturn_done,
4469 .rpc_release = nfs4_delegreturn_release,
4470};
4471
Trond Myklebuste6f81072008-01-24 18:14:34 -05004472static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004473{
4474 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004475 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004476 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004477 struct rpc_message msg = {
4478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4479 .rpc_cred = cred,
4480 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004481 struct rpc_task_setup task_setup_data = {
4482 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004483 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004484 .callback_ops = &nfs4_delegreturn_ops,
4485 .flags = RPC_TASK_ASYNC,
4486 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004487 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004488
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004489 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004490 if (data == NULL)
4491 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004492 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004493 data->args.fhandle = &data->fh;
4494 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004495 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004496 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004497 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004498 data->res.fattr = &data->fattr;
4499 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004500 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004501 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004502 data->rpc_status = 0;
4503
Trond Myklebustc970aa82007-07-14 15:39:59 -04004504 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004505 msg.rpc_argp = &data->args;
4506 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004507 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004508 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004509 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004510 if (!issync)
4511 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004512 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004513 if (status != 0)
4514 goto out;
4515 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004516 if (status == 0)
4517 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4518 else
4519 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004520out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004521 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004522 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523}
4524
Trond Myklebuste6f81072008-01-24 18:14:34 -05004525int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 struct nfs_server *server = NFS_SERVER(inode);
4528 struct nfs4_exception exception = { };
4529 int err;
4530 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004531 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 switch (err) {
4533 case -NFS4ERR_STALE_STATEID:
4534 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 case 0:
4536 return 0;
4537 }
4538 err = nfs4_handle_exception(server, err, &exception);
4539 } while (exception.retry);
4540 return err;
4541}
4542
4543#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4544#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4545
4546/*
4547 * sleep, with exponential backoff, and retry the LOCK operation.
4548 */
4549static unsigned long
4550nfs4_set_lock_task_retry(unsigned long timeout)
4551{
Jeff Laytond3103102011-12-01 22:44:39 +01004552 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 timeout <<= 1;
4554 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4555 return NFS4_LOCK_MAXTIMEOUT;
4556 return timeout;
4557}
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4560{
4561 struct inode *inode = state->inode;
4562 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004563 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004564 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004566 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004568 struct nfs_lockt_res res = {
4569 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 };
4571 struct rpc_message msg = {
4572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4573 .rpc_argp = &arg,
4574 .rpc_resp = &res,
4575 .rpc_cred = state->owner->so_cred,
4576 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct nfs4_lock_state *lsp;
4578 int status;
4579
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004580 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004581 status = nfs4_set_lock_state(state, request);
4582 if (status != 0)
4583 goto out;
4584 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004585 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004586 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004587 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004588 switch (status) {
4589 case 0:
4590 request->fl_type = F_UNLCK;
4591 break;
4592 case -NFS4ERR_DENIED:
4593 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004595 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return status;
4598}
4599
4600static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4601{
4602 struct nfs4_exception exception = { };
4603 int err;
4604
4605 do {
4606 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4607 _nfs4_proc_getlk(state, cmd, request),
4608 &exception);
4609 } while (exception.retry);
4610 return err;
4611}
4612
4613static int do_vfs_lock(struct file *file, struct file_lock *fl)
4614{
4615 int res = 0;
4616 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4617 case FL_POSIX:
4618 res = posix_lock_file_wait(file, fl);
4619 break;
4620 case FL_FLOCK:
4621 res = flock_lock_file_wait(file, fl);
4622 break;
4623 default:
4624 BUG();
4625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return res;
4627}
4628
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004629struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004630 struct nfs_locku_args arg;
4631 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004632 struct nfs4_lock_state *lsp;
4633 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004634 struct file_lock fl;
4635 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004636 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004637};
4638
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004639static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4640 struct nfs_open_context *ctx,
4641 struct nfs4_lock_state *lsp,
4642 struct nfs_seqid *seqid)
4643{
4644 struct nfs4_unlockdata *p;
4645 struct inode *inode = lsp->ls_state->inode;
4646
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004647 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004648 if (p == NULL)
4649 return NULL;
4650 p->arg.fh = NFS_FH(inode);
4651 p->arg.fl = &p->fl;
4652 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004653 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004654 p->arg.stateid = &lsp->ls_stateid;
4655 p->lsp = lsp;
4656 atomic_inc(&lsp->ls_count);
4657 /* Ensure we don't close file until we're done freeing locks! */
4658 p->ctx = get_nfs_open_context(ctx);
4659 memcpy(&p->fl, fl, sizeof(p->fl));
4660 p->server = NFS_SERVER(inode);
4661 return p;
4662}
4663
Trond Myklebust06f814a2006-01-03 09:55:07 +01004664static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004665{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004666 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004667 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004668 nfs4_put_lock_state(calldata->lsp);
4669 put_nfs_open_context(calldata->ctx);
4670 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004671}
4672
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004673static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004674{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004675 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004676
Trond Myklebust14516c32010-07-31 14:29:06 -04004677 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4678 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004679 switch (task->tk_status) {
4680 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004681 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4682 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004683 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004684 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004685 case -NFS4ERR_BAD_STATEID:
4686 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004687 case -NFS4ERR_STALE_STATEID:
4688 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004689 break;
4690 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004691 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004692 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004693 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004694 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004695}
4696
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004697static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004698{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004699 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004701 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004702 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004703 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004704 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004705 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004706 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004707 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004708 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004709 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004710 &calldata->res.seq_res,
4711 task) != 0)
4712 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004713 return;
4714out_no_action:
4715 task->tk_action = NULL;
4716out_wait:
4717 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004720static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004721 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004722 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004723 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004724};
4725
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4727 struct nfs_open_context *ctx,
4728 struct nfs4_lock_state *lsp,
4729 struct nfs_seqid *seqid)
4730{
4731 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004732 struct rpc_message msg = {
4733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4734 .rpc_cred = ctx->cred,
4735 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004736 struct rpc_task_setup task_setup_data = {
4737 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004738 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004739 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004740 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004741 .flags = RPC_TASK_ASYNC,
4742 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743
Frank Filz137d6ac2007-07-09 15:32:29 -07004744 /* Ensure this is an unlock - when canceling a lock, the
4745 * canceled lock is passed in, and it won't be an unlock.
4746 */
4747 fl->fl_type = F_UNLCK;
4748
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4750 if (data == NULL) {
4751 nfs_free_seqid(seqid);
4752 return ERR_PTR(-ENOMEM);
4753 }
4754
Trond Myklebust9d12b212012-01-17 22:04:25 -05004755 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004756 msg.rpc_argp = &data->arg;
4757 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004758 task_setup_data.callback_data = data;
4759 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004760}
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4763{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004764 struct inode *inode = state->inode;
4765 struct nfs4_state_owner *sp = state->owner;
4766 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004767 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004768 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004769 struct rpc_task *task;
4770 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004771 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Trond Myklebust9b073572006-06-29 16:38:34 -04004773 status = nfs4_set_lock_state(state, request);
4774 /* Unlock _before_ we do the RPC call */
4775 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004776 /* Exclude nfs_delegation_claim_locks() */
4777 mutex_lock(&sp->so_delegreturn_mutex);
4778 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004779 down_read(&nfsi->rwsem);
4780 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4781 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004782 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004783 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004784 }
4785 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004786 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004787 if (status != 0)
4788 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004789 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004790 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04004791 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4792 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004793 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004794 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004795 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004796 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004797 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004798 status = PTR_ERR(task);
4799 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004800 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004802 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004803out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004804 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004805 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
4807
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808struct nfs4_lockdata {
4809 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004810 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 struct nfs4_lock_state *lsp;
4812 struct nfs_open_context *ctx;
4813 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004814 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004815 int rpc_status;
4816 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004817 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818};
4819
4820static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004821 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4822 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004823{
4824 struct nfs4_lockdata *p;
4825 struct inode *inode = lsp->ls_state->inode;
4826 struct nfs_server *server = NFS_SERVER(inode);
4827
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004828 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004829 if (p == NULL)
4830 return NULL;
4831
4832 p->arg.fh = NFS_FH(inode);
4833 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004834 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004835 if (p->arg.open_seqid == NULL)
4836 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004837 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004838 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004839 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004841 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004842 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004843 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004844 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004846 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 atomic_inc(&lsp->ls_count);
4848 p->ctx = get_nfs_open_context(ctx);
4849 memcpy(&p->fl, fl, sizeof(p->fl));
4850 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004851out_free_seqid:
4852 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004853out_free:
4854 kfree(p);
4855 return NULL;
4856}
4857
4858static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4859{
4860 struct nfs4_lockdata *data = calldata;
4861 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Harvey Harrison3110ff82008-05-02 13:42:44 -07004863 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004864 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004865 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004866 /* Do we need to do an open_to_lock_owner? */
4867 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004868 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004869 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004870 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004871 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004872 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004873 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004874 } else
4875 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004876 if (!nfs4_valid_open_stateid(state)) {
4877 data->rpc_status = -EBADF;
4878 task->tk_action = NULL;
4879 goto out_release_open_seqid;
4880 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004881 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004882 if (nfs4_setup_sequence(data->server,
4883 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004884 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004885 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004886 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004887out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004888 nfs_release_seqid(data->arg.open_seqid);
4889out_release_lock_seqid:
4890 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004891out_wait:
4892 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004893 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004894}
4895
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004896static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4897{
4898 struct nfs4_lockdata *data = calldata;
4899
Harvey Harrison3110ff82008-05-02 13:42:44 -07004900 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004901
Trond Myklebust14516c32010-07-31 14:29:06 -04004902 if (!nfs4_sequence_done(task, &data->res.seq_res))
4903 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004904
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004905 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004906 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004907 if (data->rpc_status == 0)
4908 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4909 else
4910 goto out;
4911 }
4912 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004913 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004914 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004915 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004916 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004917out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004918 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004919}
4920
4921static void nfs4_lock_release(void *calldata)
4922{
4923 struct nfs4_lockdata *data = calldata;
4924
Harvey Harrison3110ff82008-05-02 13:42:44 -07004925 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004926 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004927 if (data->cancelled != 0) {
4928 struct rpc_task *task;
4929 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4930 data->arg.lock_seqid);
4931 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004932 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004933 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004934 } else
4935 nfs_free_seqid(data->arg.lock_seqid);
4936 nfs4_put_lock_state(data->lsp);
4937 put_nfs_open_context(data->ctx);
4938 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004939 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004940}
4941
4942static const struct rpc_call_ops nfs4_lock_ops = {
4943 .rpc_call_prepare = nfs4_lock_prepare,
4944 .rpc_call_done = nfs4_lock_done,
4945 .rpc_release = nfs4_lock_release,
4946};
4947
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004948static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4949{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004950 switch (error) {
4951 case -NFS4ERR_ADMIN_REVOKED:
4952 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004953 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004954 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004955 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004956 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004957 break;
4958 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004959 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004960 case -NFS4ERR_EXPIRED:
4961 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004962 };
4963}
4964
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004965static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004966{
4967 struct nfs4_lockdata *data;
4968 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004969 struct rpc_message msg = {
4970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4971 .rpc_cred = state->owner->so_cred,
4972 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004973 struct rpc_task_setup task_setup_data = {
4974 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004975 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004976 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004977 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004978 .flags = RPC_TASK_ASYNC,
4979 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004980 int ret;
4981
Harvey Harrison3110ff82008-05-02 13:42:44 -07004982 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004983 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004984 fl->fl_u.nfs4_fl.owner,
4985 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004986 if (data == NULL)
4987 return -ENOMEM;
4988 if (IS_SETLKW(cmd))
4989 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004990 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004991 msg.rpc_argp = &data->arg;
4992 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004993 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004994 if (recovery_type > NFS_LOCK_NEW) {
4995 if (recovery_type == NFS_LOCK_RECLAIM)
4996 data->arg.reclaim = NFS_LOCK_RECLAIM;
4997 nfs4_set_sequence_privileged(&data->arg.seq_args);
4998 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004999 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005000 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005001 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005002 ret = nfs4_wait_for_completion_rpc_task(task);
5003 if (ret == 0) {
5004 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005005 if (ret)
5006 nfs4_handle_setlk_error(data->server, data->lsp,
5007 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005008 } else
5009 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005010 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005011 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005012 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
5015static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5016{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005017 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005018 struct nfs4_exception exception = {
5019 .inode = state->inode,
5020 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005021 int err;
5022
5023 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005024 /* Cache the lock if possible... */
5025 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5026 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005027 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005028 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005029 break;
5030 nfs4_handle_exception(server, err, &exception);
5031 } while (exception.retry);
5032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
5035static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5036{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005037 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005038 struct nfs4_exception exception = {
5039 .inode = state->inode,
5040 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005041 int err;
5042
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005043 err = nfs4_set_lock_state(state, request);
5044 if (err != 0)
5045 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005046 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005047 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5048 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005049 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005050 switch (err) {
5051 default:
5052 goto out;
5053 case -NFS4ERR_GRACE:
5054 case -NFS4ERR_DELAY:
5055 nfs4_handle_exception(server, err, &exception);
5056 err = 0;
5057 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005058 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005059out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061}
5062
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005063#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005064/**
5065 * nfs41_check_expired_locks - possibly free a lock stateid
5066 *
5067 * @state: NFSv4 state for an inode
5068 *
5069 * Returns NFS_OK if recovery for this stateid is now finished.
5070 * Otherwise a negative NFS4ERR value is returned.
5071 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005072static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005073{
Chuck Levereb64cf92012-07-11 16:30:05 -04005074 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005075 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005076 struct nfs_server *server = NFS_SERVER(state->inode);
5077
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005078 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005079 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005080 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5081
5082 status = nfs41_test_stateid(server,
5083 &lsp->ls_stateid,
5084 cred);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005085 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005086 /* Free the stateid unless the server
5087 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005088 if (status != -NFS4ERR_BAD_STATEID)
5089 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005090 &lsp->ls_stateid,
5091 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005092 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005093 ret = status;
5094 }
5095 }
5096 };
5097
5098 return ret;
5099}
5100
5101static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5102{
5103 int status = NFS_OK;
5104
5105 if (test_bit(LK_STATE_IN_USE, &state->flags))
5106 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005107 if (status != NFS_OK)
5108 status = nfs4_lock_expired(state, request);
5109 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005110}
5111#endif
5112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5114{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005115 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005116 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005117 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005118 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005119 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005121 if ((fl_flags & FL_POSIX) &&
5122 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5123 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005124 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005125 status = nfs4_set_lock_state(state, request);
5126 if (status != 0)
5127 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005128 request->fl_flags |= FL_ACCESS;
5129 status = do_vfs_lock(request->fl_file, request);
5130 if (status < 0)
5131 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005132 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005133 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005134 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005135 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005136 request->fl_flags = fl_flags & ~FL_SLEEP;
5137 status = do_vfs_lock(request->fl_file, request);
5138 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005139 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005140 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5141 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005142 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005143 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005144 goto out;
5145 down_read(&nfsi->rwsem);
5146 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5147 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005148 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005149 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005150 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005151 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005152 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005153 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5154 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005155out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005156 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005157out:
5158 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 return status;
5160}
5161
5162static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5163{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005164 struct nfs4_exception exception = {
5165 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005166 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 int err;
5169
5170 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005171 err = _nfs4_proc_setlk(state, cmd, request);
5172 if (err == -NFS4ERR_DENIED)
5173 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005175 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 } while (exception.retry);
5177 return err;
5178}
5179
5180static int
5181nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5182{
5183 struct nfs_open_context *ctx;
5184 struct nfs4_state *state;
5185 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5186 int status;
5187
5188 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005189 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 state = ctx->state;
5191
5192 if (request->fl_start < 0 || request->fl_end < 0)
5193 return -EINVAL;
5194
Trond Myklebustd9531262009-07-21 19:22:38 -04005195 if (IS_GETLK(cmd)) {
5196 if (state != NULL)
5197 return nfs4_proc_getlk(state, F_GETLK, request);
5198 return 0;
5199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
5201 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5202 return -EINVAL;
5203
Trond Myklebustd9531262009-07-21 19:22:38 -04005204 if (request->fl_type == F_UNLCK) {
5205 if (state != NULL)
5206 return nfs4_proc_unlck(state, cmd, request);
5207 return 0;
5208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Trond Myklebustd9531262009-07-21 19:22:38 -04005210 if (state == NULL)
5211 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005212 /*
5213 * Don't rely on the VFS having checked the file open mode,
5214 * since it won't do this for flock() locks.
5215 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005216 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005217 case F_RDLCK:
5218 if (!(filp->f_mode & FMODE_READ))
5219 return -EBADF;
5220 break;
5221 case F_WRLCK:
5222 if (!(filp->f_mode & FMODE_WRITE))
5223 return -EBADF;
5224 }
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 do {
5227 status = nfs4_proc_setlk(state, cmd, request);
5228 if ((status != -EAGAIN) || IS_SETLK(cmd))
5229 break;
5230 timeout = nfs4_set_lock_task_retry(timeout);
5231 status = -ERESTARTSYS;
5232 if (signalled())
5233 break;
5234 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 return status;
5236}
5237
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005238int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005239{
5240 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005241 int err;
5242
5243 err = nfs4_set_lock_state(state, fl);
5244 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005245 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005246 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005247 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005248}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005249
Trond Myklebustcf470c32012-03-07 13:49:12 -05005250struct nfs_release_lockowner_data {
5251 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005252 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005253 struct nfs_release_lockowner_args args;
5254};
5255
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005256static void nfs4_release_lockowner_release(void *calldata)
5257{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005258 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005259 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005260 kfree(calldata);
5261}
5262
Trond Myklebust17280172012-03-11 13:11:00 -04005263static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005264 .rpc_release = nfs4_release_lockowner_release,
5265};
5266
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005267static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005268{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005269 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005270 struct rpc_message msg = {
5271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5272 };
5273
5274 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005275 return -EINVAL;
5276 data = kmalloc(sizeof(*data), GFP_NOFS);
5277 if (!data)
5278 return -ENOMEM;
5279 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005280 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005281 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5282 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5283 data->args.lock_owner.s_dev = server->s_dev;
5284 msg.rpc_argp = &data->args;
5285 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5286 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005287}
5288
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005289#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5290
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005291static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5292 const void *buf, size_t buflen,
5293 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005294{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005295 if (strcmp(key, "") != 0)
5296 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005297
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005298 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005299}
5300
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005301static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5302 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005303{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005304 if (strcmp(key, "") != 0)
5305 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005306
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005307 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005308}
5309
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005310static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5311 size_t list_len, const char *name,
5312 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005313{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005314 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005315
J. Bruce Fields096455a2006-03-20 23:23:42 -05005316 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5317 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005318
5319 if (list && len <= list_len)
5320 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005321 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005322}
5323
Andy Adamson533eb462011-06-13 18:25:56 -04005324/*
5325 * nfs_fhget will use either the mounted_on_fileid or the fileid
5326 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005327static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5328{
Andy Adamson533eb462011-06-13 18:25:56 -04005329 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5330 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5331 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005332 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005333 return;
5334
5335 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005336 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005337 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5338 fattr->nlink = 2;
5339}
5340
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005341static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5342 const struct qstr *name,
5343 struct nfs4_fs_locations *fs_locations,
5344 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005345{
5346 struct nfs_server *server = NFS_SERVER(dir);
5347 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005348 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005349 };
5350 struct nfs4_fs_locations_arg args = {
5351 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005352 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005353 .page = page,
5354 .bitmask = bitmask,
5355 };
Benny Halevy22958462009-04-01 09:22:02 -04005356 struct nfs4_fs_locations_res res = {
5357 .fs_locations = fs_locations,
5358 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005359 struct rpc_message msg = {
5360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5361 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005362 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005363 };
5364 int status;
5365
Harvey Harrison3110ff82008-05-02 13:42:44 -07005366 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005367
5368 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5369 * is not supported */
5370 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5371 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5372 else
5373 bitmask[0] |= FATTR4_WORD0_FILEID;
5374
Trond Myklebustc228fd32007-01-13 02:28:11 -05005375 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005376 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005377 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005378 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005379 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005380 return status;
5381}
5382
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005383int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5384 const struct qstr *name,
5385 struct nfs4_fs_locations *fs_locations,
5386 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005387{
5388 struct nfs4_exception exception = { };
5389 int err;
5390 do {
5391 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005392 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005393 &exception);
5394 } while (exception.retry);
5395 return err;
5396}
5397
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005398static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5399{
5400 int status;
5401 struct nfs4_secinfo_arg args = {
5402 .dir_fh = NFS_FH(dir),
5403 .name = name,
5404 };
5405 struct nfs4_secinfo_res res = {
5406 .flavors = flavors,
5407 };
5408 struct rpc_message msg = {
5409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5410 .rpc_argp = &args,
5411 .rpc_resp = &res,
5412 };
5413
5414 dprintk("NFS call secinfo %s\n", name->name);
5415 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5416 dprintk("NFS reply secinfo: %d\n", status);
5417 return status;
5418}
5419
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005420int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5421 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005422{
5423 struct nfs4_exception exception = { };
5424 int err;
5425 do {
5426 err = nfs4_handle_exception(NFS_SERVER(dir),
5427 _nfs4_proc_secinfo(dir, name, flavors),
5428 &exception);
5429 } while (exception.retry);
5430 return err;
5431}
5432
Andy Adamson557134a2009-04-01 09:21:53 -04005433#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005434/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005435 * Check the exchange flags returned by the server for invalid flags, having
5436 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5437 * DS flags set.
5438 */
5439static int nfs4_check_cl_exchange_flags(u32 flags)
5440{
5441 if (flags & ~EXCHGID4_FLAG_MASK_R)
5442 goto out_inval;
5443 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5444 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5445 goto out_inval;
5446 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5447 goto out_inval;
5448 return NFS_OK;
5449out_inval:
5450 return -NFS4ERR_INVAL;
5451}
5452
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005453static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005454nfs41_same_server_scope(struct nfs41_server_scope *a,
5455 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005456{
5457 if (a->server_scope_sz == b->server_scope_sz &&
5458 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5459 return true;
5460
5461 return false;
5462}
5463
Andy Adamson357f54d2010-12-14 10:11:57 -05005464/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005465 * nfs4_proc_bind_conn_to_session()
5466 *
5467 * The 4.1 client currently uses the same TCP connection for the
5468 * fore and backchannel.
5469 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005470int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005471{
5472 int status;
5473 struct nfs41_bind_conn_to_session_res res;
5474 struct rpc_message msg = {
5475 .rpc_proc =
5476 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5477 .rpc_argp = clp,
5478 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005479 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005480 };
5481
5482 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005483
5484 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5485 if (unlikely(res.session == NULL)) {
5486 status = -ENOMEM;
5487 goto out;
5488 }
5489
5490 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5491 if (status == 0) {
5492 if (memcmp(res.session->sess_id.data,
5493 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5494 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5495 status = -EIO;
5496 goto out_session;
5497 }
5498 if (res.dir != NFS4_CDFS4_BOTH) {
5499 dprintk("NFS: %s: Unexpected direction from server\n",
5500 __func__);
5501 status = -EIO;
5502 goto out_session;
5503 }
5504 if (res.use_conn_in_rdma_mode) {
5505 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5506 __func__);
5507 status = -EIO;
5508 goto out_session;
5509 }
5510 }
5511out_session:
5512 kfree(res.session);
5513out:
5514 dprintk("<-- %s status= %d\n", __func__, status);
5515 return status;
5516}
5517
5518/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005519 * nfs4_proc_exchange_id()
5520 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005521 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5522 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005523 * Since the clientid has expired, all compounds using sessions
5524 * associated with the stale clientid will be returning
5525 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5526 * be in some phase of session reset.
5527 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005528int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005529{
5530 nfs4_verifier verifier;
5531 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005532 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005533 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005534 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5535 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005536 };
5537 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005538 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005539 };
5540 int status;
5541 struct rpc_message msg = {
5542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5543 .rpc_argp = &args,
5544 .rpc_resp = &res,
5545 .rpc_cred = cred,
5546 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005547
Chuck Leverf0920752012-05-21 22:45:41 -04005548 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005549 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5550 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5552 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5553 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005554
Chuck Leveracdeb692012-05-21 22:46:16 -04005555 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005556 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005557 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005558 status = -ENOMEM;
5559 goto out;
5560 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005561
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005562 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005563 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005564 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005565 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005566 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005567 }
5568
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005569 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005570 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005571 status = -ENOMEM;
5572 goto out_server_scope;
5573 }
5574
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005575 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005576 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005577 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005578
Chuck Lever177313f2012-05-21 22:44:58 -04005579 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005580 clp->cl_clientid = res.clientid;
5581 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5582 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5583 clp->cl_seqid = res.seqid;
5584
Chuck Leveracdeb692012-05-21 22:46:16 -04005585 kfree(clp->cl_serverowner);
5586 clp->cl_serverowner = res.server_owner;
5587 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005588
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005589 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005590 kfree(clp->cl_implid);
5591 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005592
Chuck Lever177313f2012-05-21 22:44:58 -04005593 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005594 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005595 res.server_scope)) {
5596 dprintk("%s: server_scope mismatch detected\n",
5597 __func__);
5598 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005599 kfree(clp->cl_serverscope);
5600 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005601 }
5602
Chuck Lever177313f2012-05-21 22:44:58 -04005603 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005604 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005605 goto out;
5606 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005607 } else
5608 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005609
Chuck Leveracdeb692012-05-21 22:46:16 -04005610out_server_owner:
5611 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005612out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005613 kfree(res.server_scope);
5614out:
Chuck Lever177313f2012-05-21 22:44:58 -04005615 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005616 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005617 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005618 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005619 clp->cl_implid->date.seconds,
5620 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005621 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005622 return status;
5623}
5624
Trond Myklebust66245532012-05-25 17:18:09 -04005625static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5626 struct rpc_cred *cred)
5627{
5628 struct rpc_message msg = {
5629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5630 .rpc_argp = clp,
5631 .rpc_cred = cred,
5632 };
5633 int status;
5634
5635 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5636 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005637 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005638 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5639 return status;
5640}
5641
5642static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5643 struct rpc_cred *cred)
5644{
5645 unsigned int loop;
5646 int ret;
5647
5648 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5649 ret = _nfs4_proc_destroy_clientid(clp, cred);
5650 switch (ret) {
5651 case -NFS4ERR_DELAY:
5652 case -NFS4ERR_CLIENTID_BUSY:
5653 ssleep(1);
5654 break;
5655 default:
5656 return ret;
5657 }
5658 }
5659 return 0;
5660}
5661
5662int nfs4_destroy_clientid(struct nfs_client *clp)
5663{
5664 struct rpc_cred *cred;
5665 int ret = 0;
5666
5667 if (clp->cl_mvops->minor_version < 1)
5668 goto out;
5669 if (clp->cl_exchange_flags == 0)
5670 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005671 if (clp->cl_preserve_clid)
5672 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005673 cred = nfs4_get_exchange_id_cred(clp);
5674 ret = nfs4_proc_destroy_clientid(clp, cred);
5675 if (cred)
5676 put_rpccred(cred);
5677 switch (ret) {
5678 case 0:
5679 case -NFS4ERR_STALE_CLIENTID:
5680 clp->cl_exchange_flags = 0;
5681 }
5682out:
5683 return ret;
5684}
5685
Andy Adamson2050f0c2009-04-01 09:22:30 -04005686struct nfs4_get_lease_time_data {
5687 struct nfs4_get_lease_time_args *args;
5688 struct nfs4_get_lease_time_res *res;
5689 struct nfs_client *clp;
5690};
5691
5692static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5693 void *calldata)
5694{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005695 struct nfs4_get_lease_time_data *data =
5696 (struct nfs4_get_lease_time_data *)calldata;
5697
5698 dprintk("--> %s\n", __func__);
5699 /* just setup sequence, do not trigger session recovery
5700 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005701 nfs41_setup_sequence(data->clp->cl_session,
5702 &data->args->la_seq_args,
5703 &data->res->lr_seq_res,
5704 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005705 dprintk("<-- %s\n", __func__);
5706}
5707
5708/*
5709 * Called from nfs4_state_manager thread for session setup, so don't recover
5710 * from sequence operation or clientid errors.
5711 */
5712static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5713{
5714 struct nfs4_get_lease_time_data *data =
5715 (struct nfs4_get_lease_time_data *)calldata;
5716
5717 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005718 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5719 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005720 switch (task->tk_status) {
5721 case -NFS4ERR_DELAY:
5722 case -NFS4ERR_GRACE:
5723 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5724 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5725 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005726 /* fall through */
5727 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005728 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005729 return;
5730 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005731 dprintk("<-- %s\n", __func__);
5732}
5733
Trond Myklebust17280172012-03-11 13:11:00 -04005734static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005735 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5736 .rpc_call_done = nfs4_get_lease_time_done,
5737};
5738
5739int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5740{
5741 struct rpc_task *task;
5742 struct nfs4_get_lease_time_args args;
5743 struct nfs4_get_lease_time_res res = {
5744 .lr_fsinfo = fsinfo,
5745 };
5746 struct nfs4_get_lease_time_data data = {
5747 .args = &args,
5748 .res = &res,
5749 .clp = clp,
5750 };
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5753 .rpc_argp = &args,
5754 .rpc_resp = &res,
5755 };
5756 struct rpc_task_setup task_setup = {
5757 .rpc_client = clp->cl_rpcclient,
5758 .rpc_message = &msg,
5759 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005760 .callback_data = &data,
5761 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005762 };
5763 int status;
5764
Trond Myklebust9d12b212012-01-17 22:04:25 -05005765 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005766 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005767 dprintk("--> %s\n", __func__);
5768 task = rpc_run_task(&task_setup);
5769
5770 if (IS_ERR(task))
5771 status = PTR_ERR(task);
5772 else {
5773 status = task->tk_status;
5774 rpc_put_task(task);
5775 }
5776 dprintk("<-- %s return %d\n", __func__, status);
5777
5778 return status;
5779}
5780
Andy Adamsonfc931582009-04-01 09:22:31 -04005781/*
5782 * Initialize the values to be used by the client in CREATE_SESSION
5783 * If nfs4_init_session set the fore channel request and response sizes,
5784 * use them.
5785 *
5786 * Set the back channel max_resp_sz_cached to zero to force the client to
5787 * always set csa_cachethis to FALSE because the current implementation
5788 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5789 */
5790static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5791{
5792 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005793 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5794 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005795
5796 if (mxrqst_sz == 0)
5797 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5798 if (mxresp_sz == 0)
5799 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5800 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005801 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5802 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005803 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005804 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005805
5806 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005807 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005808 __func__,
5809 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005810 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005811
5812 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005813 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5814 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5815 args->bc_attrs.max_resp_sz_cached = 0;
5816 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5817 args->bc_attrs.max_reqs = 1;
5818
5819 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5820 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5821 __func__,
5822 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5823 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5824 args->bc_attrs.max_reqs);
5825}
5826
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005827static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005828{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005829 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5830 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5831
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005832 if (rcvd->max_resp_sz > sent->max_resp_sz)
5833 return -EINVAL;
5834 /*
5835 * Our requested max_ops is the minimum we need; we're not
5836 * prepared to break up compounds into smaller pieces than that.
5837 * So, no point even trying to continue if the server won't
5838 * cooperate:
5839 */
5840 if (rcvd->max_ops < sent->max_ops)
5841 return -EINVAL;
5842 if (rcvd->max_reqs == 0)
5843 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005844 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5845 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005846 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005847}
5848
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005849static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5850{
5851 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5852 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005853
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005854 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5855 return -EINVAL;
5856 if (rcvd->max_resp_sz < sent->max_resp_sz)
5857 return -EINVAL;
5858 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5859 return -EINVAL;
5860 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005861 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005862 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005863 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005864 return -EINVAL;
5865 return 0;
5866}
Andy Adamson8d353012009-04-01 09:22:32 -04005867
Andy Adamson8d353012009-04-01 09:22:32 -04005868static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5869 struct nfs4_session *session)
5870{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005871 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005872
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005873 ret = nfs4_verify_fore_channel_attrs(args, session);
5874 if (ret)
5875 return ret;
5876 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005877}
5878
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005879static int _nfs4_proc_create_session(struct nfs_client *clp,
5880 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005881{
5882 struct nfs4_session *session = clp->cl_session;
5883 struct nfs41_create_session_args args = {
5884 .client = clp,
5885 .cb_program = NFS4_CALLBACK,
5886 };
5887 struct nfs41_create_session_res res = {
5888 .client = clp,
5889 };
5890 struct rpc_message msg = {
5891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5892 .rpc_argp = &args,
5893 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005894 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005895 };
5896 int status;
5897
5898 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005899 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005900
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005901 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005902
Trond Myklebust43095d32012-11-20 11:13:12 -05005903 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005904 /* Verify the session's negotiated channel_attrs values */
5905 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005906 /* Increment the clientid slot sequence id */
5907 clp->cl_seqid++;
5908 }
5909
5910 return status;
5911}
5912
5913/*
5914 * Issues a CREATE_SESSION operation to the server.
5915 * It is the responsibility of the caller to verify the session is
5916 * expired before calling this routine.
5917 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005918int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005919{
5920 int status;
5921 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005922 struct nfs4_session *session = clp->cl_session;
5923
5924 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5925
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005926 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005927 if (status)
5928 goto out;
5929
Andy Adamsonaacd5532011-11-09 13:58:21 -05005930 /* Init or reset the session slot tables */
5931 status = nfs4_setup_session_slot_tables(session);
5932 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005933 if (status)
5934 goto out;
5935
5936 ptr = (unsigned *)&session->sess_id.data[0];
5937 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5938 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005939out:
5940 dprintk("<-- %s\n", __func__);
5941 return status;
5942}
5943
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005944/*
5945 * Issue the over-the-wire RPC DESTROY_SESSION.
5946 * The caller must serialize access to this routine.
5947 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005948int nfs4_proc_destroy_session(struct nfs4_session *session,
5949 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005950{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005951 struct rpc_message msg = {
5952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5953 .rpc_argp = session,
5954 .rpc_cred = cred,
5955 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005956 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005957
5958 dprintk("--> nfs4_proc_destroy_session\n");
5959
5960 /* session is still being setup */
5961 if (session->clp->cl_cons_state != NFS_CS_READY)
5962 return status;
5963
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005964 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005965
5966 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005967 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005968 "Session has been destroyed regardless...\n", status);
5969
5970 dprintk("<-- nfs4_proc_destroy_session\n");
5971 return status;
5972}
5973
Trond Myklebust7b38c362012-05-23 13:23:31 -04005974/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005975 * Renew the cl_session lease.
5976 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005977struct nfs4_sequence_data {
5978 struct nfs_client *clp;
5979 struct nfs4_sequence_args args;
5980 struct nfs4_sequence_res res;
5981};
5982
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005983static void nfs41_sequence_release(void *data)
5984{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005985 struct nfs4_sequence_data *calldata = data;
5986 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005987
Alexandros Batsakis71358402010-02-05 03:45:05 -08005988 if (atomic_read(&clp->cl_count) > 1)
5989 nfs4_schedule_state_renewal(clp);
5990 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005991 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005992}
5993
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005994static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5995{
5996 switch(task->tk_status) {
5997 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005998 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5999 return -EAGAIN;
6000 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006001 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006002 }
6003 return 0;
6004}
6005
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006006static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006007{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006008 struct nfs4_sequence_data *calldata = data;
6009 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006010
Trond Myklebust14516c32010-07-31 14:29:06 -04006011 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6012 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006013
6014 if (task->tk_status < 0) {
6015 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006016 if (atomic_read(&clp->cl_count) == 1)
6017 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006018
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006019 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6020 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006021 return;
6022 }
6023 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006025out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006026 dprintk("<-- %s\n", __func__);
6027}
6028
6029static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6030{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006031 struct nfs4_sequence_data *calldata = data;
6032 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006033 struct nfs4_sequence_args *args;
6034 struct nfs4_sequence_res *res;
6035
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006036 args = task->tk_msg.rpc_argp;
6037 res = task->tk_msg.rpc_resp;
6038
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006039 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006040}
6041
6042static const struct rpc_call_ops nfs41_sequence_ops = {
6043 .rpc_call_done = nfs41_sequence_call_done,
6044 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006045 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006046};
6047
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006048static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6049 struct rpc_cred *cred,
6050 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006051{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006052 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006053 struct rpc_message msg = {
6054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6055 .rpc_cred = cred,
6056 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006057 struct rpc_task_setup task_setup_data = {
6058 .rpc_client = clp->cl_rpcclient,
6059 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006060 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006061 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006062 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006063
Alexandros Batsakis71358402010-02-05 03:45:05 -08006064 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006065 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006066 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006067 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006068 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006069 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006070 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006071 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006072 if (is_privileged)
6073 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006074 msg.rpc_argp = &calldata->args;
6075 msg.rpc_resp = &calldata->res;
6076 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006077 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006078
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006079 return rpc_run_task(&task_setup_data);
6080}
6081
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006082static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083{
6084 struct rpc_task *task;
6085 int ret = 0;
6086
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006087 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6088 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006089 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006090 if (IS_ERR(task))
6091 ret = PTR_ERR(task);
6092 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006093 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006094 dprintk("<-- %s status=%d\n", __func__, ret);
6095 return ret;
6096}
6097
6098static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6099{
6100 struct rpc_task *task;
6101 int ret;
6102
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006103 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006104 if (IS_ERR(task)) {
6105 ret = PTR_ERR(task);
6106 goto out;
6107 }
6108 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006109 if (!ret) {
6110 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6111
6112 if (task->tk_status == 0)
6113 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006114 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006115 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006116 rpc_put_task(task);
6117out:
6118 dprintk("<-- %s status=%d\n", __func__, ret);
6119 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006120}
6121
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006122struct nfs4_reclaim_complete_data {
6123 struct nfs_client *clp;
6124 struct nfs41_reclaim_complete_args arg;
6125 struct nfs41_reclaim_complete_res res;
6126};
6127
6128static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6129{
6130 struct nfs4_reclaim_complete_data *calldata = data;
6131
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006132 nfs41_setup_sequence(calldata->clp->cl_session,
6133 &calldata->arg.seq_args,
6134 &calldata->res.seq_res,
6135 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006136}
6137
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006138static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6139{
6140 switch(task->tk_status) {
6141 case 0:
6142 case -NFS4ERR_COMPLETE_ALREADY:
6143 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6144 break;
6145 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006146 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006147 /* fall through */
6148 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006149 return -EAGAIN;
6150 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006151 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006152 }
6153 return 0;
6154}
6155
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006156static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6157{
6158 struct nfs4_reclaim_complete_data *calldata = data;
6159 struct nfs_client *clp = calldata->clp;
6160 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6161
6162 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006163 if (!nfs41_sequence_done(task, res))
6164 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006166 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6167 rpc_restart_call_prepare(task);
6168 return;
6169 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006170 dprintk("<-- %s\n", __func__);
6171}
6172
6173static void nfs4_free_reclaim_complete_data(void *data)
6174{
6175 struct nfs4_reclaim_complete_data *calldata = data;
6176
6177 kfree(calldata);
6178}
6179
6180static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6181 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6182 .rpc_call_done = nfs4_reclaim_complete_done,
6183 .rpc_release = nfs4_free_reclaim_complete_data,
6184};
6185
6186/*
6187 * Issue a global reclaim complete.
6188 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006189static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6190 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006191{
6192 struct nfs4_reclaim_complete_data *calldata;
6193 struct rpc_task *task;
6194 struct rpc_message msg = {
6195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006196 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006197 };
6198 struct rpc_task_setup task_setup_data = {
6199 .rpc_client = clp->cl_rpcclient,
6200 .rpc_message = &msg,
6201 .callback_ops = &nfs4_reclaim_complete_call_ops,
6202 .flags = RPC_TASK_ASYNC,
6203 };
6204 int status = -ENOMEM;
6205
6206 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006207 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006208 if (calldata == NULL)
6209 goto out;
6210 calldata->clp = clp;
6211 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006212
Trond Myklebust9d12b212012-01-17 22:04:25 -05006213 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006214 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006215 msg.rpc_argp = &calldata->arg;
6216 msg.rpc_resp = &calldata->res;
6217 task_setup_data.callback_data = calldata;
6218 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006219 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006220 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006221 goto out;
6222 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006223 status = nfs4_wait_for_completion_rpc_task(task);
6224 if (status == 0)
6225 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006226 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006227 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006228out:
6229 dprintk("<-- %s status=%d\n", __func__, status);
6230 return status;
6231}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006232
6233static void
6234nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6235{
6236 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006237 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006238 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006239
6240 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006241 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6242 * right now covering the LAYOUTGET we are about to send.
6243 * However, that is not so catastrophic, and there seems
6244 * to be no way to prevent it completely.
6245 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006246 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006247 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006248 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006249 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6250 NFS_I(lgp->args.inode)->layout,
6251 lgp->args.ctx->state)) {
6252 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006253 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006254}
6255
6256static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6257{
6258 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006259 struct inode *inode = lgp->args.inode;
6260 struct nfs_server *server = NFS_SERVER(inode);
6261 struct pnfs_layout_hdr *lo;
6262 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006263 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006264
6265 dprintk("--> %s\n", __func__);
6266
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006267 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006268 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006269
6270 switch (task->tk_status) {
6271 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006272 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006273 case -NFS4ERR_LAYOUTTRYLATER:
6274 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006275 timeo = rpc_get_timeout(task->tk_client);
6276 giveup = lgp->args.timestamp + timeo;
6277 if (time_after(giveup, jiffies))
6278 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006279 break;
6280 case -NFS4ERR_EXPIRED:
6281 case -NFS4ERR_BAD_STATEID:
6282 spin_lock(&inode->i_lock);
6283 lo = NFS_I(inode)->layout;
6284 if (!lo || list_empty(&lo->plh_segs)) {
6285 spin_unlock(&inode->i_lock);
6286 /* If the open stateid was bad, then recover it. */
6287 state = lgp->args.ctx->state;
6288 } else {
6289 LIST_HEAD(head);
6290
6291 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6292 spin_unlock(&inode->i_lock);
6293 /* Mark the bad layout state as invalid, then
6294 * retry using the open stateid. */
6295 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006296 }
6297 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006298 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6299 rpc_restart_call_prepare(task);
6300out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006301 dprintk("<-- %s\n", __func__);
6302}
6303
Idan Kedar85541162012-08-02 11:47:10 +03006304static size_t max_response_pages(struct nfs_server *server)
6305{
6306 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6307 return nfs_page_array_len(0, max_resp_sz);
6308}
6309
6310static void nfs4_free_pages(struct page **pages, size_t size)
6311{
6312 int i;
6313
6314 if (!pages)
6315 return;
6316
6317 for (i = 0; i < size; i++) {
6318 if (!pages[i])
6319 break;
6320 __free_page(pages[i]);
6321 }
6322 kfree(pages);
6323}
6324
6325static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6326{
6327 struct page **pages;
6328 int i;
6329
6330 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6331 if (!pages) {
6332 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6333 return NULL;
6334 }
6335
6336 for (i = 0; i < size; i++) {
6337 pages[i] = alloc_page(gfp_flags);
6338 if (!pages[i]) {
6339 dprintk("%s: failed to allocate page\n", __func__);
6340 nfs4_free_pages(pages, size);
6341 return NULL;
6342 }
6343 }
6344
6345 return pages;
6346}
6347
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006348static void nfs4_layoutget_release(void *calldata)
6349{
6350 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006351 struct inode *inode = lgp->args.inode;
6352 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006353 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006354
6355 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006356 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006357 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006358 put_nfs_open_context(lgp->args.ctx);
6359 kfree(calldata);
6360 dprintk("<-- %s\n", __func__);
6361}
6362
6363static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6364 .rpc_call_prepare = nfs4_layoutget_prepare,
6365 .rpc_call_done = nfs4_layoutget_done,
6366 .rpc_release = nfs4_layoutget_release,
6367};
6368
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006369struct pnfs_layout_segment *
6370nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006371{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006372 struct inode *inode = lgp->args.inode;
6373 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006374 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006375 struct rpc_task *task;
6376 struct rpc_message msg = {
6377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6378 .rpc_argp = &lgp->args,
6379 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006380 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006381 };
6382 struct rpc_task_setup task_setup_data = {
6383 .rpc_client = server->client,
6384 .rpc_message = &msg,
6385 .callback_ops = &nfs4_layoutget_call_ops,
6386 .callback_data = lgp,
6387 .flags = RPC_TASK_ASYNC,
6388 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006389 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006390 int status = 0;
6391
6392 dprintk("--> %s\n", __func__);
6393
Idan Kedar85541162012-08-02 11:47:10 +03006394 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6395 if (!lgp->args.layout.pages) {
6396 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006397 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006398 }
6399 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006400 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006401
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006402 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006403 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006404 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006405
6406 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6407 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6408
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006409 task = rpc_run_task(&task_setup_data);
6410 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006411 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006412 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006413 if (status == 0)
6414 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006415 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6416 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006417 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006418 rpc_put_task(task);
6419 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006420 if (status)
6421 return ERR_PTR(status);
6422 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006423}
6424
Benny Halevycbe82602011-05-22 19:52:37 +03006425static void
6426nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6427{
6428 struct nfs4_layoutreturn *lrp = calldata;
6429
6430 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006431 nfs41_setup_sequence(lrp->clp->cl_session,
6432 &lrp->args.seq_args,
6433 &lrp->res.seq_res,
6434 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006435}
6436
6437static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6438{
6439 struct nfs4_layoutreturn *lrp = calldata;
6440 struct nfs_server *server;
6441
6442 dprintk("--> %s\n", __func__);
6443
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006444 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006445 return;
6446
6447 server = NFS_SERVER(lrp->args.inode);
6448 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006449 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006450 return;
6451 }
Benny Halevycbe82602011-05-22 19:52:37 +03006452 dprintk("<-- %s\n", __func__);
6453}
6454
6455static void nfs4_layoutreturn_release(void *calldata)
6456{
6457 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006458 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006459
6460 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006461 spin_lock(&lo->plh_inode->i_lock);
6462 if (lrp->res.lrs_present)
6463 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6464 lo->plh_block_lgets--;
6465 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006466 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006467 kfree(calldata);
6468 dprintk("<-- %s\n", __func__);
6469}
6470
6471static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6472 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6473 .rpc_call_done = nfs4_layoutreturn_done,
6474 .rpc_release = nfs4_layoutreturn_release,
6475};
6476
6477int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6478{
6479 struct rpc_task *task;
6480 struct rpc_message msg = {
6481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6482 .rpc_argp = &lrp->args,
6483 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006484 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006485 };
6486 struct rpc_task_setup task_setup_data = {
6487 .rpc_client = lrp->clp->cl_rpcclient,
6488 .rpc_message = &msg,
6489 .callback_ops = &nfs4_layoutreturn_call_ops,
6490 .callback_data = lrp,
6491 };
6492 int status;
6493
6494 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006495 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006496 task = rpc_run_task(&task_setup_data);
6497 if (IS_ERR(task))
6498 return PTR_ERR(task);
6499 status = task->tk_status;
6500 dprintk("<-- %s status=%d\n", __func__, status);
6501 rpc_put_task(task);
6502 return status;
6503}
6504
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006505/*
6506 * Retrieve the list of Data Server devices from the MDS.
6507 */
6508static int _nfs4_getdevicelist(struct nfs_server *server,
6509 const struct nfs_fh *fh,
6510 struct pnfs_devicelist *devlist)
6511{
6512 struct nfs4_getdevicelist_args args = {
6513 .fh = fh,
6514 .layoutclass = server->pnfs_curr_ld->id,
6515 };
6516 struct nfs4_getdevicelist_res res = {
6517 .devlist = devlist,
6518 };
6519 struct rpc_message msg = {
6520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6521 .rpc_argp = &args,
6522 .rpc_resp = &res,
6523 };
6524 int status;
6525
6526 dprintk("--> %s\n", __func__);
6527 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6528 &res.seq_res, 0);
6529 dprintk("<-- %s status=%d\n", __func__, status);
6530 return status;
6531}
6532
6533int nfs4_proc_getdevicelist(struct nfs_server *server,
6534 const struct nfs_fh *fh,
6535 struct pnfs_devicelist *devlist)
6536{
6537 struct nfs4_exception exception = { };
6538 int err;
6539
6540 do {
6541 err = nfs4_handle_exception(server,
6542 _nfs4_getdevicelist(server, fh, devlist),
6543 &exception);
6544 } while (exception.retry);
6545
6546 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6547 err, devlist->num_devs);
6548
6549 return err;
6550}
6551EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6552
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006553static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006554_nfs4_proc_getdeviceinfo(struct nfs_server *server,
6555 struct pnfs_device *pdev,
6556 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006557{
6558 struct nfs4_getdeviceinfo_args args = {
6559 .pdev = pdev,
6560 };
6561 struct nfs4_getdeviceinfo_res res = {
6562 .pdev = pdev,
6563 };
6564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6566 .rpc_argp = &args,
6567 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006568 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006569 };
6570 int status;
6571
6572 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006573 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006574 dprintk("<-- %s status=%d\n", __func__, status);
6575
6576 return status;
6577}
6578
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006579int nfs4_proc_getdeviceinfo(struct nfs_server *server,
6580 struct pnfs_device *pdev,
6581 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006582{
6583 struct nfs4_exception exception = { };
6584 int err;
6585
6586 do {
6587 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006588 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006589 &exception);
6590 } while (exception.retry);
6591 return err;
6592}
6593EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6594
Andy Adamson863a3c62011-03-23 13:27:54 +00006595static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6596{
6597 struct nfs4_layoutcommit_data *data = calldata;
6598 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006599 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006600
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006601 nfs41_setup_sequence(session,
6602 &data->args.seq_args,
6603 &data->res.seq_res,
6604 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006605}
6606
6607static void
6608nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6609{
6610 struct nfs4_layoutcommit_data *data = calldata;
6611 struct nfs_server *server = NFS_SERVER(data->args.inode);
6612
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006613 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006614 return;
6615
6616 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006617 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6618 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6619 case -NFS4ERR_BADLAYOUT: /* no layout */
6620 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006621 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006622 break;
6623 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006624 nfs_post_op_update_inode_force_wcc(data->args.inode,
6625 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006626 break;
6627 default:
6628 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6629 rpc_restart_call_prepare(task);
6630 return;
6631 }
6632 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006633}
6634
6635static void nfs4_layoutcommit_release(void *calldata)
6636{
6637 struct nfs4_layoutcommit_data *data = calldata;
6638
Andy Adamsondb29c082011-07-30 20:52:38 -04006639 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006640 put_rpccred(data->cred);
6641 kfree(data);
6642}
6643
6644static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6645 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6646 .rpc_call_done = nfs4_layoutcommit_done,
6647 .rpc_release = nfs4_layoutcommit_release,
6648};
6649
6650int
Andy Adamsonef311532011-03-12 02:58:10 -05006651nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006652{
6653 struct rpc_message msg = {
6654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6655 .rpc_argp = &data->args,
6656 .rpc_resp = &data->res,
6657 .rpc_cred = data->cred,
6658 };
6659 struct rpc_task_setup task_setup_data = {
6660 .task = &data->task,
6661 .rpc_client = NFS_CLIENT(data->args.inode),
6662 .rpc_message = &msg,
6663 .callback_ops = &nfs4_layoutcommit_ops,
6664 .callback_data = data,
6665 .flags = RPC_TASK_ASYNC,
6666 };
6667 struct rpc_task *task;
6668 int status = 0;
6669
6670 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6671 "lbw: %llu inode %lu\n",
6672 data->task.tk_pid, sync,
6673 data->args.lastbytewritten,
6674 data->args.inode->i_ino);
6675
Trond Myklebust9d12b212012-01-17 22:04:25 -05006676 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006677 task = rpc_run_task(&task_setup_data);
6678 if (IS_ERR(task))
6679 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006680 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006681 goto out;
6682 status = nfs4_wait_for_completion_rpc_task(task);
6683 if (status != 0)
6684 goto out;
6685 status = task->tk_status;
6686out:
6687 dprintk("%s: status %d\n", __func__, status);
6688 rpc_put_task(task);
6689 return status;
6690}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006691
6692static int
6693_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6694 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6695{
6696 struct nfs41_secinfo_no_name_args args = {
6697 .style = SECINFO_STYLE_CURRENT_FH,
6698 };
6699 struct nfs4_secinfo_res res = {
6700 .flavors = flavors,
6701 };
6702 struct rpc_message msg = {
6703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6704 .rpc_argp = &args,
6705 .rpc_resp = &res,
6706 };
6707 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6708}
6709
6710static int
6711nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6712 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6713{
6714 struct nfs4_exception exception = { };
6715 int err;
6716 do {
6717 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6718 switch (err) {
6719 case 0:
6720 case -NFS4ERR_WRONGSEC:
6721 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006722 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006723 default:
6724 err = nfs4_handle_exception(server, err, &exception);
6725 }
6726 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006727out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006728 return err;
6729}
6730
6731static int
6732nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6733 struct nfs_fsinfo *info)
6734{
6735 int err;
6736 struct page *page;
6737 rpc_authflavor_t flavor;
6738 struct nfs4_secinfo_flavors *flavors;
6739
6740 page = alloc_page(GFP_KERNEL);
6741 if (!page) {
6742 err = -ENOMEM;
6743 goto out;
6744 }
6745
6746 flavors = page_address(page);
6747 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6748
6749 /*
6750 * Fall back on "guess and check" method if
6751 * the server doesn't support SECINFO_NO_NAME
6752 */
6753 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6754 err = nfs4_find_root_sec(server, fhandle, info);
6755 goto out_freepage;
6756 }
6757 if (err)
6758 goto out_freepage;
6759
6760 flavor = nfs_find_best_sec(flavors);
6761 if (err == 0)
6762 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6763
6764out_freepage:
6765 put_page(page);
6766 if (err == -EACCES)
6767 return -EPERM;
6768out:
6769 return err;
6770}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006771
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006772static int _nfs41_test_stateid(struct nfs_server *server,
6773 nfs4_stateid *stateid,
6774 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006775{
6776 int status;
6777 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006778 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006779 };
6780 struct nfs41_test_stateid_res res;
6781 struct rpc_message msg = {
6782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6783 .rpc_argp = &args,
6784 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006785 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006786 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006787
Chuck Lever38527b12012-07-11 16:30:23 -04006788 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006789 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006790 nfs4_set_sequence_privileged(&args.seq_args);
6791 status = nfs4_call_sync_sequence(server->client, server, &msg,
6792 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006793 if (status != NFS_OK) {
6794 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006795 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006796 }
6797 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006798 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006799}
6800
Chuck Lever38527b12012-07-11 16:30:23 -04006801/**
6802 * nfs41_test_stateid - perform a TEST_STATEID operation
6803 *
6804 * @server: server / transport on which to perform the operation
6805 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006806 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04006807 *
6808 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6809 * Otherwise a negative NFS4ERR value is returned if the operation
6810 * failed or the state ID is not currently valid.
6811 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006812static int nfs41_test_stateid(struct nfs_server *server,
6813 nfs4_stateid *stateid,
6814 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006815{
6816 struct nfs4_exception exception = { };
6817 int err;
6818 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006819 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04006820 if (err != -NFS4ERR_DELAY)
6821 break;
6822 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006823 } while (exception.retry);
6824 return err;
6825}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006826
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006827struct nfs_free_stateid_data {
6828 struct nfs_server *server;
6829 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006830 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006831};
6832
6833static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6834{
6835 struct nfs_free_stateid_data *data = calldata;
6836 nfs41_setup_sequence(nfs4_get_session(data->server),
6837 &data->args.seq_args,
6838 &data->res.seq_res,
6839 task);
6840}
6841
6842static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6843{
6844 struct nfs_free_stateid_data *data = calldata;
6845
6846 nfs41_sequence_done(task, &data->res.seq_res);
6847
6848 switch (task->tk_status) {
6849 case -NFS4ERR_DELAY:
6850 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6851 rpc_restart_call_prepare(task);
6852 }
6853}
6854
6855static void nfs41_free_stateid_release(void *calldata)
6856{
6857 kfree(calldata);
6858}
6859
6860const struct rpc_call_ops nfs41_free_stateid_ops = {
6861 .rpc_call_prepare = nfs41_free_stateid_prepare,
6862 .rpc_call_done = nfs41_free_stateid_done,
6863 .rpc_release = nfs41_free_stateid_release,
6864};
6865
6866static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6867 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006868 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006869 bool privileged)
6870{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006871 struct rpc_message msg = {
6872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006873 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006874 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006875 struct rpc_task_setup task_setup = {
6876 .rpc_client = server->client,
6877 .rpc_message = &msg,
6878 .callback_ops = &nfs41_free_stateid_ops,
6879 .flags = RPC_TASK_ASYNC,
6880 };
6881 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006882
Chuck Lever38527b12012-07-11 16:30:23 -04006883 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006884 data = kmalloc(sizeof(*data), GFP_NOFS);
6885 if (!data)
6886 return ERR_PTR(-ENOMEM);
6887 data->server = server;
6888 nfs4_stateid_copy(&data->args.stateid, stateid);
6889
6890 task_setup.callback_data = data;
6891
6892 msg.rpc_argp = &data->args;
6893 msg.rpc_resp = &data->res;
6894 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6895 if (privileged)
6896 nfs4_set_sequence_privileged(&data->args.seq_args);
6897
6898 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006899}
6900
Chuck Lever38527b12012-07-11 16:30:23 -04006901/**
6902 * nfs41_free_stateid - perform a FREE_STATEID operation
6903 *
6904 * @server: server / transport on which to perform the operation
6905 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006906 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04006907 *
6908 * Returns NFS_OK if the server freed "stateid". Otherwise a
6909 * negative NFS4ERR value is returned.
6910 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006911static int nfs41_free_stateid(struct nfs_server *server,
6912 nfs4_stateid *stateid,
6913 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006914{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006915 struct rpc_task *task;
6916 int ret;
6917
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006918 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006919 if (IS_ERR(task))
6920 return PTR_ERR(task);
6921 ret = rpc_wait_for_completion_task(task);
6922 if (!ret)
6923 ret = task->tk_status;
6924 rpc_put_task(task);
6925 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006926}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006927
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006928static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6929{
6930 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006931 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006932
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006933 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006934 nfs4_free_lock_state(server, lsp);
6935 if (IS_ERR(task))
6936 return PTR_ERR(task);
6937 rpc_put_task(task);
6938 return 0;
6939}
6940
Trond Myklebust36281ca2012-03-04 18:13:56 -05006941static bool nfs41_match_stateid(const nfs4_stateid *s1,
6942 const nfs4_stateid *s2)
6943{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006944 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006945 return false;
6946
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006947 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006948 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006949 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006950 return true;
6951
6952 return false;
6953}
6954
Andy Adamson557134a2009-04-01 09:21:53 -04006955#endif /* CONFIG_NFS_V4_1 */
6956
Trond Myklebust36281ca2012-03-04 18:13:56 -05006957static bool nfs4_match_stateid(const nfs4_stateid *s1,
6958 const nfs4_stateid *s2)
6959{
Trond Myklebustf597c532012-03-04 18:13:56 -05006960 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006961}
6962
6963
Trond Myklebust17280172012-03-11 13:11:00 -04006964static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006965 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006966 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 .recover_open = nfs4_open_reclaim,
6968 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006969 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006970 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006971 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972};
6973
Andy Adamson591d71c2009-04-01 09:22:47 -04006974#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006975static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006976 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6977 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6978 .recover_open = nfs4_open_reclaim,
6979 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006980 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006981 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006982 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006983 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006984};
6985#endif /* CONFIG_NFS_V4_1 */
6986
Trond Myklebust17280172012-03-11 13:11:00 -04006987static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006988 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006989 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 .recover_open = nfs4_open_expired,
6991 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006992 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006993 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994};
6995
Andy Adamson591d71c2009-04-01 09:22:47 -04006996#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006997static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006998 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6999 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007000 .recover_open = nfs41_open_expired,
7001 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007002 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007003 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007004};
7005#endif /* CONFIG_NFS_V4_1 */
7006
Trond Myklebust17280172012-03-11 13:11:00 -04007007static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007008 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007009 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007010 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007011};
7012
7013#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007014static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007015 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007016 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007017 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007018};
7019#endif
7020
Trond Myklebust97dc1352010-06-16 09:52:26 -04007021static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7022 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007023 .init_caps = NFS_CAP_READDIRPLUS
7024 | NFS_CAP_ATOMIC_OPEN
7025 | NFS_CAP_CHANGE_ATTR
7026 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007027 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007028 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007029 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007030 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007031 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7032 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7033 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007034};
7035
7036#if defined(CONFIG_NFS_V4_1)
7037static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7038 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007039 .init_caps = NFS_CAP_READDIRPLUS
7040 | NFS_CAP_ATOMIC_OPEN
7041 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007042 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007043 | NFS_CAP_STATEID_NFSV41
7044 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007045 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007046 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007047 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007048 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007049 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7050 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7051 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007052};
7053#endif
7054
Trond Myklebust97dc1352010-06-16 09:52:26 -04007055const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7056 [0] = &nfs_v4_0_minor_ops,
7057#if defined(CONFIG_NFS_V4_1)
7058 [1] = &nfs_v4_1_minor_ops,
7059#endif
7060};
7061
Bryan Schumaker73a79702012-07-16 16:39:12 -04007062const struct inode_operations nfs4_dir_inode_operations = {
7063 .create = nfs_create,
7064 .lookup = nfs_lookup,
7065 .atomic_open = nfs_atomic_open,
7066 .link = nfs_link,
7067 .unlink = nfs_unlink,
7068 .symlink = nfs_symlink,
7069 .mkdir = nfs_mkdir,
7070 .rmdir = nfs_rmdir,
7071 .mknod = nfs_mknod,
7072 .rename = nfs_rename,
7073 .permission = nfs_permission,
7074 .getattr = nfs_getattr,
7075 .setattr = nfs_setattr,
7076 .getxattr = generic_getxattr,
7077 .setxattr = generic_setxattr,
7078 .listxattr = generic_listxattr,
7079 .removexattr = generic_removexattr,
7080};
7081
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007082static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007083 .permission = nfs_permission,
7084 .getattr = nfs_getattr,
7085 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007086 .getxattr = generic_getxattr,
7087 .setxattr = generic_setxattr,
7088 .listxattr = generic_listxattr,
7089 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007090};
7091
David Howells509de812006-08-22 20:06:11 -04007092const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 .version = 4, /* protocol version */
7094 .dentry_ops = &nfs4_dentry_operations,
7095 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007096 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007097 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007099 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007100 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 .getattr = nfs4_proc_getattr,
7102 .setattr = nfs4_proc_setattr,
7103 .lookup = nfs4_proc_lookup,
7104 .access = nfs4_proc_access,
7105 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 .create = nfs4_proc_create,
7107 .remove = nfs4_proc_remove,
7108 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007109 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 .unlink_done = nfs4_proc_unlink_done,
7111 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007112 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007113 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007114 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 .link = nfs4_proc_link,
7116 .symlink = nfs4_proc_symlink,
7117 .mkdir = nfs4_proc_mkdir,
7118 .rmdir = nfs4_proc_remove,
7119 .readdir = nfs4_proc_readdir,
7120 .mknod = nfs4_proc_mknod,
7121 .statfs = nfs4_proc_statfs,
7122 .fsinfo = nfs4_proc_fsinfo,
7123 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007124 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 .decode_dirent = nfs4_decode_dirent,
7126 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007127 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007128 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007129 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007131 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007132 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007133 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007135 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007136 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007138 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007139 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007140 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007141 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007142 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007143 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007144 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007145 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007146 .create_server = nfs4_create_server,
7147 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148};
7149
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007150static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7151 .prefix = XATTR_NAME_NFSV4_ACL,
7152 .list = nfs4_xattr_list_nfs4_acl,
7153 .get = nfs4_xattr_get_nfs4_acl,
7154 .set = nfs4_xattr_set_nfs4_acl,
7155};
7156
7157const struct xattr_handler *nfs4_xattr_handlers[] = {
7158 &nfs4_xattr_nfs4_acl_handler,
7159 NULL
7160};
7161
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162/*
7163 * Local variables:
7164 * c-basic-offset: 8
7165 * End:
7166 */