blob: 81343944e09671897d32f3c244782cc9551371c4 [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
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050096 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400108 case -NFS4ERR_ACCESS:
109 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700114 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400117 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * This is our standard bitmap for GETATTR requests.
122 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400123const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138};
139
Trond Myklebust1549210f2012-06-05 09:16:47 -0400140static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
156};
157
Andy Adamsone23008e2012-10-02 21:07:32 -0400158static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
162};
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
171};
172
Trond Myklebust4ce79712005-06-22 17:16:21 +0000173const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
176 0
177};
178
Fred Isamandae100c2011-07-30 20:52:37 -0400179const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700183 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Manoj Naik830b8e32006-06-09 09:34:25 -0400188const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
205};
206
Al Virobc4785c2006-10-19 23:28:51 -0700207static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct nfs4_readdir_arg *readdir)
209{
Al Viro0dbb4c62006-10-19 23:28:49 -0700210 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000213 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
216 }
217
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
222
223 /*
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
229 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800230 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800258 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Trond Myklebust65de8722008-12-23 15:21:44 -0500261static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
262{
263 int res = 0;
264
265 might_sleep();
266
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100271 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
276}
277
278/* This is the error handling routine for processes that are allowed
279 * to sleep.
280 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800281static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500282{
283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500284 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500285 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 int ret = errorcode;
287
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500292 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400294 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500295 exception->retry = 1;
296 return 0;
297 }
298 if (state == NULL)
299 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500304 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500307 if (state == NULL)
308 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400309 nfs_remove_bad_delegation(state->inode);
Trond Myklebust5d422302013-03-14 16:57:48 -0400310 ret = nfs4_schedule_stateid_recovery(server, state);
311 if (ret < 0)
312 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500313 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400314 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400315 if (state != NULL) {
316 ret = nfs4_schedule_stateid_recovery(server, state);
317 if (ret < 0)
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500321 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322 nfs4_schedule_lease_recovery(clp);
323 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500324#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400325 case -NFS4ERR_BADSESSION:
326 case -NFS4ERR_BADSLOT:
327 case -NFS4ERR_BAD_HIGH_SLOT:
328 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
329 case -NFS4ERR_DEADSESSION:
330 case -NFS4ERR_SEQ_FALSE_RETRY:
331 case -NFS4ERR_SEQ_MISORDERED:
332 dprintk("%s ERROR: %d Reset session\n", __func__,
333 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400334 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400335 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
347 ret = nfs4_delay(server->client, &exception->timeout);
348 if (ret != 0)
349 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400350 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_OLD_STATEID:
352 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800353 break;
354 case -NFS4ERR_BADOWNER:
355 /* The following works around a Linux server bug! */
356 case -NFS4ERR_BADNAME:
357 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
358 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
359 exception->retry = 1;
360 printk(KERN_WARNING "NFS: v4 server %s "
361 "does not accept raw "
362 "uid/gids. "
363 "Reenabling the idmapper.\n",
364 server->nfs_client->cl_hostname);
365 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500366 }
367 /* We failed to handle the error */
368 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500369wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500370 ret = nfs4_wait_clnt_recover(clp);
371 if (ret == 0)
372 exception->retry = 1;
373 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500374}
375
376
Trond Myklebust452e9352010-07-31 14:29:06 -0400377static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock(&clp->cl_lock);
380 if (time_before(clp->cl_last_renewal,timestamp))
381 clp->cl_last_renewal = timestamp;
382 spin_unlock(&clp->cl_lock);
383}
384
Trond Myklebust452e9352010-07-31 14:29:06 -0400385static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
386{
387 do_renew_lease(server->nfs_client, timestamp);
388}
389
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400390#if defined(CONFIG_NFS_V4_1)
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400393{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400395 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500396 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400397
Benny Halevydfb4f3092010-09-24 09:17:01 -0400398 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500401 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 return;
Andy Adamson13615872009-04-01 09:22:17 -0400403 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500404 tbl = res->sr_slot->table;
405 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500406
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408 /* Be nice to the server: try to ensure that the last transmitted
409 * value for highest_user_slotid <= target_highest_slotid
410 */
411 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
412 send_new_highest_used_slotid = true;
413
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500414 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
415 send_new_highest_used_slotid = false;
416 goto out_unlock;
417 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500418 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500419
420 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
421 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500422out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500423 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400424 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500425 if (send_new_highest_used_slotid)
426 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400427}
428
Trond Myklebust14516c32010-07-31 14:29:06 -0400429static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400433 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500434 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500435 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436
Bryan Schumaker468f8612011-04-18 15:57:32 -0400437 /* don't increment the sequence number if the task wasn't sent */
438 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439 goto out;
440
Trond Myklebust933602e2012-11-16 12:12:38 -0500441 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500442 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500443
Trond Myklebustac20d162012-12-15 15:36:07 -0500444 if (slot->interrupted) {
445 slot->interrupted = 0;
446 interrupted = true;
447 }
448
Andy Adamson691daf32009-12-04 15:55:39 -0500449 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 switch (res->sr_status) {
451 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500453 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500455 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500461 case 1:
462 /*
463 * sr_status remains 1 if an RPC level error occurred.
464 * The server may or may not have processed the sequence
465 * operation..
466 * Mark the slot as having hosted an interrupted RPC call.
467 */
468 slot->interrupted = 1;
469 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500475 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500477 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500478 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500480 case -NFS4ERR_BADSLOT:
481 /*
482 * The slot id we used was probably retired. Try again
483 * using a different slot id.
484 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 goto retry_nowait;
486 case -NFS4ERR_SEQ_MISORDERED:
487 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500488 * Was the last operation on this sequence interrupted?
489 * If so, retry after bumping the sequence number.
490 */
491 if (interrupted) {
492 ++slot->seq_nr;
493 goto retry_nowait;
494 }
495 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500496 * Could this slot have been previously retired?
497 * If so, then the server may be expecting seq_nr = 1!
498 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500499 if (slot->seq_nr != 1) {
500 slot->seq_nr = 1;
501 goto retry_nowait;
502 }
503 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500504 case -NFS4ERR_SEQ_FALSE_RETRY:
505 ++slot->seq_nr;
506 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400507 default:
508 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500509 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 }
511out:
512 /* The session may be reset by one of the error handlers. */
513 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400514 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500515 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500516retry_nowait:
517 if (rpc_restart_call_prepare(task)) {
518 task->tk_status = 0;
519 ret = 0;
520 }
521 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400522out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400523 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400524 goto out;
525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
526 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527}
528
Trond Myklebust14516c32010-07-31 14:29:06 -0400529static int nfs4_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400531{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500532 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 return 1;
534 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400535}
536
Trond Myklebust9d12b212012-01-17 22:04:25 -0500537static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
539{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400542 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500543 if (cache_reply)
544 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 res->sr_slot = NULL;
546}
547
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400548static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
549{
550 args->sa_privileged = 1;
551}
552
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000553int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556 struct rpc_task *task)
557{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 struct nfs4_slot *slot;
559 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560
561 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400563 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400564 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 tbl = &session->fc_slot_table;
567
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 task->tk_timeout = 0;
569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400571 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400572 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500573 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500574 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400575 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400576 }
577
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500578 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500579 if (IS_ERR(slot)) {
580 /* If out of memory, try again in 1/4 second */
581 if (slot == ERR_PTR(-ENOMEM))
582 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400584 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 }
586 spin_unlock(&tbl->slot_tbl_lock);
587
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500588 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
591 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500594 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400595 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 /*
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
599 */
600 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400601out_success:
602 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400604out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 /* Privileged tasks are queued with top priority */
606 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400607 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
608 NULL, RPC_PRIORITY_PRIVILEGED);
609 else
610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400611 spin_unlock(&tbl->slot_tbl_lock);
612 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000614EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615
Trond Myklebust035168a2010-06-16 09:52:26 -0400616int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Trond Myklebust035168a2010-06-16 09:52:26 -0400621 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 int ret = 0;
623
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400624 if (session == NULL) {
625 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400628
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500629 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400630 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500631 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebust9d12b212012-01-17 22:04:25 -0500633 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634out:
635 dprintk("<-- %s status=%d\n", __func__, ret);
636 return ret;
637}
638
639struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400640 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643};
644
645static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400648 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
651
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400652 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653}
654
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660}
661
Trond Myklebust17280172012-03-11 13:11:00 -0400662static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665};
666
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400671 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672{
673 int ret;
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400676 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .seq_args = args,
678 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 };
680 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000681 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
685 };
686
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 task = rpc_run_task(&task_setup);
688 if (IS_ERR(task))
689 ret = PTR_ERR(task);
690 else {
691 ret = task->tk_status;
692 rpc_put_task(task);
693 }
694 return ret;
695}
696
Trond Myklebustdf896452010-06-16 09:52:26 -0400697#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400698static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500699void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
701{
702}
703
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400704static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
705{
706}
707
708
Trond Myklebust14516c32010-07-31 14:29:06 -0400709static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400711{
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714#endif /* CONFIG_NFS_V4_1 */
715
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400716static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724}
725
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400734 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400736 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400738
Trond Myklebust38478b22006-05-25 01:40:57 -0400739static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700745 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400746 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700747 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000748 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400753 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400762 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100766 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400767 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100768 int rpc_status;
769 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770};
771
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400772static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
774{
775 if (err != -EINVAL)
776 return false;
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
778 return false;
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
781 return true;
782}
783
784static enum open_claim_type4
785nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
787{
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
789 return claim;
790 switch (claim) {
791 default:
792 return claim;
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
799 }
800}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801
802static void nfs4_init_opendata_res(struct nfs4_opendata *p)
803{
804 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400807 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400808 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400809 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811}
812
Al Viro82a2c1b2011-06-22 18:30:55 -0400813static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400815 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400816 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818{
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
823
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (p == NULL)
826 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p->o_arg.seqid == NULL)
829 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
841 * are cached */
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700844 }
David Howells7539bba2006-08-22 20:06:09 -0400845 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
858 break;
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
864 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500865 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500866 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400867
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500870
871 verf[0] = jiffies;
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400879 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 return p;
882err_free:
883 kfree(p);
884err:
885 dput(parent);
886 return NULL;
887}
888
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400889static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400893 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894
895 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 nfs4_put_state_owner(p->owner);
899 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400900 dput(p->dentry);
901 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500902 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kfree(p);
904}
905
906static void nfs4_opendata_put(struct nfs4_opendata *p)
907{
908 if (p != NULL)
909 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910}
911
Trond Myklebust06f814a2006-01-03 09:55:07 +0100912static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
913{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100914 int ret;
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917 return ret;
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921{
922 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923
Trond Myklebust536e43d2012-01-17 22:04:26 -0500924 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 goto out;
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400930 break;
931 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500945 if (delegation == NULL)
946 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
952 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500953 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 1;
955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100958{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
968 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
986 }
987}
988
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400990{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0700997{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 /*
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1001 */
1002 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1006 }
1007 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001012 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015static 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 -05001016{
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001022
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1027
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001032 goto no_delegation_unlock;
1033
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
Trond Myklebustb7391f42008-12-23 15:21:52 -05001039 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001041 ret = 1;
1042no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044no_delegation:
1045 rcu_read_unlock();
1046
1047 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050 }
1051
1052 return ret;
1053}
1054
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057{
1058 struct nfs_delegation *delegation;
1059
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 rcu_read_unlock();
1064 return;
1065 }
1066 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001067 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068}
1069
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1079
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 goto out_return_state;
1087 }
1088 spin_unlock(&state->owner->so_lock);
1089 }
Trond Myklebust34310432008-12-23 15:21:38 -05001090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001092 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001097 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001106 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
1109 return ERR_PTR(ret);
1110out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1113}
1114
Andy Adamsone23008e2012-10-02 21:07:32 -04001115static void
1116nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1117{
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1121
1122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1131 clp->cl_hostname);
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140}
1141
1142/*
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1145 */
1146static struct nfs4_state *
1147_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1148{
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1151 int ret;
1152
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1155 goto err;
1156 }
1157
1158 ret = -ESTALE;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1162 goto err;
1163
1164 ret = -ENOMEM;
1165 state = nfs4_get_open_state(inode, data->owner);
1166 if (state == NULL)
1167 goto err;
1168
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1170 if (ret)
1171 goto err;
1172
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1176 data->o_arg.fmode);
1177
1178 return state;
1179err:
1180 return ERR_PTR(ret);
1181
1182}
1183
1184static struct nfs4_state *
1185_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001186{
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001189 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001192 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 goto out;
1194 }
1195
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001201 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
1203 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 state = nfs4_get_open_state(inode, data->owner);
1205 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001209 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001213 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215err_put_inode:
1216 iput(inode);
1217err:
1218 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219}
1220
Andy Adamsone23008e2012-10-02 21:07:32 -04001221static struct nfs4_state *
1222nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1223{
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1227}
1228
Trond Myklebust864472e2006-01-03 09:55:15 +01001229static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1230{
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1233
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1237 continue;
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1240 return ctx;
1241 }
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1244}
1245
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001246static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001248{
1249 struct nfs4_opendata *opendata;
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1257 return opendata;
1258}
1259
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001261{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 int ret;
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001270 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 if (ret != 0)
1272 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001273 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001276 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 return 0;
1279}
1280
1281static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1282{
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 int ret;
1285
1286 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 smp_rmb();
1289 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 if (ret != 0)
1293 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 if (newstate != state)
1295 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 }
1297 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 if (ret != 0)
1301 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001302 if (newstate != state)
1303 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 }
1305 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 if (ret != 0)
1309 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 if (newstate != state)
1311 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001313 /*
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1316 */
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001319 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001322 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001323 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * OPEN_RECLAIM:
1329 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001331static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001333 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int status;
1337
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001345 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001349 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return status;
1351}
1352
Trond Myklebust539cd032007-06-05 11:46:42 -04001353static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1357 int err;
1358 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001359 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1361 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001362 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001363 break;
1364 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } while (exception.retry);
1366 return err;
1367}
1368
Trond Myklebust864472e2006-01-03 09:55:15 +01001369static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1370{
1371 struct nfs_open_context *ctx;
1372 int ret;
1373
1374 ctx = nfs4_state_find_open_context(state);
1375 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001376 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 put_nfs_open_context(ctx);
1379 return ret;
1380}
1381
Trond Myklebust13437e12007-07-06 15:10:43 -04001382static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001384 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001387 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebustccb46e22013-03-18 12:50:59 -04001388 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001389 if (IS_ERR(opendata))
1390 return PTR_ERR(opendata);
Trond Myklebustf597c532012-03-04 18:13:56 -05001391 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001392 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Trond Myklebust13437e12007-07-06 15:10:43 -04001397int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001400 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int err;
1402 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001403 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 switch (err) {
1405 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001406 case -ENOENT:
1407 case -ESTALE:
1408 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001409 case -NFS4ERR_BADSESSION:
1410 case -NFS4ERR_BADSLOT:
1411 case -NFS4ERR_BAD_HIGH_SLOT:
1412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1413 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001414 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001415 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001416 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case -NFS4ERR_STALE_CLIENTID:
1419 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001420 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case -NFS4ERR_EXPIRED:
1422 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001423 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001424 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001425 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001426 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001427 case -NFS4ERR_ADMIN_REVOKED:
1428 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001429 nfs_inode_find_state_and_recover(state->inode,
1430 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001431 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001432 case -ENOMEM:
1433 err = 0;
1434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001436 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 err = nfs4_handle_exception(server, err, &exception);
1438 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return err;
1441}
1442
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001443static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1444{
1445 struct nfs4_opendata *data = calldata;
1446
1447 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001448 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001449 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001451 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001452 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001453 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001454}
1455
1456static void nfs4_open_confirm_release(void *calldata)
1457{
1458 struct nfs4_opendata *data = calldata;
1459 struct nfs4_state *state = NULL;
1460
1461 /* If this request hasn't been cancelled, do nothing */
1462 if (data->cancelled == 0)
1463 goto out_free;
1464 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001466 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001468 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001469 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001471 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472}
1473
1474static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001475 .rpc_call_done = nfs4_open_confirm_done,
1476 .rpc_release = nfs4_open_confirm_release,
1477};
1478
1479/*
1480 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1481 */
1482static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1483{
1484 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1485 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001486 struct rpc_message msg = {
1487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1488 .rpc_argp = &data->c_arg,
1489 .rpc_resp = &data->c_res,
1490 .rpc_cred = data->owner->so_cred,
1491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001492 struct rpc_task_setup task_setup_data = {
1493 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001495 .callback_ops = &nfs4_open_confirm_ops,
1496 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001497 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001498 .flags = RPC_TASK_ASYNC,
1499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int status;
1501
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001502 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001503 data->rpc_done = 0;
1504 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001505 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001506 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001507 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001508 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001509 status = nfs4_wait_for_completion_rpc_task(task);
1510 if (status != 0) {
1511 data->cancelled = 1;
1512 smp_wmb();
1513 } else
1514 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001515 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return status;
1517}
1518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 struct nfs4_opendata *data = calldata;
1522 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001525 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001526 /*
1527 * Check if we still need to send an OPEN call, or if we can use
1528 * a delegation instead.
1529 */
1530 if (data->state != NULL) {
1531 struct nfs_delegation *delegation;
1532
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001533 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001534 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001535 rcu_read_lock();
1536 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001537 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1538 can_open_delegated(delegation, data->o_arg.fmode))
1539 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 rcu_read_unlock();
1541 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001542 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001543 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001544 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001546 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001547 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1548 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001549 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001550 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001551 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001552 &data->o_res.seq_res,
1553 task) != 0)
1554 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001555 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001556unlock_no_action:
1557 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001558out_no_action:
1559 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001560out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001561 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001562}
1563
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564static void nfs4_open_done(struct rpc_task *task, void *calldata)
1565{
1566 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001569
Trond Myklebust14516c32010-07-31 14:29:06 -04001570 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1571 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001572
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001574 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1575 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001576 case S_IFREG:
1577 break;
1578 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001580 break;
1581 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001583 break;
1584 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001586 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001587 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001588 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001589 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1590 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001591 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595static void nfs4_open_release(void *calldata)
1596{
1597 struct nfs4_opendata *data = calldata;
1598 struct nfs4_state *state = NULL;
1599
1600 /* If this request hasn't been cancelled, do nothing */
1601 if (data->cancelled == 0)
1602 goto out_free;
1603 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001604 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 goto out_free;
1606 /* In case we need an open_confirm, no cleanup! */
1607 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1608 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001610 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001611 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001613 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614}
1615
1616static const struct rpc_call_ops nfs4_open_ops = {
1617 .rpc_call_prepare = nfs4_open_prepare,
1618 .rpc_call_done = nfs4_open_done,
1619 .rpc_release = nfs4_open_release,
1620};
1621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001622static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623{
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1628 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001629 struct rpc_message msg = {
1630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1631 .rpc_argp = o_arg,
1632 .rpc_resp = o_res,
1633 .rpc_cred = data->owner->so_cred,
1634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001635 struct rpc_task_setup task_setup_data = {
1636 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001638 .callback_ops = &nfs4_open_ops,
1639 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001640 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001641 .flags = RPC_TASK_ASYNC,
1642 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643 int status;
1644
Trond Myklebust9d12b212012-01-17 22:04:25 -05001645 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001646 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001647 data->rpc_done = 0;
1648 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001649 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001650 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001651 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001652 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001653 if (IS_ERR(task))
1654 return PTR_ERR(task);
1655 status = nfs4_wait_for_completion_rpc_task(task);
1656 if (status != 0) {
1657 data->cancelled = 1;
1658 smp_wmb();
1659 } else
1660 status = data->rpc_status;
1661 rpc_put_task(task);
1662
1663 return status;
1664}
1665
1666static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1667{
1668 struct inode *dir = data->dir->d_inode;
1669 struct nfs_openres *o_res = &data->o_res;
1670 int status;
1671
1672 status = nfs4_run_open_task(data, 1);
1673 if (status != 0 || !data->rpc_done)
1674 return status;
1675
Trond Myklebust6926afd2012-01-07 13:22:46 -05001676 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1677
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1679 status = _nfs4_proc_open_confirm(data);
1680 if (status != 0)
1681 return status;
1682 }
1683
1684 return status;
1685}
1686
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001687static int nfs4_opendata_access(struct rpc_cred *cred,
1688 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001689 struct nfs4_state *state, fmode_t fmode,
1690 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001691{
1692 struct nfs_access_entry cache;
1693 u32 mask;
1694
1695 /* access call failed or for some reason the server doesn't
1696 * support any access modes -- defer access call until later */
1697 if (opendata->o_res.access_supported == 0)
1698 return 0;
1699
1700 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001701 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001702 * write mode bits, but POSIX allows the creating process to write.
1703 * use openflags to check for exec, because fmode won't
1704 * always have FMODE_EXEC set when file open for exec. */
1705 if (openflags & __FMODE_EXEC) {
1706 /* ONLY check for exec rights */
1707 mask = MAY_EXEC;
1708 } else if (fmode & FMODE_READ)
1709 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001710
1711 cache.cred = cred;
1712 cache.jiffies = jiffies;
1713 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1714 nfs_access_add_cache(state->inode, &cache);
1715
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001716 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001717 return 0;
1718
1719 /* even though OPEN succeeded, access is denied. Close the file */
1720 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001721 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001722}
1723
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001724/*
1725 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1726 */
1727static int _nfs4_proc_open(struct nfs4_opendata *data)
1728{
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 int status;
1734
1735 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001736 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001738 if (status != 0) {
1739 if (status == -NFS4ERR_BADNAME &&
1740 !(o_arg->open_flags & O_CREAT))
1741 return -ENOENT;
1742 return status;
1743 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744
Trond Myklebust6926afd2012-01-07 13:22:46 -05001745 nfs_fattr_map_and_free_names(server, &data->f_attr);
1746
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001747 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001748 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001749 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1750 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001752 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001757 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Andy Adamsond83217c2011-03-01 01:34:17 +00001761static int nfs4_recover_expired_lease(struct nfs_server *server)
1762{
1763 return nfs4_client_recover_expired_lease(server->nfs_client);
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
1767 * OPEN_EXPIRED:
1768 * reclaim state on the server after a network partition.
1769 * Assumes caller holds the appropriate lock
1770 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001771static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001773 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001777 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001781 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001782 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001783 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001787static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001788{
Trond Myklebust539cd032007-06-05 11:46:42 -04001789 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001790 struct nfs4_exception exception = { };
1791 int err;
1792
1793 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001794 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001795 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1796 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001797 switch (err) {
1798 default:
1799 goto out;
1800 case -NFS4ERR_GRACE:
1801 case -NFS4ERR_DELAY:
1802 nfs4_handle_exception(server, err, &exception);
1803 err = 0;
1804 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001805 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001806out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001807 return err;
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001813 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Trond Myklebust864472e2006-01-03 09:55:15 +01001815 ctx = nfs4_state_find_open_context(state);
1816 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001817 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001818 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 put_nfs_open_context(ctx);
1820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001823#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001824static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001826 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001827 nfs4_stateid *stateid = &state->stateid;
1828 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001829
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001830 /* If a state reset has been done, test_stateid is unneeded */
1831 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1832 return;
1833
1834 status = nfs41_test_stateid(server, stateid);
1835 if (status != NFS_OK) {
1836 /* Free the stateid unless the server explicitly
1837 * informs us the stateid is unrecognized. */
1838 if (status != -NFS4ERR_BAD_STATEID)
1839 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001840 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001841
Bryan Schumaker69388672012-09-26 15:25:52 -04001842 write_seqlock(&state->seqlock);
1843 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1844 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001845 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1846 }
1847}
1848
1849/**
1850 * nfs41_check_open_stateid - possibly free an open stateid
1851 *
1852 * @state: NFSv4 state for an inode
1853 *
1854 * Returns NFS_OK if recovery for this stateid is now finished.
1855 * Otherwise a negative NFS4ERR value is returned.
1856 */
1857static int nfs41_check_open_stateid(struct nfs4_state *state)
1858{
1859 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001860 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001861 int status;
1862
1863 /* If a state reset has been done, test_stateid is unneeded */
1864 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1865 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1867 return -NFS4ERR_BAD_STATEID;
1868
1869 status = nfs41_test_stateid(server, stateid);
1870 if (status != NFS_OK) {
1871 /* Free the stateid unless the server explicitly
1872 * informs us the stateid is unrecognized. */
1873 if (status != -NFS4ERR_BAD_STATEID)
1874 nfs41_free_stateid(server, stateid);
1875
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001879 }
1880 return status;
1881}
1882
1883static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1884{
Chuck Levereb64cf92012-07-11 16:30:05 -04001885 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001886
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001887 nfs41_clear_delegation_stateid(state);
1888 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001889 if (status != NFS_OK)
1890 status = nfs4_open_expired(sp, state);
1891 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001892}
1893#endif
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001896 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1897 * fields corresponding to attributes that were used to store the verifier.
1898 * Make sure we clobber those fields in the later setattr call
1899 */
1900static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1901{
1902 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1903 !(sattr->ia_valid & ATTR_ATIME_SET))
1904 sattr->ia_valid |= ATTR_ATIME;
1905
1906 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1907 !(sattr->ia_valid & ATTR_MTIME_SET))
1908 sattr->ia_valid |= ATTR_MTIME;
1909}
1910
Trond Myklebustc21443c2013-02-07 14:26:21 -05001911static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1912 fmode_t fmode,
1913 int flags,
1914 struct nfs4_state **res)
1915{
1916 struct nfs4_state_owner *sp = opendata->owner;
1917 struct nfs_server *server = sp->so_server;
1918 struct nfs4_state *state;
1919 unsigned int seq;
1920 int ret;
1921
1922 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1923
1924 ret = _nfs4_proc_open(opendata);
1925 if (ret != 0)
1926 goto out;
1927
1928 state = nfs4_opendata_to_nfs4_state(opendata);
1929 ret = PTR_ERR(state);
1930 if (IS_ERR(state))
1931 goto out;
1932 if (server->caps & NFS_CAP_POSIX_LOCK)
1933 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1934
1935 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1936 if (ret != 0)
1937 goto out;
1938
Trond Myklebust91876b12013-03-28 14:01:33 -04001939 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001940 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001941 *res = state;
1942out:
1943 return ret;
1944}
1945
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001946/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001947 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Andy Adamson82be4172012-05-23 05:02:35 -04001949static int _nfs4_do_open(struct inode *dir,
1950 struct dentry *dentry,
1951 fmode_t fmode,
1952 int flags,
1953 struct iattr *sattr,
1954 struct rpc_cred *cred,
1955 struct nfs4_state **res,
1956 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 struct nfs4_state_owner *sp;
1959 struct nfs4_state *state = NULL;
1960 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001961 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001962 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001963 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001967 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1968 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1970 goto out_err;
1971 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001972 status = nfs4_recover_expired_lease(server);
1973 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001974 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001975 if (dentry->d_inode != NULL)
1976 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001977 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001978 if (dentry->d_inode)
1979 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001980 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001981 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001982 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001983 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Andy Adamson82be4172012-05-23 05:02:35 -04001985 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1986 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1987 if (!opendata->f_attr.mdsthreshold)
1988 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001989 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001990 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001991 if (dentry->d_inode != NULL)
1992 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001993
Trond Myklebustc21443c2013-02-07 14:26:21 -05001994 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001995 if (status != 0)
1996 goto err_opendata_put;
1997
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001998 if (opendata->o_arg.open_flags & O_EXCL) {
1999 nfs4_exclusive_attrset(opendata, sattr);
2000
2001 nfs_fattr_init(opendata->o_res.f_attr);
2002 status = nfs4_do_setattr(state->inode, cred,
2003 opendata->o_res.f_attr, sattr,
2004 state);
2005 if (status == 0)
2006 nfs_setattr_update_inode(state->inode, sattr);
2007 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2008 }
Andy Adamson82be4172012-05-23 05:02:35 -04002009
2010 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2011 *ctx_th = opendata->f_attr.mdsthreshold;
2012 else
2013 kfree(opendata->f_attr.mdsthreshold);
2014 opendata->f_attr.mdsthreshold = NULL;
2015
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002016 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *res = state;
2019 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002021 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002022 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002023err_put_state_owner:
2024 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 *res = NULL;
2027 return status;
2028}
2029
2030
Andy Adamson82be4172012-05-23 05:02:35 -04002031static struct nfs4_state *nfs4_do_open(struct inode *dir,
2032 struct dentry *dentry,
2033 fmode_t fmode,
2034 int flags,
2035 struct iattr *sattr,
2036 struct rpc_cred *cred,
2037 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002039 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs4_exception exception = { };
2041 struct nfs4_state *res;
2042 int status;
2043
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002044 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002046 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2047 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (status == 0)
2049 break;
2050 /* NOTE: BAD_SEQID means the server and client disagree about the
2051 * book-keeping w.r.t. state-changing operations
2052 * (OPEN/CLOSE/LOCK/LOCKU...)
2053 * It is actually a sign of a bug on the client or on the server.
2054 *
2055 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002056 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 * have unhashed the old state_owner for us, and that we can
2058 * therefore safely retry using a new one. We should still warn
2059 * the user though...
2060 */
2061 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002062 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002063 " returned a bad sequence-id error!\n",
2064 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 exception.retry = 1;
2066 continue;
2067 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002068 /*
2069 * BAD_STATEID on OPEN means that the server cancelled our
2070 * state before it received the OPEN_CONFIRM.
2071 * Recover by retrying the request as per the discussion
2072 * on Page 181 of RFC3530.
2073 */
2074 if (status == -NFS4ERR_BAD_STATEID) {
2075 exception.retry = 1;
2076 continue;
2077 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002078 if (status == -EAGAIN) {
2079 /* We must have found a delegation */
2080 exception.retry = 1;
2081 continue;
2082 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002083 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2084 continue;
2085 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 status, &exception));
2087 } while (exception.retry);
2088 return res;
2089}
2090
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002091static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2092 struct nfs_fattr *fattr, struct iattr *sattr,
2093 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002095 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002097 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 .iap = sattr,
2099 .server = server,
2100 .bitmask = server->attr_bitmask,
2101 };
2102 struct nfs_setattrres res = {
2103 .fattr = fattr,
2104 .server = server,
2105 };
2106 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2108 .rpc_argp = &arg,
2109 .rpc_resp = &res,
2110 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002112 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002113 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Trond Myklebust0e574af2005-10-27 22:12:38 -04002115 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Trond Myklebust5d422302013-03-14 16:57:48 -04002117 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002118 struct nfs_lockowner lockowner = {
2119 .l_owner = current->files,
2120 .l_pid = current->tgid,
2121 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002122 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002123 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002124 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2125 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002126 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002127 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002128 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Bryan Schumaker7c513052011-03-24 17:12:24 +00002130 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 if (status == 0 && state != NULL)
2132 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002133 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002136static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2137 struct nfs_fattr *fattr, struct iattr *sattr,
2138 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002140 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002141 struct nfs4_exception exception = {
2142 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002143 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002144 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int err;
2146 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002147 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2148 switch (err) {
2149 case -NFS4ERR_OPENMODE:
2150 if (state && !(state->state & FMODE_WRITE)) {
2151 err = -EBADF;
2152 if (sattr->ia_valid & ATTR_OPEN)
2153 err = -EACCES;
2154 goto out;
2155 }
2156 }
2157 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002159out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return err;
2161}
2162
2163struct nfs4_closedata {
2164 struct inode *inode;
2165 struct nfs4_state *state;
2166 struct nfs_closeargs arg;
2167 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002168 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002169 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002170 bool roc;
2171 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172};
2173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002174static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002175{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002176 struct nfs4_closedata *calldata = data;
2177 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002178 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002179
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 if (calldata->roc)
2181 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002182 nfs4_put_open_state(calldata->state);
2183 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002184 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002185 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002186 kfree(calldata);
2187}
2188
Trond Myklebust88069f72009-12-08 08:33:16 -05002189static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2190 fmode_t fmode)
2191{
2192 spin_lock(&state->owner->so_lock);
2193 if (!(fmode & FMODE_READ))
2194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2195 if (!(fmode & FMODE_WRITE))
2196 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2198 spin_unlock(&state->owner->so_lock);
2199}
2200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs_server *server = NFS_SERVER(calldata->inode);
2206
Chuck Levera3ca5652012-03-01 17:00:40 -05002207 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2209 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* hmm. we are done with the inode, and in the process of freeing
2211 * the state_owner. we keep this around to process errors
2212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 switch (task->tk_status) {
2214 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002215 if (calldata->roc)
2216 pnfs_roc_set_barrier(state->inode,
2217 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002218 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002219 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002220 nfs4_close_clear_stateid_flags(state,
2221 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002224 case -NFS4ERR_OLD_STATEID:
2225 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002227 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002230 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2231 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002233 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002234 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002235 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002238static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002240 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002241 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002242 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002243 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002244
Chuck Levera3ca5652012-03-01 17:00:40 -05002245 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002247 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002248
Trond Myklebust88069f72009-12-08 08:33:16 -05002249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2250 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002251 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002252 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002253 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002254 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002255 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2256 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2257 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002258 }
2259 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002260 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2262 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002263 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002264 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002265 if (!nfs4_valid_open_stateid(state))
2266 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002267 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002268
2269 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002270 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002271 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002272 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002273
Fred Isamanf7e89172011-01-06 11:36:32 +00002274 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002275 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002276 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002277 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002278 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002279 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002280
Trond Myklebust516a6af2005-10-27 22:12:41 -04002281 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002282 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002283 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002284 &calldata->arg.seq_args,
2285 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002286 task) != 0)
2287 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002288 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002289 return;
2290out_no_action:
2291 task->tk_action = NULL;
2292out_wait:
2293 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002296static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002297 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002298 .rpc_call_done = nfs4_close_done,
2299 .rpc_release = nfs4_free_closedata,
2300};
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/*
2303 * It is possible for data to be read/written from a mem-mapped file
2304 * after the sys_close call (which hits the vfs layer as a flush).
2305 * This means that we can't safely call nfsv4 close on a file until
2306 * the inode is cleared. This in turn means that we are not good
2307 * NFSv4 citizens - we do not indicate to the server to update the file's
2308 * share state even when we are done with one of the three share
2309 * stateid's in the inode.
2310 *
2311 * NOTE: Caller must be holding the sp->so_owner semaphore!
2312 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002313int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002315 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002317 struct nfs4_state_owner *sp = state->owner;
2318 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002319 struct rpc_message msg = {
2320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2321 .rpc_cred = state->owner->so_cred,
2322 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002323 struct rpc_task_setup task_setup_data = {
2324 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002326 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002327 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002328 .flags = RPC_TASK_ASYNC,
2329 };
Trond Myklebust95121352005-10-18 14:20:12 -07002330 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002332 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002334 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002335 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002336 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002338 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002339 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002341 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002342 if (calldata->arg.seqid == NULL)
2343 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002344 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002345 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002346 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002347 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002348 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002349 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002350 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002351
Trond Myklebust1174dd12010-12-21 10:52:24 -05002352 msg.rpc_argp = &calldata->arg;
2353 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002354 task_setup_data.callback_data = calldata;
2355 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002356 if (IS_ERR(task))
2357 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002358 status = 0;
2359 if (wait)
2360 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002361 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002362 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002363out_free_calldata:
2364 kfree(calldata);
2365out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002366 nfs4_put_open_state(state);
2367 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002368 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Trond Myklebust2b484292010-09-17 10:56:51 -04002371static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002372nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs4_state *state;
2375
Trond Myklebust565277f2007-10-15 18:17:53 -04002376 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002377 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2378 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002379 if (IS_ERR(state))
2380 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002381 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002382 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust1185a552009-12-03 15:54:02 -05002385static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002386{
2387 if (ctx->state == NULL)
2388 return;
2389 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002390 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002391 else
Al Viro643168c2011-06-22 18:20:23 -04002392 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2396{
Benny Halevy43652ad2009-04-01 09:21:54 -04002397 struct nfs4_server_caps_arg args = {
2398 .fhandle = fhandle,
2399 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct nfs4_server_caps_res res = {};
2401 struct rpc_message msg = {
2402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002403 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .rpc_resp = &res,
2405 };
2406 int status;
2407
Bryan Schumaker7c513052011-03-24 17:12:24 +00002408 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (status == 0) {
2410 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002411 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2412 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2413 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2414 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2415 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2417 server->caps |= NFS_CAP_ACLS;
2418 if (res.has_links != 0)
2419 server->caps |= NFS_CAP_HARDLINKS;
2420 if (res.has_symlinks != 0)
2421 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002422 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2423 server->caps |= NFS_CAP_FILEID;
2424 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2425 server->caps |= NFS_CAP_MODE;
2426 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2427 server->caps |= NFS_CAP_NLINK;
2428 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2429 server->caps |= NFS_CAP_OWNER;
2430 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2431 server->caps |= NFS_CAP_OWNER_GROUP;
2432 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2433 server->caps |= NFS_CAP_ATIME;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2435 server->caps |= NFS_CAP_CTIME;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2437 server->caps |= NFS_CAP_MTIME;
2438
Trond Myklebusta65318b2009-03-11 14:10:28 -04002439 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2440 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2441 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002443 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return status;
2447}
2448
Trond Myklebust55a97592006-06-09 09:34:19 -04002449int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(server,
2455 _nfs4_server_capabilities(server, fhandle),
2456 &exception);
2457 } while (exception.retry);
2458 return err;
2459}
2460
2461static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2462 struct nfs_fsinfo *info)
2463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs4_lookup_root_arg args = {
2465 .bitmask = nfs4_fattr_bitmap,
2466 };
2467 struct nfs4_lookup_res res = {
2468 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002469 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .fh = fhandle,
2471 };
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2474 .rpc_argp = &args,
2475 .rpc_resp = &res,
2476 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002477
Trond Myklebust0e574af2005-10-27 22:12:38 -04002478 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002479 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2483 struct nfs_fsinfo *info)
2484{
2485 struct nfs4_exception exception = { };
2486 int err;
2487 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002488 err = _nfs4_lookup_root(server, fhandle, info);
2489 switch (err) {
2490 case 0:
2491 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002492 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002493 default:
2494 err = nfs4_handle_exception(server, err, &exception);
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return err;
2499}
2500
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002501static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2502 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2503{
2504 struct rpc_auth *auth;
2505 int ret;
2506
2507 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002508 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002509 ret = -EIO;
2510 goto out;
2511 }
2512 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002513out:
2514 return ret;
2515}
2516
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002517static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002518 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002520 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002521 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002523 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002524 if (len < 0)
2525 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002526
2527 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002528 /* AUTH_UNIX is the default flavor if none was specified,
2529 * thus has already been tried. */
2530 if (flav_array[i] == RPC_AUTH_UNIX)
2531 continue;
2532
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002533 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002534 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002535 continue;
2536 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002537 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002538 /*
2539 * -EACCESS could mean that the user doesn't have correct permissions
2540 * to access the mount. It could also mean that we tried to mount
2541 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2542 * existing mount programs don't handle -EACCES very well so it should
2543 * be mapped to -EPERM instead.
2544 */
2545 if (status == -EACCES)
2546 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002547 return status;
2548}
2549
2550/*
2551 * get the file handle for the "/" directory on the server
2552 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002553int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2554 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002555{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002556 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002557 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002558 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2559 /*
2560 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2561 * by nfs4_map_errors() as this function exits.
2562 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002563 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (status == 0)
2565 status = nfs4_server_capabilities(server, fhandle);
2566 if (status == 0)
2567 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002568 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Bryan Schumakerbae36242012-05-10 15:07:31 -04002571static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2572 struct nfs_fsinfo *info)
2573{
2574 int error;
2575 struct nfs_fattr *fattr = info->fattr;
2576
2577 error = nfs4_server_capabilities(server, mntfh);
2578 if (error < 0) {
2579 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2580 return error;
2581 }
2582
2583 error = nfs4_proc_getattr(server, mntfh, fattr);
2584 if (error < 0) {
2585 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2586 return error;
2587 }
2588
2589 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2590 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2591 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2592
2593 return error;
2594}
2595
Manoj Naik6b97fd32006-06-09 09:34:29 -04002596/*
2597 * Get locations and (maybe) other attributes of a referral.
2598 * Note that we'll actually follow the referral later when
2599 * we detect fsid mismatch in inode revalidation
2600 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002601static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2602 const struct qstr *name, struct nfs_fattr *fattr,
2603 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002604{
2605 int status = -ENOMEM;
2606 struct page *page = NULL;
2607 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002608
2609 page = alloc_page(GFP_KERNEL);
2610 if (page == NULL)
2611 goto out;
2612 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2613 if (locations == NULL)
2614 goto out;
2615
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002616 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002617 if (status != 0)
2618 goto out;
2619 /* Make sure server returned a different fsid for the referral */
2620 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002621 dprintk("%s: server did not return a different fsid for"
2622 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002623 status = -EIO;
2624 goto out;
2625 }
Andy Adamson533eb462011-06-13 18:25:56 -04002626 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2627 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002628
Andy Adamson533eb462011-06-13 18:25:56 -04002629 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002630 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631 memset(fhandle, 0, sizeof(struct nfs_fh));
2632out:
2633 if (page)
2634 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002635 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002636 return status;
2637}
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2640{
2641 struct nfs4_getattr_arg args = {
2642 .fh = fhandle,
2643 .bitmask = server->attr_bitmask,
2644 };
2645 struct nfs4_getattr_res res = {
2646 .fattr = fattr,
2647 .server = server,
2648 };
2649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2651 .rpc_argp = &args,
2652 .rpc_resp = &res,
2653 };
2654
Trond Myklebust0e574af2005-10-27 22:12:38 -04002655 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002656 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2660{
2661 struct nfs4_exception exception = { };
2662 int err;
2663 do {
2664 err = nfs4_handle_exception(server,
2665 _nfs4_proc_getattr(server, fhandle, fattr),
2666 &exception);
2667 } while (exception.retry);
2668 return err;
2669}
2670
2671/*
2672 * The file is not closed if it is opened due to the a request to change
2673 * the size of the file. The open call will not be needed once the
2674 * VFS layer lookup-intents are implemented.
2675 *
2676 * Close is called when the inode is destroyed.
2677 * If we haven't opened the file for O_WRONLY, we
2678 * need to in the size_change case to obtain a stateid.
2679 *
2680 * Got race?
2681 * Because OPEN is always done by name in nfsv4, it is
2682 * possible that we opened a different file by the same
2683 * name. We can recognize this race condition, but we
2684 * can't do anything about it besides returning an error.
2685 *
2686 * This will be fixed with VFS changes (lookup-intent).
2687 */
2688static int
2689nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2690 struct iattr *sattr)
2691{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002692 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002693 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002694 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 int status;
2696
Benny Halevy8a1636c2010-07-14 15:43:57 -04002697 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002698 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002699
Trond Myklebust0e574af2005-10-27 22:12:38 -04002700 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Andy Adamson26699402012-05-30 16:12:24 -04002702 /* Deal with open(O_TRUNC) */
2703 if (sattr->ia_valid & ATTR_OPEN)
2704 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2705
2706 /* Optimization: if the end result is no change, don't RPC */
2707 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2708 return 0;
2709
Trond Myklebustd5308382005-11-04 15:33:38 -05002710 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002711 if (sattr->ia_valid & ATTR_FILE) {
2712 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002713
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002714 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002715 if (ctx) {
2716 cred = ctx->cred;
2717 state = ctx->state;
2718 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002719 }
2720
2721 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002722 if (status == 0)
2723 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return status;
2725}
2726
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002727static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2728 const struct qstr *name, struct nfs_fh *fhandle,
2729 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002730{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002731 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002732 int status;
2733 struct nfs4_lookup_arg args = {
2734 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002735 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002736 .name = name,
2737 };
2738 struct nfs4_lookup_res res = {
2739 .server = server,
2740 .fattr = fattr,
2741 .fh = fhandle,
2742 };
2743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2745 .rpc_argp = &args,
2746 .rpc_resp = &res,
2747 };
2748
2749 nfs_fattr_init(fattr);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002752 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 dprintk("NFS reply lookup: %d\n", status);
2754 return status;
2755}
2756
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002757static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002758{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002759 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002760 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002761 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2762 fattr->nlink = 2;
2763}
2764
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002765static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2766 struct qstr *name, struct nfs_fh *fhandle,
2767 struct nfs_fattr *fattr)
2768{
2769 struct nfs4_exception exception = { };
2770 struct rpc_clnt *client = *clnt;
2771 int err;
2772 do {
2773 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2774 switch (err) {
2775 case -NFS4ERR_BADNAME:
2776 err = -ENOENT;
2777 goto out;
2778 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002779 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002780 goto out;
2781 case -NFS4ERR_WRONGSEC:
2782 err = -EPERM;
2783 if (client != *clnt)
2784 goto out;
2785
2786 client = nfs4_create_sec_client(client, dir, name);
2787 if (IS_ERR(client))
2788 return PTR_ERR(client);
2789
2790 exception.retry = 1;
2791 break;
2792 default:
2793 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2794 }
2795 } while (exception.retry);
2796
2797out:
2798 if (err == 0)
2799 *clnt = client;
2800 else if (client != *clnt)
2801 rpc_shutdown_client(client);
2802
2803 return err;
2804}
2805
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002806static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002807 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002809 int status;
2810 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002811
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002812 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2813 if (client != NFS_CLIENT(dir)) {
2814 rpc_shutdown_client(client);
2815 nfs_fixup_secinfo_attributes(fattr);
2816 }
2817 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002820struct rpc_clnt *
2821nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2822 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2823{
2824 int status;
2825 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2826
2827 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2828 if (status < 0) {
2829 rpc_shutdown_client(client);
2830 return ERR_PTR(status);
2831 }
2832 return client;
2833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2836{
Trond Myklebust76b32992007-08-10 17:45:11 -04002837 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 struct nfs4_accessargs args = {
2839 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002840 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002842 struct nfs4_accessres res = {
2843 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002844 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 struct rpc_message msg = {
2846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2847 .rpc_argp = &args,
2848 .rpc_resp = &res,
2849 .rpc_cred = entry->cred,
2850 };
2851 int mode = entry->mask;
2852 int status;
2853
2854 /*
2855 * Determine which access bits we want to ask for...
2856 */
2857 if (mode & MAY_READ)
2858 args.access |= NFS4_ACCESS_READ;
2859 if (S_ISDIR(inode->i_mode)) {
2860 if (mode & MAY_WRITE)
2861 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2862 if (mode & MAY_EXEC)
2863 args.access |= NFS4_ACCESS_LOOKUP;
2864 } else {
2865 if (mode & MAY_WRITE)
2866 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2867 if (mode & MAY_EXEC)
2868 args.access |= NFS4_ACCESS_EXECUTE;
2869 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002870
2871 res.fattr = nfs_alloc_fattr();
2872 if (res.fattr == NULL)
2873 return -ENOMEM;
2874
Bryan Schumaker7c513052011-03-24 17:12:24 +00002875 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002877 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002878 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002880 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return status;
2882}
2883
2884static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2885{
2886 struct nfs4_exception exception = { };
2887 int err;
2888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(inode),
2890 _nfs4_proc_access(inode, entry),
2891 &exception);
2892 } while (exception.retry);
2893 return err;
2894}
2895
2896/*
2897 * TODO: For the time being, we don't try to get any attributes
2898 * along with any of the zero-copy operations READ, READDIR,
2899 * READLINK, WRITE.
2900 *
2901 * In the case of the first three, we want to put the GETATTR
2902 * after the read-type operation -- this is because it is hard
2903 * to predict the length of a GETATTR response in v4, and thus
2904 * align the READ data correctly. This means that the GETATTR
2905 * may end up partially falling into the page cache, and we should
2906 * shift it into the 'tail' of the xdr_buf before processing.
2907 * To do this efficiently, we need to know the total length
2908 * of data received, which doesn't seem to be available outside
2909 * of the RPC layer.
2910 *
2911 * In the case of WRITE, we also want to put the GETATTR after
2912 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002913 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 *
2915 * Both of these changes to the XDR layer would in fact be quite
2916 * minor, but I decided to leave them for a subsequent patch.
2917 */
2918static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2919 unsigned int pgbase, unsigned int pglen)
2920{
2921 struct nfs4_readlink args = {
2922 .fh = NFS_FH(inode),
2923 .pgbase = pgbase,
2924 .pglen = pglen,
2925 .pages = &page,
2926 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002927 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2930 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002931 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 };
2933
Bryan Schumaker7c513052011-03-24 17:12:24 +00002934 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 -07002935}
2936
2937static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2938 unsigned int pgbase, unsigned int pglen)
2939{
2940 struct nfs4_exception exception = { };
2941 int err;
2942 do {
2943 err = nfs4_handle_exception(NFS_SERVER(inode),
2944 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2945 &exception);
2946 } while (exception.retry);
2947 return err;
2948}
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002951 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953static int
2954nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002955 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002957 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 int status = 0;
2960
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002961 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2962 if (IS_ERR(ctx))
2963 return PTR_ERR(ctx);
2964
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002965 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002966 state = nfs4_do_open(dir, dentry, ctx->mode,
2967 flags, sattr, ctx->cred,
2968 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002969 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (IS_ERR(state)) {
2971 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002974 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002975 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002976 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002978 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return status;
2980}
2981
2982static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2983{
Trond Myklebust16e42952005-10-27 22:12:44 -04002984 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002985 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002987 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002989 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002990 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002997 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002998
Bryan Schumaker7c513052011-03-24 17:12:24 +00002999 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003000 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003001 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return status;
3003}
3004
3005static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3006{
3007 struct nfs4_exception exception = { };
3008 int err;
3009 do {
3010 err = nfs4_handle_exception(NFS_SERVER(dir),
3011 _nfs4_proc_remove(dir, name),
3012 &exception);
3013 } while (exception.retry);
3014 return err;
3015}
3016
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003017static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003019 struct nfs_server *server = NFS_SERVER(dir);
3020 struct nfs_removeargs *args = msg->rpc_argp;
3021 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003023 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003025 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003028static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3029{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003030 nfs4_setup_sequence(NFS_SERVER(data->dir),
3031 &data->args.seq_args,
3032 &data->res.seq_res,
3033 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003036static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003038 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3039
Trond Myklebust14516c32010-07-31 14:29:06 -04003040 if (!nfs4_sequence_done(task, &res->seq_res))
3041 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003042 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003043 return 0;
3044 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003045 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Jeff Laytond3d41522010-09-17 17:31:57 -04003048static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3049{
3050 struct nfs_server *server = NFS_SERVER(dir);
3051 struct nfs_renameargs *arg = msg->rpc_argp;
3052 struct nfs_renameres *res = msg->rpc_resp;
3053
3054 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003055 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003056 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003057}
3058
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003059static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3060{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003061 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3062 &data->args.seq_args,
3063 &data->res.seq_res,
3064 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003065}
3066
3067static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3068 struct inode *new_dir)
3069{
3070 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3071
3072 if (!nfs4_sequence_done(task, &res->seq_res))
3073 return 0;
3074 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3075 return 0;
3076
3077 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003078 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003079 return 1;
3080}
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3083 struct inode *new_dir, struct qstr *new_name)
3084{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003085 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003086 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 .old_dir = NFS_FH(old_dir),
3088 .new_dir = NFS_FH(new_dir),
3089 .old_name = old_name,
3090 .new_name = new_name,
3091 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003092 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003093 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003094 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 struct rpc_message msg = {
3096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3097 .rpc_argp = &arg,
3098 .rpc_resp = &res,
3099 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003100 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Bryan Schumaker7c513052011-03-24 17:12:24 +00003102 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (!status) {
3104 update_changeattr(old_dir, &res.old_cinfo);
3105 update_changeattr(new_dir, &res.new_cinfo);
3106 }
3107 return status;
3108}
3109
3110static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3111 struct inode *new_dir, struct qstr *new_name)
3112{
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3117 _nfs4_proc_rename(old_dir, old_name,
3118 new_dir, new_name),
3119 &exception);
3120 } while (exception.retry);
3121 return err;
3122}
3123
3124static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3125{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003126 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 struct nfs4_link_arg arg = {
3128 .fh = NFS_FH(inode),
3129 .dir_fh = NFS_FH(dir),
3130 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003131 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003133 struct nfs4_link_res res = {
3134 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct rpc_message msg = {
3137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3138 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003139 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003141 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Trond Myklebust136f2622010-04-16 16:22:49 -04003143 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003144 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003145 goto out;
3146
Bryan Schumaker7c513052011-03-24 17:12:24 +00003147 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003148 if (!status) {
3149 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003150 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003152out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003153 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return status;
3155}
3156
3157static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3158{
3159 struct nfs4_exception exception = { };
3160 int err;
3161 do {
3162 err = nfs4_handle_exception(NFS_SERVER(inode),
3163 _nfs4_proc_link(inode, dir, name),
3164 &exception);
3165 } while (exception.retry);
3166 return err;
3167}
3168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169struct nfs4_createdata {
3170 struct rpc_message msg;
3171 struct nfs4_create_arg arg;
3172 struct nfs4_create_res res;
3173 struct nfs_fh fh;
3174 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003175};
3176
3177static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3178 struct qstr *name, struct iattr *sattr, u32 ftype)
3179{
3180 struct nfs4_createdata *data;
3181
3182 data = kzalloc(sizeof(*data), GFP_KERNEL);
3183 if (data != NULL) {
3184 struct nfs_server *server = NFS_SERVER(dir);
3185
3186 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3187 data->msg.rpc_argp = &data->arg;
3188 data->msg.rpc_resp = &data->res;
3189 data->arg.dir_fh = NFS_FH(dir);
3190 data->arg.server = server;
3191 data->arg.name = name;
3192 data->arg.attrs = sattr;
3193 data->arg.ftype = ftype;
3194 data->arg.bitmask = server->attr_bitmask;
3195 data->res.server = server;
3196 data->res.fh = &data->fh;
3197 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003198 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 }
3200 return data;
3201}
3202
3203static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3204{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003205 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003206 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 if (status == 0) {
3208 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003209 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3210 }
3211 return status;
3212}
3213
3214static void nfs4_free_createdata(struct nfs4_createdata *data)
3215{
3216 kfree(data);
3217}
3218
Chuck Lever4f390c12006-08-22 20:06:22 -04003219static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003220 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003222 struct nfs4_createdata *data;
3223 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Chuck Lever94a6d752006-08-22 20:06:23 -04003225 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003226 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003227
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 status = -ENOMEM;
3229 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3230 if (data == NULL)
3231 goto out;
3232
3233 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3234 data->arg.u.symlink.pages = &page;
3235 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237 status = nfs4_do_create(dir, dentry, data);
3238
3239 nfs4_free_createdata(data);
3240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return status;
3242}
3243
Chuck Lever4f390c12006-08-22 20:06:22 -04003244static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003245 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 struct nfs4_exception exception = { };
3248 int err;
3249 do {
3250 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003251 _nfs4_proc_symlink(dir, dentry, page,
3252 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 &exception);
3254 } while (exception.retry);
3255 return err;
3256}
3257
3258static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3259 struct iattr *sattr)
3260{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003261 struct nfs4_createdata *data;
3262 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3265 if (data == NULL)
3266 goto out;
3267
3268 status = nfs4_do_create(dir, dentry, data);
3269
3270 nfs4_free_createdata(data);
3271out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return status;
3273}
3274
3275static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3276 struct iattr *sattr)
3277{
3278 struct nfs4_exception exception = { };
3279 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003280
3281 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 do {
3283 err = nfs4_handle_exception(NFS_SERVER(dir),
3284 _nfs4_proc_mkdir(dir, dentry, sattr),
3285 &exception);
3286 } while (exception.retry);
3287 return err;
3288}
3289
3290static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003291 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 struct inode *dir = dentry->d_inode;
3294 struct nfs4_readdir_arg args = {
3295 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003296 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 .pgbase = 0,
3298 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003299 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003300 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 };
3302 struct nfs4_readdir_res res;
3303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3305 .rpc_argp = &args,
3306 .rpc_resp = &res,
3307 .rpc_cred = cred,
3308 };
3309 int status;
3310
Harvey Harrison3110ff82008-05-02 13:42:44 -07003311 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003312 dentry->d_parent->d_name.name,
3313 dentry->d_name.name,
3314 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003315 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003317 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 -05003318 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003319 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003320 status += args.pgbase;
3321 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003322
3323 nfs_invalidate_atime(dir);
3324
Harvey Harrison3110ff82008-05-02 13:42:44 -07003325 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return status;
3327}
3328
3329static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003330 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332 struct nfs4_exception exception = { };
3333 int err;
3334 do {
3335 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3336 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003337 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 &exception);
3339 } while (exception.retry);
3340 return err;
3341}
3342
3343static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3344 struct iattr *sattr, dev_t rdev)
3345{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003346 struct nfs4_createdata *data;
3347 int mode = sattr->ia_mode;
3348 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3351 if (data == NULL)
3352 goto out;
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003355 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003357 data->arg.ftype = NF4BLK;
3358 data->arg.u.device.specdata1 = MAJOR(rdev);
3359 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003362 data->arg.ftype = NF4CHR;
3363 data->arg.u.device.specdata1 = MAJOR(rdev);
3364 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003365 } else if (!S_ISSOCK(mode)) {
3366 status = -EINVAL;
3367 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003370 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003371out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003372 nfs4_free_createdata(data);
3373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return status;
3375}
3376
3377static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3378 struct iattr *sattr, dev_t rdev)
3379{
3380 struct nfs4_exception exception = { };
3381 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003382
3383 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 do {
3385 err = nfs4_handle_exception(NFS_SERVER(dir),
3386 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3387 &exception);
3388 } while (exception.retry);
3389 return err;
3390}
3391
3392static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3393 struct nfs_fsstat *fsstat)
3394{
3395 struct nfs4_statfs_arg args = {
3396 .fh = fhandle,
3397 .bitmask = server->attr_bitmask,
3398 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003399 struct nfs4_statfs_res res = {
3400 .fsstat = fsstat,
3401 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 struct rpc_message msg = {
3403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3404 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003405 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 };
3407
Trond Myklebust0e574af2005-10-27 22:12:38 -04003408 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003409 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
3412static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
3416 do {
3417 err = nfs4_handle_exception(server,
3418 _nfs4_proc_statfs(server, fhandle, fsstat),
3419 &exception);
3420 } while (exception.retry);
3421 return err;
3422}
3423
3424static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *fsinfo)
3426{
3427 struct nfs4_fsinfo_arg args = {
3428 .fh = fhandle,
3429 .bitmask = server->attr_bitmask,
3430 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003431 struct nfs4_fsinfo_res res = {
3432 .fsinfo = fsinfo,
3433 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3436 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003437 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 };
3439
Bryan Schumaker7c513052011-03-24 17:12:24 +00003440 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
3443static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3444{
3445 struct nfs4_exception exception = { };
3446 int err;
3447
3448 do {
3449 err = nfs4_handle_exception(server,
3450 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3451 &exception);
3452 } while (exception.retry);
3453 return err;
3454}
3455
3456static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3457{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003458 int error;
3459
Trond Myklebust0e574af2005-10-27 22:12:38 -04003460 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003461 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003462 if (error == 0) {
3463 /* block layout checks this! */
3464 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003465 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003466 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003467
3468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
3471static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3472 struct nfs_pathconf *pathconf)
3473{
3474 struct nfs4_pathconf_arg args = {
3475 .fh = fhandle,
3476 .bitmask = server->attr_bitmask,
3477 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003478 struct nfs4_pathconf_res res = {
3479 .pathconf = pathconf,
3480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct rpc_message msg = {
3482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3483 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003484 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 };
3486
3487 /* None of the pathconf attributes are mandatory to implement */
3488 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3489 memset(pathconf, 0, sizeof(*pathconf));
3490 return 0;
3491 }
3492
Trond Myklebust0e574af2005-10-27 22:12:38 -04003493 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
3497static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3498 struct nfs_pathconf *pathconf)
3499{
3500 struct nfs4_exception exception = { };
3501 int err;
3502
3503 do {
3504 err = nfs4_handle_exception(server,
3505 _nfs4_proc_pathconf(server, fhandle, pathconf),
3506 &exception);
3507 } while (exception.retry);
3508 return err;
3509}
3510
Trond Myklebust5521abf2013-03-16 20:54:34 -04003511int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003512 const struct nfs_open_context *ctx,
3513 const struct nfs_lock_context *l_ctx,
3514 fmode_t fmode)
3515{
3516 const struct nfs_lockowner *lockowner = NULL;
3517
3518 if (l_ctx != NULL)
3519 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003520 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003521}
3522EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3523
Trond Myklebust5521abf2013-03-16 20:54:34 -04003524static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3525 const struct nfs_open_context *ctx,
3526 const struct nfs_lock_context *l_ctx,
3527 fmode_t fmode)
3528{
3529 nfs4_stateid current_stateid;
3530
3531 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3532 return false;
3533 return nfs4_stateid_match(stateid, &current_stateid);
3534}
3535
3536static bool nfs4_error_stateid_expired(int err)
3537{
3538 switch (err) {
3539 case -NFS4ERR_DELEG_REVOKED:
3540 case -NFS4ERR_ADMIN_REVOKED:
3541 case -NFS4ERR_BAD_STATEID:
3542 case -NFS4ERR_STALE_STATEID:
3543 case -NFS4ERR_OLD_STATEID:
3544 case -NFS4ERR_OPENMODE:
3545 case -NFS4ERR_EXPIRED:
3546 return true;
3547 }
3548 return false;
3549}
3550
Benny Halevyd20581a2011-05-22 19:52:03 +03003551void __nfs4_read_done_cb(struct nfs_read_data *data)
3552{
Fred Isamancd841602012-04-20 14:47:44 -04003553 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003554}
3555
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003556static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
Fred Isamancd841602012-04-20 14:47:44 -04003558 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003560 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003561 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003562 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003564
Benny Halevyd20581a2011-05-22 19:52:03 +03003565 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003567 renew_lease(server, data->timestamp);
3568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
Trond Myklebust5521abf2013-03-16 20:54:34 -04003571static bool nfs4_read_stateid_changed(struct rpc_task *task,
3572 struct nfs_readargs *args)
3573{
3574
3575 if (!nfs4_error_stateid_expired(task->tk_status) ||
3576 nfs4_stateid_is_current(&args->stateid,
3577 args->context,
3578 args->lock_context,
3579 FMODE_READ))
3580 return false;
3581 rpc_restart_call_prepare(task);
3582 return true;
3583}
3584
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003585static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3586{
3587
3588 dprintk("--> %s\n", __func__);
3589
3590 if (!nfs4_sequence_done(task, &data->res.seq_res))
3591 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003592 if (nfs4_read_stateid_changed(task, &data->args))
3593 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003594 return data->read_done_cb ? data->read_done_cb(task, data) :
3595 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003596}
3597
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003598static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003601 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003603 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003606static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3607{
Trond Myklebust9b206142013-03-17 15:52:00 -04003608 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003609 &data->args.seq_args,
3610 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003611 task))
3612 return;
3613 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3614 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Fred Isamanb029bc92011-03-03 15:13:42 +00003617static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Fred Isamancd841602012-04-20 14:47:44 -04003619 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003621 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003622 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003623 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003625 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003627 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003628 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Trond Myklebust5521abf2013-03-16 20:54:34 -04003632static bool nfs4_write_stateid_changed(struct rpc_task *task,
3633 struct nfs_writeargs *args)
3634{
3635
3636 if (!nfs4_error_stateid_expired(task->tk_status) ||
3637 nfs4_stateid_is_current(&args->stateid,
3638 args->context,
3639 args->lock_context,
3640 FMODE_WRITE))
3641 return false;
3642 rpc_restart_call_prepare(task);
3643 return true;
3644}
3645
Fred Isamanb029bc92011-03-03 15:13:42 +00003646static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3647{
3648 if (!nfs4_sequence_done(task, &data->res.seq_res))
3649 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003650 if (nfs4_write_stateid_changed(task, &data->args))
3651 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003652 return data->write_done_cb ? data->write_done_cb(task, data) :
3653 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003654}
3655
Trond Myklebust5a37f852012-04-28 14:55:16 -04003656static
3657bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003658{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003659 const struct nfs_pgio_header *hdr = data->header;
3660
3661 /* Don't request attributes for pNFS or O_DIRECT writes */
3662 if (data->ds_clp != NULL || hdr->dreq != NULL)
3663 return false;
3664 /* Otherwise, request attributes if and only if we don't hold
3665 * a delegation
3666 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003667 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003668}
Fred Isamana69aef12011-03-03 15:13:47 +00003669
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003670static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Fred Isamancd841602012-04-20 14:47:44 -04003672 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003673
Trond Myklebust5a37f852012-04-28 14:55:16 -04003674 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003675 data->args.bitmask = NULL;
3676 data->res.fattr = NULL;
3677 } else
3678 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003679
Fred Isamanb029bc92011-03-03 15:13:42 +00003680 if (!data->write_done_cb)
3681 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003682 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 data->timestamp = jiffies;
3684
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003685 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003686 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003689static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3690{
Trond Myklebust9b206142013-03-17 15:52:00 -04003691 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003692 &data->args.seq_args,
3693 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003694 task))
3695 return;
3696 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3697 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Fred Isaman0b7c0152012-04-20 14:47:39 -04003700static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3701{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003702 nfs4_setup_sequence(NFS_SERVER(data->inode),
3703 &data->args.seq_args,
3704 &data->res.seq_res,
3705 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Fred Isaman0b7c0152012-04-20 14:47:39 -04003708static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003711
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003712 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003713 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003714 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Fred Isaman0b7c0152012-04-20 14:47:39 -04003719static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003720{
3721 if (!nfs4_sequence_done(task, &data->res.seq_res))
3722 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003723 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003724}
3725
Fred Isaman0b7c0152012-04-20 14:47:39 -04003726static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003728 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003729
Fred Isaman0b7c0152012-04-20 14:47:39 -04003730 if (data->commit_done_cb == NULL)
3731 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003732 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003733 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003734 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003737struct nfs4_renewdata {
3738 struct nfs_client *client;
3739 unsigned long timestamp;
3740};
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742/*
3743 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3744 * standalone procedure for queueing an asynchronous RENEW.
3745 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003746static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003747{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003748 struct nfs4_renewdata *data = calldata;
3749 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003750
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003751 if (atomic_read(&clp->cl_count) > 1)
3752 nfs4_schedule_state_renewal(clp);
3753 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003754 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003755}
3756
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003759 struct nfs4_renewdata *data = calldata;
3760 struct nfs_client *clp = data->client;
3761 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003764 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003765 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3766 return;
3767 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003768 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003769 return;
3770 }
3771 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003773 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003776static const struct rpc_call_ops nfs4_renew_ops = {
3777 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003778 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003779};
3780
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003781static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 struct rpc_message msg = {
3784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3785 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003786 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003788 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003790 if (renew_flags == 0)
3791 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003792 if (!atomic_inc_not_zero(&clp->cl_count))
3793 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003794 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003795 if (data == NULL)
3796 return -ENOMEM;
3797 data->client = clp;
3798 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003800 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003803static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3807 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003808 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 };
3810 unsigned long now = jiffies;
3811 int status;
3812
3813 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3814 if (status < 0)
3815 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003816 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return 0;
3818}
3819
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003820static inline int nfs4_server_supports_acls(struct nfs_server *server)
3821{
3822 return (server->caps & NFS_CAP_ACLS)
3823 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3824 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3825}
3826
Trond Myklebust21f498c2012-08-24 10:59:25 -04003827/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3828 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003829 * the stack.
3830 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003831#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003832
Neil Hormane9e3d722011-03-04 19:26:03 -05003833static int buf_to_pages_noslab(const void *buf, size_t buflen,
3834 struct page **pages, unsigned int *pgbase)
3835{
3836 struct page *newpage, **spages;
3837 int rc = 0;
3838 size_t len;
3839 spages = pages;
3840
3841 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003842 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003843 newpage = alloc_page(GFP_KERNEL);
3844
3845 if (newpage == NULL)
3846 goto unwind;
3847 memcpy(page_address(newpage), buf, len);
3848 buf += len;
3849 buflen -= len;
3850 *pages++ = newpage;
3851 rc++;
3852 } while (buflen != 0);
3853
3854 return rc;
3855
3856unwind:
3857 for(; rc > 0; rc--)
3858 __free_page(spages[rc-1]);
3859 return -ENOMEM;
3860}
3861
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003862struct nfs4_cached_acl {
3863 int cached;
3864 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003865 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003866};
3867
3868static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003869{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003870 struct nfs_inode *nfsi = NFS_I(inode);
3871
3872 spin_lock(&inode->i_lock);
3873 kfree(nfsi->nfs4_acl);
3874 nfsi->nfs4_acl = acl;
3875 spin_unlock(&inode->i_lock);
3876}
3877
3878static void nfs4_zap_acl_attr(struct inode *inode)
3879{
3880 nfs4_set_cached_acl(inode, NULL);
3881}
3882
3883static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3884{
3885 struct nfs_inode *nfsi = NFS_I(inode);
3886 struct nfs4_cached_acl *acl;
3887 int ret = -ENOENT;
3888
3889 spin_lock(&inode->i_lock);
3890 acl = nfsi->nfs4_acl;
3891 if (acl == NULL)
3892 goto out;
3893 if (buf == NULL) /* user is just asking for length */
3894 goto out_len;
3895 if (acl->cached == 0)
3896 goto out;
3897 ret = -ERANGE; /* see getxattr(2) man page */
3898 if (acl->len > buflen)
3899 goto out;
3900 memcpy(buf, acl->data, acl->len);
3901out_len:
3902 ret = acl->len;
3903out:
3904 spin_unlock(&inode->i_lock);
3905 return ret;
3906}
3907
Sachin Prabhu5794d212012-04-17 14:36:40 +01003908static 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 +00003909{
3910 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003911 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003912
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003913 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003914 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003915 if (acl == NULL)
3916 goto out;
3917 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003918 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003919 } else {
3920 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3921 if (acl == NULL)
3922 goto out;
3923 acl->cached = 0;
3924 }
3925 acl->len = acl_len;
3926out:
3927 nfs4_set_cached_acl(inode, acl);
3928}
3929
Andy Adamsonbf118a32011-12-07 11:55:27 -05003930/*
3931 * The getxattr API returns the required buffer length when called with a
3932 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3933 * the required buf. On a NULL buf, we send a page of data to the server
3934 * guessing that the ACL request can be serviced by a page. If so, we cache
3935 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3936 * the cache. If not so, we throw away the page, and cache the required
3937 * length. The next getxattr call will then produce another round trip to
3938 * the server, this time with the input buf of the required size.
3939 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003940static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003941{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003942 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003943 struct nfs_getaclargs args = {
3944 .fh = NFS_FH(inode),
3945 .acl_pages = pages,
3946 .acl_len = buflen,
3947 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003948 struct nfs_getaclres res = {
3949 .acl_len = buflen,
3950 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3953 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003954 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003955 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003956 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3957 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003958
Andy Adamsonbf118a32011-12-07 11:55:27 -05003959 /* As long as we're doing a round trip to the server anyway,
3960 * let's be prepared for a page of acl data. */
3961 if (npages == 0)
3962 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003963 if (npages > ARRAY_SIZE(pages))
3964 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003965
Andy Adamsonbf118a32011-12-07 11:55:27 -05003966 for (i = 0; i < npages; i++) {
3967 pages[i] = alloc_page(GFP_KERNEL);
3968 if (!pages[i])
3969 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003970 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003971
3972 /* for decoding across pages */
3973 res.acl_scratch = alloc_page(GFP_KERNEL);
3974 if (!res.acl_scratch)
3975 goto out_free;
3976
Andy Adamsonbf118a32011-12-07 11:55:27 -05003977 args.acl_len = npages * PAGE_SIZE;
3978 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003979
Peng Taode040be2012-01-10 22:42:47 +08003980 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003981 __func__, buf, buflen, npages, args.acl_len);
3982 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3983 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003984 if (ret)
3985 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003986
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003987 /* Handle the case where the passed-in buffer is too short */
3988 if (res.acl_flags & NFS4_ACL_TRUNC) {
3989 /* Did the user only issue a request for the acl length? */
3990 if (buf == NULL)
3991 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003992 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003993 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003994 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003995 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003996 if (buf) {
3997 if (res.acl_len > buflen) {
3998 ret = -ERANGE;
3999 goto out_free;
4000 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004001 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004002 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004003out_ok:
4004 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004005out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004006 for (i = 0; i < npages; i++)
4007 if (pages[i])
4008 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004009 if (res.acl_scratch)
4010 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004011 return ret;
4012}
4013
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004014static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4015{
4016 struct nfs4_exception exception = { };
4017 ssize_t ret;
4018 do {
4019 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4020 if (ret >= 0)
4021 break;
4022 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4023 } while (exception.retry);
4024 return ret;
4025}
4026
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004027static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4028{
4029 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004030 int ret;
4031
4032 if (!nfs4_server_supports_acls(server))
4033 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004034 ret = nfs_revalidate_inode(server, inode);
4035 if (ret < 0)
4036 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004037 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4038 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004039 ret = nfs4_read_cached_acl(inode, buf, buflen);
4040 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004041 /* -ENOENT is returned if there is no ACL or if there is an ACL
4042 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004043 return ret;
4044 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004045}
4046
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004047static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004048{
4049 struct nfs_server *server = NFS_SERVER(inode);
4050 struct page *pages[NFS4ACL_MAXPAGES];
4051 struct nfs_setaclargs arg = {
4052 .fh = NFS_FH(inode),
4053 .acl_pages = pages,
4054 .acl_len = buflen,
4055 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004056 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4059 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004060 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004061 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004062 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004063 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004064
4065 if (!nfs4_server_supports_acls(server))
4066 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004067 if (npages > ARRAY_SIZE(pages))
4068 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004069 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4070 if (i < 0)
4071 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004072 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004073 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004074
4075 /*
4076 * Free each page after tx, so the only ref left is
4077 * held by the network stack
4078 */
4079 for (; i > 0; i--)
4080 put_page(pages[i-1]);
4081
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004082 /*
4083 * Acl update can result in inode attribute update.
4084 * so mark the attribute cache invalid.
4085 */
4086 spin_lock(&inode->i_lock);
4087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4088 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004089 nfs_access_zap_cache(inode);
4090 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004091 return ret;
4092}
4093
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004094static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4095{
4096 struct nfs4_exception exception = { };
4097 int err;
4098 do {
4099 err = nfs4_handle_exception(NFS_SERVER(inode),
4100 __nfs4_proc_set_acl(inode, buf, buflen),
4101 &exception);
4102 } while (exception.retry);
4103 return err;
4104}
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004107nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004109 struct nfs_client *clp = server->nfs_client;
4110
4111 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return 0;
4113 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004114 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004115 case -NFS4ERR_ADMIN_REVOKED:
4116 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004117 if (state == NULL)
4118 break;
4119 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004120 case -NFS4ERR_OPENMODE:
4121 if (state == NULL)
4122 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004123 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4124 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004125 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004126 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004127 if (state != NULL) {
4128 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4129 goto stateid_invalid;
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004132 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004133 nfs4_schedule_lease_recovery(clp);
4134 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004135#if defined(CONFIG_NFS_V4_1)
4136 case -NFS4ERR_BADSESSION:
4137 case -NFS4ERR_BADSLOT:
4138 case -NFS4ERR_BAD_HIGH_SLOT:
4139 case -NFS4ERR_DEADSESSION:
4140 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4141 case -NFS4ERR_SEQ_FALSE_RETRY:
4142 case -NFS4ERR_SEQ_MISORDERED:
4143 dprintk("%s ERROR %d, Reset session\n", __func__,
4144 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004145 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004146 task->tk_status = 0;
4147 return -EAGAIN;
4148#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004150 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004151 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4153 task->tk_status = 0;
4154 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004155 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 case -NFS4ERR_OLD_STATEID:
4157 task->tk_status = 0;
4158 return -EAGAIN;
4159 }
4160 task->tk_status = nfs4_map_errors(task->tk_status);
4161 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004162stateid_invalid:
4163 task->tk_status = -EIO;
4164 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004165wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004166 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004167 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4168 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4169 task->tk_status = 0;
4170 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
Chuck Leverf0920752012-05-21 22:45:41 -04004173static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4174 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004175{
4176 __be32 verf[2];
4177
Chuck Lever2c820d92012-05-21 22:45:33 -04004178 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4179 /* An impossible timestamp guarantees this value
4180 * will never match a generated boot time. */
4181 verf[0] = 0;
4182 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4183 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004184 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4185 verf[0] = (__be32)nn->boot_time.tv_sec;
4186 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004187 }
Chuck Levercd937102012-03-02 17:14:31 -05004188 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4189}
4190
Chuck Levere984a552012-09-14 17:24:21 -04004191static unsigned int
4192nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4193 char *buf, size_t len)
4194{
4195 unsigned int result;
4196
4197 rcu_read_lock();
4198 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4199 clp->cl_ipaddr,
4200 rpc_peeraddr2str(clp->cl_rpcclient,
4201 RPC_DISPLAY_ADDR),
4202 rpc_peeraddr2str(clp->cl_rpcclient,
4203 RPC_DISPLAY_PROTO));
4204 rcu_read_unlock();
4205 return result;
4206}
4207
4208static unsigned int
4209nfs4_init_uniform_client_string(const struct nfs_client *clp,
4210 char *buf, size_t len)
4211{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004212 char *nodename = clp->cl_rpcclient->cl_nodename;
4213
4214 if (nfs4_client_id_uniquifier[0] != '\0')
4215 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004216 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4217 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004218 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004219}
4220
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004221/**
4222 * nfs4_proc_setclientid - Negotiate client ID
4223 * @clp: state data structure
4224 * @program: RPC program for NFSv4 callback service
4225 * @port: IP port number for NFS4 callback service
4226 * @cred: RPC credential to use for this call
4227 * @res: where to place the result
4228 *
4229 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4230 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004231int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4232 unsigned short port, struct rpc_cred *cred,
4233 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
4235 nfs4_verifier sc_verifier;
4236 struct nfs4_setclientid setclientid = {
4237 .sc_verifier = &sc_verifier,
4238 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004239 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 };
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4243 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004244 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004245 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004247 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Chuck Leverde734832012-07-11 16:30:50 -04004249 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004250 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004251 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4252 setclientid.sc_name_len =
4253 nfs4_init_uniform_client_string(clp,
4254 setclientid.sc_name,
4255 sizeof(setclientid.sc_name));
4256 else
4257 setclientid.sc_name_len =
4258 nfs4_init_nonuniform_client_string(clp,
4259 setclientid.sc_name,
4260 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004261 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004262 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004263 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004264 sizeof(setclientid.sc_netid),
4265 rpc_peeraddr2str(clp->cl_rpcclient,
4266 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004267 rcu_read_unlock();
4268 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004269 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 clp->cl_ipaddr, port >> 8, port & 255);
4271
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004272 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4273 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4274 setclientid.sc_name_len, setclientid.sc_name);
4275 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4276 dprintk("NFS reply setclientid: %d\n", status);
4277 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278}
4279
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004280/**
4281 * nfs4_proc_setclientid_confirm - Confirm client ID
4282 * @clp: state data structure
4283 * @res: result of a previous SETCLIENTID
4284 * @cred: RPC credential to use for this call
4285 *
4286 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4287 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004288int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004289 struct nfs4_setclientid_res *arg,
4290 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 struct nfs_fsinfo fsinfo;
4293 struct rpc_message msg = {
4294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004295 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004297 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 };
4299 unsigned long now;
4300 int status;
4301
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004302 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4304 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004306 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (status == 0) {
4308 spin_lock(&clp->cl_lock);
4309 clp->cl_lease_time = fsinfo.lease_time * HZ;
4310 clp->cl_last_renewal = now;
4311 spin_unlock(&clp->cl_lock);
4312 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004313 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
Trond Myklebustfe650402006-01-03 09:55:18 +01004317struct nfs4_delegreturndata {
4318 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004319 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004320 struct nfs_fh fh;
4321 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004322 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004323 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004324 int rpc_status;
4325};
4326
Trond Myklebustfe650402006-01-03 09:55:18 +01004327static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4328{
4329 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004330
Trond Myklebust14516c32010-07-31 14:29:06 -04004331 if (!nfs4_sequence_done(task, &data->res.seq_res))
4332 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004333
Ricardo Labiaga79708862009-12-07 09:23:21 -05004334 switch (task->tk_status) {
4335 case -NFS4ERR_STALE_STATEID:
4336 case -NFS4ERR_EXPIRED:
4337 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004338 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004339 break;
4340 default:
4341 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4342 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004343 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004344 return;
4345 }
4346 }
4347 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004348}
4349
4350static void nfs4_delegreturn_release(void *calldata)
4351{
Trond Myklebustfe650402006-01-03 09:55:18 +01004352 kfree(calldata);
4353}
4354
Andy Adamson938e1012009-04-01 09:22:28 -04004355#if defined(CONFIG_NFS_V4_1)
4356static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4357{
4358 struct nfs4_delegreturndata *d_data;
4359
4360 d_data = (struct nfs4_delegreturndata *)data;
4361
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004362 nfs4_setup_sequence(d_data->res.server,
4363 &d_data->args.seq_args,
4364 &d_data->res.seq_res,
4365 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004366}
4367#endif /* CONFIG_NFS_V4_1 */
4368
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004369static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004370#if defined(CONFIG_NFS_V4_1)
4371 .rpc_call_prepare = nfs4_delegreturn_prepare,
4372#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004373 .rpc_call_done = nfs4_delegreturn_done,
4374 .rpc_release = nfs4_delegreturn_release,
4375};
4376
Trond Myklebuste6f81072008-01-24 18:14:34 -05004377static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004378{
4379 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004380 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004381 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004382 struct rpc_message msg = {
4383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4384 .rpc_cred = cred,
4385 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004386 struct rpc_task_setup task_setup_data = {
4387 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004388 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004389 .callback_ops = &nfs4_delegreturn_ops,
4390 .flags = RPC_TASK_ASYNC,
4391 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004392 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004393
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004394 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004395 if (data == NULL)
4396 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004397 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004398 data->args.fhandle = &data->fh;
4399 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004400 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004401 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004402 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004403 data->res.fattr = &data->fattr;
4404 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004405 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004406 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004407 data->rpc_status = 0;
4408
Trond Myklebustc970aa82007-07-14 15:39:59 -04004409 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004410 msg.rpc_argp = &data->args;
4411 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004412 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004413 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004414 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004415 if (!issync)
4416 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004417 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004418 if (status != 0)
4419 goto out;
4420 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004421 if (status == 0)
4422 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4423 else
4424 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004425out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004426 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Trond Myklebuste6f81072008-01-24 18:14:34 -05004430int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
4432 struct nfs_server *server = NFS_SERVER(inode);
4433 struct nfs4_exception exception = { };
4434 int err;
4435 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004436 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 switch (err) {
4438 case -NFS4ERR_STALE_STATEID:
4439 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 case 0:
4441 return 0;
4442 }
4443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4445 return err;
4446}
4447
4448#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4449#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4450
4451/*
4452 * sleep, with exponential backoff, and retry the LOCK operation.
4453 */
4454static unsigned long
4455nfs4_set_lock_task_retry(unsigned long timeout)
4456{
Jeff Laytond3103102011-12-01 22:44:39 +01004457 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 timeout <<= 1;
4459 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4460 return NFS4_LOCK_MAXTIMEOUT;
4461 return timeout;
4462}
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4465{
4466 struct inode *inode = state->inode;
4467 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004468 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004469 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004471 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004473 struct nfs_lockt_res res = {
4474 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 };
4476 struct rpc_message msg = {
4477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4478 .rpc_argp = &arg,
4479 .rpc_resp = &res,
4480 .rpc_cred = state->owner->so_cred,
4481 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct nfs4_lock_state *lsp;
4483 int status;
4484
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004485 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004486 status = nfs4_set_lock_state(state, request);
4487 if (status != 0)
4488 goto out;
4489 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004490 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004491 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004493 switch (status) {
4494 case 0:
4495 request->fl_type = F_UNLCK;
4496 break;
4497 case -NFS4ERR_DENIED:
4498 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004500 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004501out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return status;
4503}
4504
4505static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4506{
4507 struct nfs4_exception exception = { };
4508 int err;
4509
4510 do {
4511 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4512 _nfs4_proc_getlk(state, cmd, request),
4513 &exception);
4514 } while (exception.retry);
4515 return err;
4516}
4517
4518static int do_vfs_lock(struct file *file, struct file_lock *fl)
4519{
4520 int res = 0;
4521 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4522 case FL_POSIX:
4523 res = posix_lock_file_wait(file, fl);
4524 break;
4525 case FL_FLOCK:
4526 res = flock_lock_file_wait(file, fl);
4527 break;
4528 default:
4529 BUG();
4530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return res;
4532}
4533
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004534struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004535 struct nfs_locku_args arg;
4536 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537 struct nfs4_lock_state *lsp;
4538 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004539 struct file_lock fl;
4540 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004541 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004542};
4543
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004544static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4545 struct nfs_open_context *ctx,
4546 struct nfs4_lock_state *lsp,
4547 struct nfs_seqid *seqid)
4548{
4549 struct nfs4_unlockdata *p;
4550 struct inode *inode = lsp->ls_state->inode;
4551
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004552 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004553 if (p == NULL)
4554 return NULL;
4555 p->arg.fh = NFS_FH(inode);
4556 p->arg.fl = &p->fl;
4557 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004558 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004559 p->arg.stateid = &lsp->ls_stateid;
4560 p->lsp = lsp;
4561 atomic_inc(&lsp->ls_count);
4562 /* Ensure we don't close file until we're done freeing locks! */
4563 p->ctx = get_nfs_open_context(ctx);
4564 memcpy(&p->fl, fl, sizeof(p->fl));
4565 p->server = NFS_SERVER(inode);
4566 return p;
4567}
4568
Trond Myklebust06f814a2006-01-03 09:55:07 +01004569static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004570{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004571 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004572 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004573 nfs4_put_lock_state(calldata->lsp);
4574 put_nfs_open_context(calldata->ctx);
4575 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004576}
4577
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004578static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004579{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004580 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004581
Trond Myklebust14516c32010-07-31 14:29:06 -04004582 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4583 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004584 switch (task->tk_status) {
4585 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004586 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4587 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004588 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004589 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004590 case -NFS4ERR_BAD_STATEID:
4591 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004592 case -NFS4ERR_STALE_STATEID:
4593 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004594 break;
4595 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004596 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004597 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004598 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004599 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600}
4601
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004602static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004603{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004604 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004607 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004608 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004609 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004610 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004612 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004613 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004614 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004615 &calldata->res.seq_res,
4616 task) != 0)
4617 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004618 return;
4619out_no_action:
4620 task->tk_action = NULL;
4621out_wait:
4622 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004625static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004626 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004627 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004628 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004629};
4630
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4632 struct nfs_open_context *ctx,
4633 struct nfs4_lock_state *lsp,
4634 struct nfs_seqid *seqid)
4635{
4636 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004637 struct rpc_message msg = {
4638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4639 .rpc_cred = ctx->cred,
4640 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004641 struct rpc_task_setup task_setup_data = {
4642 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004643 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004644 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004645 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004646 .flags = RPC_TASK_ASYNC,
4647 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004648
Frank Filz137d6ac2007-07-09 15:32:29 -07004649 /* Ensure this is an unlock - when canceling a lock, the
4650 * canceled lock is passed in, and it won't be an unlock.
4651 */
4652 fl->fl_type = F_UNLCK;
4653
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4655 if (data == NULL) {
4656 nfs_free_seqid(seqid);
4657 return ERR_PTR(-ENOMEM);
4658 }
4659
Trond Myklebust9d12b212012-01-17 22:04:25 -05004660 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004661 msg.rpc_argp = &data->arg;
4662 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004663 task_setup_data.callback_data = data;
4664 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004665}
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4668{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004669 struct inode *inode = state->inode;
4670 struct nfs4_state_owner *sp = state->owner;
4671 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004672 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004673 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004674 struct rpc_task *task;
4675 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004676 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Trond Myklebust9b073572006-06-29 16:38:34 -04004678 status = nfs4_set_lock_state(state, request);
4679 /* Unlock _before_ we do the RPC call */
4680 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004681 /* Exclude nfs_delegation_claim_locks() */
4682 mutex_lock(&sp->so_delegreturn_mutex);
4683 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004684 down_read(&nfsi->rwsem);
4685 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4686 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004687 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004688 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004689 }
4690 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004691 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004692 if (status != 0)
4693 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004694 /* Is this a delegated lock? */
4695 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004696 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004697 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004698 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004699 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004700 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004701 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004702 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004703 status = PTR_ERR(task);
4704 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004705 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004706 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004707 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004708out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004709 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713struct nfs4_lockdata {
4714 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004715 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716 struct nfs4_lock_state *lsp;
4717 struct nfs_open_context *ctx;
4718 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004719 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720 int rpc_status;
4721 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004722 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723};
4724
4725static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004726 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4727 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004728{
4729 struct nfs4_lockdata *p;
4730 struct inode *inode = lsp->ls_state->inode;
4731 struct nfs_server *server = NFS_SERVER(inode);
4732
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004733 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734 if (p == NULL)
4735 return NULL;
4736
4737 p->arg.fh = NFS_FH(inode);
4738 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004739 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004740 if (p->arg.open_seqid == NULL)
4741 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004742 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004744 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004745 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004746 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004747 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004748 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004749 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004751 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752 atomic_inc(&lsp->ls_count);
4753 p->ctx = get_nfs_open_context(ctx);
4754 memcpy(&p->fl, fl, sizeof(p->fl));
4755 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004756out_free_seqid:
4757 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758out_free:
4759 kfree(p);
4760 return NULL;
4761}
4762
4763static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4764{
4765 struct nfs4_lockdata *data = calldata;
4766 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Harvey Harrison3110ff82008-05-02 13:42:44 -07004768 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004769 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004770 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004771 /* Do we need to do an open_to_lock_owner? */
4772 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004773 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004774 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004775 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004776 data->arg.open_stateid = &state->stateid;
4777 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004778 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004779 } else
4780 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004781 if (!nfs4_valid_open_stateid(state)) {
4782 data->rpc_status = -EBADF;
4783 task->tk_action = NULL;
4784 goto out_release_open_seqid;
4785 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004786 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004787 if (nfs4_setup_sequence(data->server,
4788 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004789 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004790 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004791 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004792out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004793 nfs_release_seqid(data->arg.open_seqid);
4794out_release_lock_seqid:
4795 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004796out_wait:
4797 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004798 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004799}
4800
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4802{
4803 struct nfs4_lockdata *data = calldata;
4804
Harvey Harrison3110ff82008-05-02 13:42:44 -07004805 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004806
Trond Myklebust14516c32010-07-31 14:29:06 -04004807 if (!nfs4_sequence_done(task, &data->res.seq_res))
4808 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004809
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004810 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 if (data->rpc_status == 0)
4813 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4814 else
4815 goto out;
4816 }
4817 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004818 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004819 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004820 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004821 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004823 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004824}
4825
4826static void nfs4_lock_release(void *calldata)
4827{
4828 struct nfs4_lockdata *data = calldata;
4829
Harvey Harrison3110ff82008-05-02 13:42:44 -07004830 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004831 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004832 if (data->cancelled != 0) {
4833 struct rpc_task *task;
4834 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4835 data->arg.lock_seqid);
4836 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004837 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004838 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004839 } else
4840 nfs_free_seqid(data->arg.lock_seqid);
4841 nfs4_put_lock_state(data->lsp);
4842 put_nfs_open_context(data->ctx);
4843 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004844 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845}
4846
4847static const struct rpc_call_ops nfs4_lock_ops = {
4848 .rpc_call_prepare = nfs4_lock_prepare,
4849 .rpc_call_done = nfs4_lock_done,
4850 .rpc_release = nfs4_lock_release,
4851};
4852
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004853static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4854{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004855 switch (error) {
4856 case -NFS4ERR_ADMIN_REVOKED:
4857 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004858 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004859 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004860 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004861 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004862 break;
4863 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004864 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004865 case -NFS4ERR_EXPIRED:
4866 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004867 };
4868}
4869
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004870static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004871{
4872 struct nfs4_lockdata *data;
4873 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004874 struct rpc_message msg = {
4875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4876 .rpc_cred = state->owner->so_cred,
4877 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004878 struct rpc_task_setup task_setup_data = {
4879 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004880 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004881 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004882 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004883 .flags = RPC_TASK_ASYNC,
4884 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004885 int ret;
4886
Harvey Harrison3110ff82008-05-02 13:42:44 -07004887 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004888 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004889 fl->fl_u.nfs4_fl.owner,
4890 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891 if (data == NULL)
4892 return -ENOMEM;
4893 if (IS_SETLKW(cmd))
4894 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004895 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004896 msg.rpc_argp = &data->arg;
4897 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004898 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004899 if (recovery_type > NFS_LOCK_NEW) {
4900 if (recovery_type == NFS_LOCK_RECLAIM)
4901 data->arg.reclaim = NFS_LOCK_RECLAIM;
4902 nfs4_set_sequence_privileged(&data->arg.seq_args);
4903 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004904 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004905 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004906 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004907 ret = nfs4_wait_for_completion_rpc_task(task);
4908 if (ret == 0) {
4909 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004910 if (ret)
4911 nfs4_handle_setlk_error(data->server, data->lsp,
4912 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004913 } else
4914 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004915 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004916 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004917 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918}
4919
4920static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4921{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004922 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004923 struct nfs4_exception exception = {
4924 .inode = state->inode,
4925 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004926 int err;
4927
4928 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004929 /* Cache the lock if possible... */
4930 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4931 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004932 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004933 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004934 break;
4935 nfs4_handle_exception(server, err, &exception);
4936 } while (exception.retry);
4937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4941{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004942 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004943 struct nfs4_exception exception = {
4944 .inode = state->inode,
4945 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004946 int err;
4947
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004948 err = nfs4_set_lock_state(state, request);
4949 if (err != 0)
4950 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004951 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4953 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004954 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004955 switch (err) {
4956 default:
4957 goto out;
4958 case -NFS4ERR_GRACE:
4959 case -NFS4ERR_DELAY:
4960 nfs4_handle_exception(server, err, &exception);
4961 err = 0;
4962 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004963 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004964out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004968#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004969/**
4970 * nfs41_check_expired_locks - possibly free a lock stateid
4971 *
4972 * @state: NFSv4 state for an inode
4973 *
4974 * Returns NFS_OK if recovery for this stateid is now finished.
4975 * Otherwise a negative NFS4ERR value is returned.
4976 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004977static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004978{
Chuck Levereb64cf92012-07-11 16:30:05 -04004979 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004980 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004981 struct nfs_server *server = NFS_SERVER(state->inode);
4982
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004983 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004984 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004985 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4986 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004987 /* Free the stateid unless the server
4988 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004989 if (status != -NFS4ERR_BAD_STATEID)
4990 nfs41_free_stateid(server,
4991 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004992 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004993 ret = status;
4994 }
4995 }
4996 };
4997
4998 return ret;
4999}
5000
5001static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5002{
5003 int status = NFS_OK;
5004
5005 if (test_bit(LK_STATE_IN_USE, &state->flags))
5006 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005007 if (status != NFS_OK)
5008 status = nfs4_lock_expired(state, request);
5009 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005010}
5011#endif
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5014{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005015 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005016 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005017 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005018 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005019 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005021 if ((fl_flags & FL_POSIX) &&
5022 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5023 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005024 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005025 status = nfs4_set_lock_state(state, request);
5026 if (status != 0)
5027 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005028 request->fl_flags |= FL_ACCESS;
5029 status = do_vfs_lock(request->fl_file, request);
5030 if (status < 0)
5031 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005032 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005033 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005034 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005035 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005036 request->fl_flags = fl_flags & ~FL_SLEEP;
5037 status = do_vfs_lock(request->fl_file, request);
5038 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005039 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5041 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005042 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005043 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005044 goto out;
5045 down_read(&nfsi->rwsem);
5046 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5047 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005048 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005049 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005050 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005051 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005052 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005053 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5054 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005055out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005056 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005057out:
5058 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 return status;
5060}
5061
5062static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5063{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005064 struct nfs4_exception exception = {
5065 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005066 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int err;
5069
5070 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005071 err = _nfs4_proc_setlk(state, cmd, request);
5072 if (err == -NFS4ERR_DENIED)
5073 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005075 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 } while (exception.retry);
5077 return err;
5078}
5079
5080static int
5081nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5082{
5083 struct nfs_open_context *ctx;
5084 struct nfs4_state *state;
5085 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5086 int status;
5087
5088 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005089 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 state = ctx->state;
5091
5092 if (request->fl_start < 0 || request->fl_end < 0)
5093 return -EINVAL;
5094
Trond Myklebustd9531262009-07-21 19:22:38 -04005095 if (IS_GETLK(cmd)) {
5096 if (state != NULL)
5097 return nfs4_proc_getlk(state, F_GETLK, request);
5098 return 0;
5099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5102 return -EINVAL;
5103
Trond Myklebustd9531262009-07-21 19:22:38 -04005104 if (request->fl_type == F_UNLCK) {
5105 if (state != NULL)
5106 return nfs4_proc_unlck(state, cmd, request);
5107 return 0;
5108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Trond Myklebustd9531262009-07-21 19:22:38 -04005110 if (state == NULL)
5111 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005112 /*
5113 * Don't rely on the VFS having checked the file open mode,
5114 * since it won't do this for flock() locks.
5115 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005116 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005117 case F_RDLCK:
5118 if (!(filp->f_mode & FMODE_READ))
5119 return -EBADF;
5120 break;
5121 case F_WRLCK:
5122 if (!(filp->f_mode & FMODE_WRITE))
5123 return -EBADF;
5124 }
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 do {
5127 status = nfs4_proc_setlk(state, cmd, request);
5128 if ((status != -EAGAIN) || IS_SETLK(cmd))
5129 break;
5130 timeout = nfs4_set_lock_task_retry(timeout);
5131 status = -ERESTARTSYS;
5132 if (signalled())
5133 break;
5134 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return status;
5136}
5137
Trond Myklebust888e6942005-11-04 15:38:11 -05005138int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5139{
5140 struct nfs_server *server = NFS_SERVER(state->inode);
5141 struct nfs4_exception exception = { };
5142 int err;
5143
5144 err = nfs4_set_lock_state(state, fl);
5145 if (err != 0)
5146 goto out;
5147 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005148 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005149 switch (err) {
5150 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005151 printk(KERN_ERR "NFS: %s: unhandled error "
5152 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005153 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005154 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005155 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005156 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005157 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005158 set_bit(NFS_DELEGATED_STATE, &state->flags);
5159 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005160 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005161 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005162 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005163 case -NFS4ERR_BADSESSION:
5164 case -NFS4ERR_BADSLOT:
5165 case -NFS4ERR_BAD_HIGH_SLOT:
5166 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5167 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005168 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005169 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005170 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005171 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005172 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005173 case -NFS4ERR_ADMIN_REVOKED:
5174 case -NFS4ERR_BAD_STATEID:
5175 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005176 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005177 err = 0;
5178 goto out;
5179 case -ENOMEM:
5180 case -NFS4ERR_DENIED:
5181 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5182 err = 0;
5183 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005184 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005185 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005186 err = nfs4_handle_exception(server, err, &exception);
5187 } while (exception.retry);
5188out:
5189 return err;
5190}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005191
Trond Myklebustcf470c32012-03-07 13:49:12 -05005192struct nfs_release_lockowner_data {
5193 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005194 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005195 struct nfs_release_lockowner_args args;
5196};
5197
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005198static void nfs4_release_lockowner_release(void *calldata)
5199{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005200 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005201 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005202 kfree(calldata);
5203}
5204
Trond Myklebust17280172012-03-11 13:11:00 -04005205static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005206 .rpc_release = nfs4_release_lockowner_release,
5207};
5208
Trond Myklebustcf470c32012-03-07 13:49:12 -05005209int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005210{
5211 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005212 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5215 };
5216
5217 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005218 return -EINVAL;
5219 data = kmalloc(sizeof(*data), GFP_NOFS);
5220 if (!data)
5221 return -ENOMEM;
5222 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005223 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005224 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5225 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5226 data->args.lock_owner.s_dev = server->s_dev;
5227 msg.rpc_argp = &data->args;
5228 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5229 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005230}
5231
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005232#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5233
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005234static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5235 const void *buf, size_t buflen,
5236 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005237{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005238 if (strcmp(key, "") != 0)
5239 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005240
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005241 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005242}
5243
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005244static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5245 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005246{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005247 if (strcmp(key, "") != 0)
5248 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005249
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005250 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005251}
5252
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005253static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5254 size_t list_len, const char *name,
5255 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005256{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005257 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005258
J. Bruce Fields096455a2006-03-20 23:23:42 -05005259 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5260 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005261
5262 if (list && len <= list_len)
5263 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005264 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005265}
5266
Andy Adamson533eb462011-06-13 18:25:56 -04005267/*
5268 * nfs_fhget will use either the mounted_on_fileid or the fileid
5269 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005270static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5271{
Andy Adamson533eb462011-06-13 18:25:56 -04005272 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5273 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5274 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005275 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005276 return;
5277
5278 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005279 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005280 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5281 fattr->nlink = 2;
5282}
5283
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005284static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5285 const struct qstr *name,
5286 struct nfs4_fs_locations *fs_locations,
5287 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005288{
5289 struct nfs_server *server = NFS_SERVER(dir);
5290 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005291 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005292 };
5293 struct nfs4_fs_locations_arg args = {
5294 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005295 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005296 .page = page,
5297 .bitmask = bitmask,
5298 };
Benny Halevy22958462009-04-01 09:22:02 -04005299 struct nfs4_fs_locations_res res = {
5300 .fs_locations = fs_locations,
5301 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5304 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005305 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005306 };
5307 int status;
5308
Harvey Harrison3110ff82008-05-02 13:42:44 -07005309 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005310
5311 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5312 * is not supported */
5313 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5314 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5315 else
5316 bitmask[0] |= FATTR4_WORD0_FILEID;
5317
Trond Myklebustc228fd32007-01-13 02:28:11 -05005318 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005319 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005320 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005321 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005322 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005323 return status;
5324}
5325
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005326int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5327 const struct qstr *name,
5328 struct nfs4_fs_locations *fs_locations,
5329 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005330{
5331 struct nfs4_exception exception = { };
5332 int err;
5333 do {
5334 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005335 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005336 &exception);
5337 } while (exception.retry);
5338 return err;
5339}
5340
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005341static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5342{
5343 int status;
5344 struct nfs4_secinfo_arg args = {
5345 .dir_fh = NFS_FH(dir),
5346 .name = name,
5347 };
5348 struct nfs4_secinfo_res res = {
5349 .flavors = flavors,
5350 };
5351 struct rpc_message msg = {
5352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5353 .rpc_argp = &args,
5354 .rpc_resp = &res,
5355 };
5356
5357 dprintk("NFS call secinfo %s\n", name->name);
5358 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5359 dprintk("NFS reply secinfo: %d\n", status);
5360 return status;
5361}
5362
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005363int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5364 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005365{
5366 struct nfs4_exception exception = { };
5367 int err;
5368 do {
5369 err = nfs4_handle_exception(NFS_SERVER(dir),
5370 _nfs4_proc_secinfo(dir, name, flavors),
5371 &exception);
5372 } while (exception.retry);
5373 return err;
5374}
5375
Andy Adamson557134a2009-04-01 09:21:53 -04005376#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005377/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005378 * Check the exchange flags returned by the server for invalid flags, having
5379 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5380 * DS flags set.
5381 */
5382static int nfs4_check_cl_exchange_flags(u32 flags)
5383{
5384 if (flags & ~EXCHGID4_FLAG_MASK_R)
5385 goto out_inval;
5386 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5387 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5388 goto out_inval;
5389 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5390 goto out_inval;
5391 return NFS_OK;
5392out_inval:
5393 return -NFS4ERR_INVAL;
5394}
5395
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005396static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005397nfs41_same_server_scope(struct nfs41_server_scope *a,
5398 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005399{
5400 if (a->server_scope_sz == b->server_scope_sz &&
5401 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5402 return true;
5403
5404 return false;
5405}
5406
Andy Adamson357f54d2010-12-14 10:11:57 -05005407/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005408 * nfs4_proc_bind_conn_to_session()
5409 *
5410 * The 4.1 client currently uses the same TCP connection for the
5411 * fore and backchannel.
5412 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005413int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005414{
5415 int status;
5416 struct nfs41_bind_conn_to_session_res res;
5417 struct rpc_message msg = {
5418 .rpc_proc =
5419 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5420 .rpc_argp = clp,
5421 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005422 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005423 };
5424
5425 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005426
5427 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5428 if (unlikely(res.session == NULL)) {
5429 status = -ENOMEM;
5430 goto out;
5431 }
5432
5433 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5434 if (status == 0) {
5435 if (memcmp(res.session->sess_id.data,
5436 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5437 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5438 status = -EIO;
5439 goto out_session;
5440 }
5441 if (res.dir != NFS4_CDFS4_BOTH) {
5442 dprintk("NFS: %s: Unexpected direction from server\n",
5443 __func__);
5444 status = -EIO;
5445 goto out_session;
5446 }
5447 if (res.use_conn_in_rdma_mode) {
5448 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5449 __func__);
5450 status = -EIO;
5451 goto out_session;
5452 }
5453 }
5454out_session:
5455 kfree(res.session);
5456out:
5457 dprintk("<-- %s status= %d\n", __func__, status);
5458 return status;
5459}
5460
5461/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005462 * nfs4_proc_exchange_id()
5463 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005464 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5465 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005466 * Since the clientid has expired, all compounds using sessions
5467 * associated with the stale clientid will be returning
5468 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5469 * be in some phase of session reset.
5470 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005471int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005472{
5473 nfs4_verifier verifier;
5474 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005475 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005476 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005477 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005478 };
5479 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005480 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005481 };
5482 int status;
5483 struct rpc_message msg = {
5484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5485 .rpc_argp = &args,
5486 .rpc_resp = &res,
5487 .rpc_cred = cred,
5488 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489
Chuck Leverf0920752012-05-21 22:45:41 -04005490 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005491 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5492 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5494 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5495 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005496
Chuck Leveracdeb692012-05-21 22:46:16 -04005497 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005498 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005499 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005500 status = -ENOMEM;
5501 goto out;
5502 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005503
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005504 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005505 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005506 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005507 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005508 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005509 }
5510
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005511 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005512 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005513 status = -ENOMEM;
5514 goto out_server_scope;
5515 }
5516
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005517 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005518 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005519 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005520
Chuck Lever177313f2012-05-21 22:44:58 -04005521 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005522 clp->cl_clientid = res.clientid;
5523 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5524 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5525 clp->cl_seqid = res.seqid;
5526
Chuck Leveracdeb692012-05-21 22:46:16 -04005527 kfree(clp->cl_serverowner);
5528 clp->cl_serverowner = res.server_owner;
5529 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005530
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005531 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005532 kfree(clp->cl_implid);
5533 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005534
Chuck Lever177313f2012-05-21 22:44:58 -04005535 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005536 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005537 res.server_scope)) {
5538 dprintk("%s: server_scope mismatch detected\n",
5539 __func__);
5540 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005541 kfree(clp->cl_serverscope);
5542 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005543 }
5544
Chuck Lever177313f2012-05-21 22:44:58 -04005545 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005546 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005547 goto out;
5548 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005549 } else
5550 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005551
Chuck Leveracdeb692012-05-21 22:46:16 -04005552out_server_owner:
5553 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005554out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005555 kfree(res.server_scope);
5556out:
Chuck Lever177313f2012-05-21 22:44:58 -04005557 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005558 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005559 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005560 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005561 clp->cl_implid->date.seconds,
5562 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005563 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005564 return status;
5565}
5566
Trond Myklebust66245532012-05-25 17:18:09 -04005567static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5568 struct rpc_cred *cred)
5569{
5570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5572 .rpc_argp = clp,
5573 .rpc_cred = cred,
5574 };
5575 int status;
5576
5577 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5578 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005579 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005580 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5581 return status;
5582}
5583
5584static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5585 struct rpc_cred *cred)
5586{
5587 unsigned int loop;
5588 int ret;
5589
5590 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5591 ret = _nfs4_proc_destroy_clientid(clp, cred);
5592 switch (ret) {
5593 case -NFS4ERR_DELAY:
5594 case -NFS4ERR_CLIENTID_BUSY:
5595 ssleep(1);
5596 break;
5597 default:
5598 return ret;
5599 }
5600 }
5601 return 0;
5602}
5603
5604int nfs4_destroy_clientid(struct nfs_client *clp)
5605{
5606 struct rpc_cred *cred;
5607 int ret = 0;
5608
5609 if (clp->cl_mvops->minor_version < 1)
5610 goto out;
5611 if (clp->cl_exchange_flags == 0)
5612 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005613 if (clp->cl_preserve_clid)
5614 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005615 cred = nfs4_get_exchange_id_cred(clp);
5616 ret = nfs4_proc_destroy_clientid(clp, cred);
5617 if (cred)
5618 put_rpccred(cred);
5619 switch (ret) {
5620 case 0:
5621 case -NFS4ERR_STALE_CLIENTID:
5622 clp->cl_exchange_flags = 0;
5623 }
5624out:
5625 return ret;
5626}
5627
Andy Adamson2050f0c2009-04-01 09:22:30 -04005628struct nfs4_get_lease_time_data {
5629 struct nfs4_get_lease_time_args *args;
5630 struct nfs4_get_lease_time_res *res;
5631 struct nfs_client *clp;
5632};
5633
5634static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5635 void *calldata)
5636{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005637 struct nfs4_get_lease_time_data *data =
5638 (struct nfs4_get_lease_time_data *)calldata;
5639
5640 dprintk("--> %s\n", __func__);
5641 /* just setup sequence, do not trigger session recovery
5642 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005643 nfs41_setup_sequence(data->clp->cl_session,
5644 &data->args->la_seq_args,
5645 &data->res->lr_seq_res,
5646 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005647 dprintk("<-- %s\n", __func__);
5648}
5649
5650/*
5651 * Called from nfs4_state_manager thread for session setup, so don't recover
5652 * from sequence operation or clientid errors.
5653 */
5654static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5655{
5656 struct nfs4_get_lease_time_data *data =
5657 (struct nfs4_get_lease_time_data *)calldata;
5658
5659 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005660 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5661 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005662 switch (task->tk_status) {
5663 case -NFS4ERR_DELAY:
5664 case -NFS4ERR_GRACE:
5665 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5666 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5667 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005668 /* fall through */
5669 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005670 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005671 return;
5672 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005673 dprintk("<-- %s\n", __func__);
5674}
5675
Trond Myklebust17280172012-03-11 13:11:00 -04005676static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005677 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5678 .rpc_call_done = nfs4_get_lease_time_done,
5679};
5680
5681int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5682{
5683 struct rpc_task *task;
5684 struct nfs4_get_lease_time_args args;
5685 struct nfs4_get_lease_time_res res = {
5686 .lr_fsinfo = fsinfo,
5687 };
5688 struct nfs4_get_lease_time_data data = {
5689 .args = &args,
5690 .res = &res,
5691 .clp = clp,
5692 };
5693 struct rpc_message msg = {
5694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5695 .rpc_argp = &args,
5696 .rpc_resp = &res,
5697 };
5698 struct rpc_task_setup task_setup = {
5699 .rpc_client = clp->cl_rpcclient,
5700 .rpc_message = &msg,
5701 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005702 .callback_data = &data,
5703 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005704 };
5705 int status;
5706
Trond Myklebust9d12b212012-01-17 22:04:25 -05005707 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005708 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005709 dprintk("--> %s\n", __func__);
5710 task = rpc_run_task(&task_setup);
5711
5712 if (IS_ERR(task))
5713 status = PTR_ERR(task);
5714 else {
5715 status = task->tk_status;
5716 rpc_put_task(task);
5717 }
5718 dprintk("<-- %s return %d\n", __func__, status);
5719
5720 return status;
5721}
5722
Andy Adamsonfc931582009-04-01 09:22:31 -04005723/*
5724 * Initialize the values to be used by the client in CREATE_SESSION
5725 * If nfs4_init_session set the fore channel request and response sizes,
5726 * use them.
5727 *
5728 * Set the back channel max_resp_sz_cached to zero to force the client to
5729 * always set csa_cachethis to FALSE because the current implementation
5730 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5731 */
5732static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5733{
5734 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005735 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5736 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005737
5738 if (mxrqst_sz == 0)
5739 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5740 if (mxresp_sz == 0)
5741 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5742 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005743 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5744 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005745 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005746 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005747
5748 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005749 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005750 __func__,
5751 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005752 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005753
5754 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005755 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5756 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5757 args->bc_attrs.max_resp_sz_cached = 0;
5758 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5759 args->bc_attrs.max_reqs = 1;
5760
5761 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5762 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5763 __func__,
5764 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5765 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5766 args->bc_attrs.max_reqs);
5767}
5768
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005769static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005770{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005771 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5772 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5773
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774 if (rcvd->max_resp_sz > sent->max_resp_sz)
5775 return -EINVAL;
5776 /*
5777 * Our requested max_ops is the minimum we need; we're not
5778 * prepared to break up compounds into smaller pieces than that.
5779 * So, no point even trying to continue if the server won't
5780 * cooperate:
5781 */
5782 if (rcvd->max_ops < sent->max_ops)
5783 return -EINVAL;
5784 if (rcvd->max_reqs == 0)
5785 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005786 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5787 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005788 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005789}
5790
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005791static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5792{
5793 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5794 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005795
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005796 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5797 return -EINVAL;
5798 if (rcvd->max_resp_sz < sent->max_resp_sz)
5799 return -EINVAL;
5800 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5801 return -EINVAL;
5802 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005803 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005804 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005805 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005806 return -EINVAL;
5807 return 0;
5808}
Andy Adamson8d353012009-04-01 09:22:32 -04005809
Andy Adamson8d353012009-04-01 09:22:32 -04005810static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5811 struct nfs4_session *session)
5812{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005813 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005814
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005815 ret = nfs4_verify_fore_channel_attrs(args, session);
5816 if (ret)
5817 return ret;
5818 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005819}
5820
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005821static int _nfs4_proc_create_session(struct nfs_client *clp,
5822 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005823{
5824 struct nfs4_session *session = clp->cl_session;
5825 struct nfs41_create_session_args args = {
5826 .client = clp,
5827 .cb_program = NFS4_CALLBACK,
5828 };
5829 struct nfs41_create_session_res res = {
5830 .client = clp,
5831 };
5832 struct rpc_message msg = {
5833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5834 .rpc_argp = &args,
5835 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005836 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005837 };
5838 int status;
5839
5840 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005841 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005842
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005843 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005844
Trond Myklebust43095d32012-11-20 11:13:12 -05005845 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005846 /* Verify the session's negotiated channel_attrs values */
5847 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005848 /* Increment the clientid slot sequence id */
5849 clp->cl_seqid++;
5850 }
5851
5852 return status;
5853}
5854
5855/*
5856 * Issues a CREATE_SESSION operation to the server.
5857 * It is the responsibility of the caller to verify the session is
5858 * expired before calling this routine.
5859 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005860int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005861{
5862 int status;
5863 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 struct nfs4_session *session = clp->cl_session;
5865
5866 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5867
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005868 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005869 if (status)
5870 goto out;
5871
Andy Adamsonaacd5532011-11-09 13:58:21 -05005872 /* Init or reset the session slot tables */
5873 status = nfs4_setup_session_slot_tables(session);
5874 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005875 if (status)
5876 goto out;
5877
5878 ptr = (unsigned *)&session->sess_id.data[0];
5879 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5880 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005881out:
5882 dprintk("<-- %s\n", __func__);
5883 return status;
5884}
5885
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005886/*
5887 * Issue the over-the-wire RPC DESTROY_SESSION.
5888 * The caller must serialize access to this routine.
5889 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005890int nfs4_proc_destroy_session(struct nfs4_session *session,
5891 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005892{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5895 .rpc_argp = session,
5896 .rpc_cred = cred,
5897 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005898 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005899
5900 dprintk("--> nfs4_proc_destroy_session\n");
5901
5902 /* session is still being setup */
5903 if (session->clp->cl_cons_state != NFS_CS_READY)
5904 return status;
5905
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005906 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005907
5908 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005909 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005910 "Session has been destroyed regardless...\n", status);
5911
5912 dprintk("<-- nfs4_proc_destroy_session\n");
5913 return status;
5914}
5915
Trond Myklebust7b38c362012-05-23 13:23:31 -04005916/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005917 * Renew the cl_session lease.
5918 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005919struct nfs4_sequence_data {
5920 struct nfs_client *clp;
5921 struct nfs4_sequence_args args;
5922 struct nfs4_sequence_res res;
5923};
5924
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005925static void nfs41_sequence_release(void *data)
5926{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005927 struct nfs4_sequence_data *calldata = data;
5928 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005929
Alexandros Batsakis71358402010-02-05 03:45:05 -08005930 if (atomic_read(&clp->cl_count) > 1)
5931 nfs4_schedule_state_renewal(clp);
5932 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005933 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005934}
5935
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005936static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5937{
5938 switch(task->tk_status) {
5939 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005940 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5941 return -EAGAIN;
5942 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005943 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005944 }
5945 return 0;
5946}
5947
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005948static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005949{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005950 struct nfs4_sequence_data *calldata = data;
5951 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005952
Trond Myklebust14516c32010-07-31 14:29:06 -04005953 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5954 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005955
5956 if (task->tk_status < 0) {
5957 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005958 if (atomic_read(&clp->cl_count) == 1)
5959 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005960
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005961 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5962 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005963 return;
5964 }
5965 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005967out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968 dprintk("<-- %s\n", __func__);
5969}
5970
5971static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5972{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005973 struct nfs4_sequence_data *calldata = data;
5974 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005975 struct nfs4_sequence_args *args;
5976 struct nfs4_sequence_res *res;
5977
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005978 args = task->tk_msg.rpc_argp;
5979 res = task->tk_msg.rpc_resp;
5980
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005981 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982}
5983
5984static const struct rpc_call_ops nfs41_sequence_ops = {
5985 .rpc_call_done = nfs41_sequence_call_done,
5986 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005987 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005988};
5989
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005990static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5991 struct rpc_cred *cred,
5992 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005993{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005994 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005995 struct rpc_message msg = {
5996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5997 .rpc_cred = cred,
5998 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005999 struct rpc_task_setup task_setup_data = {
6000 .rpc_client = clp->cl_rpcclient,
6001 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006002 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006003 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6004 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006005
Alexandros Batsakis71358402010-02-05 03:45:05 -08006006 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006007 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006008 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006009 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006010 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006011 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006012 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006013 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006014 if (is_privileged)
6015 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006016 msg.rpc_argp = &calldata->args;
6017 msg.rpc_resp = &calldata->res;
6018 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006019 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006020
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006021 return rpc_run_task(&task_setup_data);
6022}
6023
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006024static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006025{
6026 struct rpc_task *task;
6027 int ret = 0;
6028
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006029 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6030 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006031 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006032 if (IS_ERR(task))
6033 ret = PTR_ERR(task);
6034 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006035 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006036 dprintk("<-- %s status=%d\n", __func__, ret);
6037 return ret;
6038}
6039
6040static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6041{
6042 struct rpc_task *task;
6043 int ret;
6044
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006045 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006046 if (IS_ERR(task)) {
6047 ret = PTR_ERR(task);
6048 goto out;
6049 }
6050 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006051 if (!ret) {
6052 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6053
6054 if (task->tk_status == 0)
6055 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006056 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006057 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006058 rpc_put_task(task);
6059out:
6060 dprintk("<-- %s status=%d\n", __func__, ret);
6061 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006062}
6063
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006064struct nfs4_reclaim_complete_data {
6065 struct nfs_client *clp;
6066 struct nfs41_reclaim_complete_args arg;
6067 struct nfs41_reclaim_complete_res res;
6068};
6069
6070static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6071{
6072 struct nfs4_reclaim_complete_data *calldata = data;
6073
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006074 nfs41_setup_sequence(calldata->clp->cl_session,
6075 &calldata->arg.seq_args,
6076 &calldata->res.seq_res,
6077 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006078}
6079
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006080static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6081{
6082 switch(task->tk_status) {
6083 case 0:
6084 case -NFS4ERR_COMPLETE_ALREADY:
6085 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6086 break;
6087 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006088 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006089 /* fall through */
6090 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006091 return -EAGAIN;
6092 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006093 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006094 }
6095 return 0;
6096}
6097
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006098static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6099{
6100 struct nfs4_reclaim_complete_data *calldata = data;
6101 struct nfs_client *clp = calldata->clp;
6102 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6103
6104 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006105 if (!nfs41_sequence_done(task, res))
6106 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006107
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006108 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6109 rpc_restart_call_prepare(task);
6110 return;
6111 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006112 dprintk("<-- %s\n", __func__);
6113}
6114
6115static void nfs4_free_reclaim_complete_data(void *data)
6116{
6117 struct nfs4_reclaim_complete_data *calldata = data;
6118
6119 kfree(calldata);
6120}
6121
6122static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6123 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6124 .rpc_call_done = nfs4_reclaim_complete_done,
6125 .rpc_release = nfs4_free_reclaim_complete_data,
6126};
6127
6128/*
6129 * Issue a global reclaim complete.
6130 */
6131static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6132{
6133 struct nfs4_reclaim_complete_data *calldata;
6134 struct rpc_task *task;
6135 struct rpc_message msg = {
6136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6137 };
6138 struct rpc_task_setup task_setup_data = {
6139 .rpc_client = clp->cl_rpcclient,
6140 .rpc_message = &msg,
6141 .callback_ops = &nfs4_reclaim_complete_call_ops,
6142 .flags = RPC_TASK_ASYNC,
6143 };
6144 int status = -ENOMEM;
6145
6146 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006147 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006148 if (calldata == NULL)
6149 goto out;
6150 calldata->clp = clp;
6151 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006152
Trond Myklebust9d12b212012-01-17 22:04:25 -05006153 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006154 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006155 msg.rpc_argp = &calldata->arg;
6156 msg.rpc_resp = &calldata->res;
6157 task_setup_data.callback_data = calldata;
6158 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006159 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006160 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006161 goto out;
6162 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006163 status = nfs4_wait_for_completion_rpc_task(task);
6164 if (status == 0)
6165 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006166 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006167 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006168out:
6169 dprintk("<-- %s status=%d\n", __func__, status);
6170 return status;
6171}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172
6173static void
6174nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6175{
6176 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006177 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006178 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179
6180 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006181 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6182 * right now covering the LAYOUTGET we are about to send.
6183 * However, that is not so catastrophic, and there seems
6184 * to be no way to prevent it completely.
6185 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006186 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006187 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006188 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006189 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6190 NFS_I(lgp->args.inode)->layout,
6191 lgp->args.ctx->state)) {
6192 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006193 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006194}
6195
6196static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6197{
6198 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006199 struct inode *inode = lgp->args.inode;
6200 struct nfs_server *server = NFS_SERVER(inode);
6201 struct pnfs_layout_hdr *lo;
6202 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006203 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006204
6205 dprintk("--> %s\n", __func__);
6206
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006207 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006208 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006209
6210 switch (task->tk_status) {
6211 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006212 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006213 case -NFS4ERR_LAYOUTTRYLATER:
6214 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006215 timeo = rpc_get_timeout(task->tk_client);
6216 giveup = lgp->args.timestamp + timeo;
6217 if (time_after(giveup, jiffies))
6218 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006219 break;
6220 case -NFS4ERR_EXPIRED:
6221 case -NFS4ERR_BAD_STATEID:
6222 spin_lock(&inode->i_lock);
6223 lo = NFS_I(inode)->layout;
6224 if (!lo || list_empty(&lo->plh_segs)) {
6225 spin_unlock(&inode->i_lock);
6226 /* If the open stateid was bad, then recover it. */
6227 state = lgp->args.ctx->state;
6228 } else {
6229 LIST_HEAD(head);
6230
6231 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6232 spin_unlock(&inode->i_lock);
6233 /* Mark the bad layout state as invalid, then
6234 * retry using the open stateid. */
6235 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006236 }
6237 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006238 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6239 rpc_restart_call_prepare(task);
6240out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006241 dprintk("<-- %s\n", __func__);
6242}
6243
Idan Kedar85541162012-08-02 11:47:10 +03006244static size_t max_response_pages(struct nfs_server *server)
6245{
6246 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6247 return nfs_page_array_len(0, max_resp_sz);
6248}
6249
6250static void nfs4_free_pages(struct page **pages, size_t size)
6251{
6252 int i;
6253
6254 if (!pages)
6255 return;
6256
6257 for (i = 0; i < size; i++) {
6258 if (!pages[i])
6259 break;
6260 __free_page(pages[i]);
6261 }
6262 kfree(pages);
6263}
6264
6265static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6266{
6267 struct page **pages;
6268 int i;
6269
6270 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6271 if (!pages) {
6272 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6273 return NULL;
6274 }
6275
6276 for (i = 0; i < size; i++) {
6277 pages[i] = alloc_page(gfp_flags);
6278 if (!pages[i]) {
6279 dprintk("%s: failed to allocate page\n", __func__);
6280 nfs4_free_pages(pages, size);
6281 return NULL;
6282 }
6283 }
6284
6285 return pages;
6286}
6287
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006288static void nfs4_layoutget_release(void *calldata)
6289{
6290 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006291 struct inode *inode = lgp->args.inode;
6292 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006293 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294
6295 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006296 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006297 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006298 put_nfs_open_context(lgp->args.ctx);
6299 kfree(calldata);
6300 dprintk("<-- %s\n", __func__);
6301}
6302
6303static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6304 .rpc_call_prepare = nfs4_layoutget_prepare,
6305 .rpc_call_done = nfs4_layoutget_done,
6306 .rpc_release = nfs4_layoutget_release,
6307};
6308
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006309struct pnfs_layout_segment *
6310nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006311{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006312 struct inode *inode = lgp->args.inode;
6313 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006314 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006315 struct rpc_task *task;
6316 struct rpc_message msg = {
6317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6318 .rpc_argp = &lgp->args,
6319 .rpc_resp = &lgp->res,
6320 };
6321 struct rpc_task_setup task_setup_data = {
6322 .rpc_client = server->client,
6323 .rpc_message = &msg,
6324 .callback_ops = &nfs4_layoutget_call_ops,
6325 .callback_data = lgp,
6326 .flags = RPC_TASK_ASYNC,
6327 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006328 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006329 int status = 0;
6330
6331 dprintk("--> %s\n", __func__);
6332
Idan Kedar85541162012-08-02 11:47:10 +03006333 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6334 if (!lgp->args.layout.pages) {
6335 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006336 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006337 }
6338 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006339 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006340
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006341 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006343 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006344
6345 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6346 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6347
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006348 task = rpc_run_task(&task_setup_data);
6349 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006350 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006351 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006352 if (status == 0)
6353 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006354 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6355 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006356 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006357 rpc_put_task(task);
6358 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006359 if (status)
6360 return ERR_PTR(status);
6361 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006362}
6363
Benny Halevycbe82602011-05-22 19:52:37 +03006364static void
6365nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6366{
6367 struct nfs4_layoutreturn *lrp = calldata;
6368
6369 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006370 nfs41_setup_sequence(lrp->clp->cl_session,
6371 &lrp->args.seq_args,
6372 &lrp->res.seq_res,
6373 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006374}
6375
6376static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6377{
6378 struct nfs4_layoutreturn *lrp = calldata;
6379 struct nfs_server *server;
6380
6381 dprintk("--> %s\n", __func__);
6382
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006383 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006384 return;
6385
6386 server = NFS_SERVER(lrp->args.inode);
6387 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006388 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006389 return;
6390 }
Benny Halevycbe82602011-05-22 19:52:37 +03006391 dprintk("<-- %s\n", __func__);
6392}
6393
6394static void nfs4_layoutreturn_release(void *calldata)
6395{
6396 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006397 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006398
6399 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006400 spin_lock(&lo->plh_inode->i_lock);
6401 if (lrp->res.lrs_present)
6402 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6403 lo->plh_block_lgets--;
6404 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006405 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006406 kfree(calldata);
6407 dprintk("<-- %s\n", __func__);
6408}
6409
6410static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6411 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6412 .rpc_call_done = nfs4_layoutreturn_done,
6413 .rpc_release = nfs4_layoutreturn_release,
6414};
6415
6416int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6417{
6418 struct rpc_task *task;
6419 struct rpc_message msg = {
6420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6421 .rpc_argp = &lrp->args,
6422 .rpc_resp = &lrp->res,
6423 };
6424 struct rpc_task_setup task_setup_data = {
6425 .rpc_client = lrp->clp->cl_rpcclient,
6426 .rpc_message = &msg,
6427 .callback_ops = &nfs4_layoutreturn_call_ops,
6428 .callback_data = lrp,
6429 };
6430 int status;
6431
6432 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006433 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006434 task = rpc_run_task(&task_setup_data);
6435 if (IS_ERR(task))
6436 return PTR_ERR(task);
6437 status = task->tk_status;
6438 dprintk("<-- %s status=%d\n", __func__, status);
6439 rpc_put_task(task);
6440 return status;
6441}
6442
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006443/*
6444 * Retrieve the list of Data Server devices from the MDS.
6445 */
6446static int _nfs4_getdevicelist(struct nfs_server *server,
6447 const struct nfs_fh *fh,
6448 struct pnfs_devicelist *devlist)
6449{
6450 struct nfs4_getdevicelist_args args = {
6451 .fh = fh,
6452 .layoutclass = server->pnfs_curr_ld->id,
6453 };
6454 struct nfs4_getdevicelist_res res = {
6455 .devlist = devlist,
6456 };
6457 struct rpc_message msg = {
6458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6459 .rpc_argp = &args,
6460 .rpc_resp = &res,
6461 };
6462 int status;
6463
6464 dprintk("--> %s\n", __func__);
6465 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6466 &res.seq_res, 0);
6467 dprintk("<-- %s status=%d\n", __func__, status);
6468 return status;
6469}
6470
6471int nfs4_proc_getdevicelist(struct nfs_server *server,
6472 const struct nfs_fh *fh,
6473 struct pnfs_devicelist *devlist)
6474{
6475 struct nfs4_exception exception = { };
6476 int err;
6477
6478 do {
6479 err = nfs4_handle_exception(server,
6480 _nfs4_getdevicelist(server, fh, devlist),
6481 &exception);
6482 } while (exception.retry);
6483
6484 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6485 err, devlist->num_devs);
6486
6487 return err;
6488}
6489EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6490
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006491static int
6492_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6493{
6494 struct nfs4_getdeviceinfo_args args = {
6495 .pdev = pdev,
6496 };
6497 struct nfs4_getdeviceinfo_res res = {
6498 .pdev = pdev,
6499 };
6500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6502 .rpc_argp = &args,
6503 .rpc_resp = &res,
6504 };
6505 int status;
6506
6507 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006508 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006509 dprintk("<-- %s status=%d\n", __func__, status);
6510
6511 return status;
6512}
6513
6514int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6515{
6516 struct nfs4_exception exception = { };
6517 int err;
6518
6519 do {
6520 err = nfs4_handle_exception(server,
6521 _nfs4_proc_getdeviceinfo(server, pdev),
6522 &exception);
6523 } while (exception.retry);
6524 return err;
6525}
6526EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6527
Andy Adamson863a3c62011-03-23 13:27:54 +00006528static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6529{
6530 struct nfs4_layoutcommit_data *data = calldata;
6531 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006532 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006533
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006534 nfs41_setup_sequence(session,
6535 &data->args.seq_args,
6536 &data->res.seq_res,
6537 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006538}
6539
6540static void
6541nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6542{
6543 struct nfs4_layoutcommit_data *data = calldata;
6544 struct nfs_server *server = NFS_SERVER(data->args.inode);
6545
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006546 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006547 return;
6548
6549 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006550 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6551 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6552 case -NFS4ERR_BADLAYOUT: /* no layout */
6553 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006554 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006555 break;
6556 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006557 nfs_post_op_update_inode_force_wcc(data->args.inode,
6558 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006559 break;
6560 default:
6561 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6562 rpc_restart_call_prepare(task);
6563 return;
6564 }
6565 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006566}
6567
6568static void nfs4_layoutcommit_release(void *calldata)
6569{
6570 struct nfs4_layoutcommit_data *data = calldata;
6571
Andy Adamsondb29c082011-07-30 20:52:38 -04006572 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006573 put_rpccred(data->cred);
6574 kfree(data);
6575}
6576
6577static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6578 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6579 .rpc_call_done = nfs4_layoutcommit_done,
6580 .rpc_release = nfs4_layoutcommit_release,
6581};
6582
6583int
Andy Adamsonef311532011-03-12 02:58:10 -05006584nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006585{
6586 struct rpc_message msg = {
6587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6588 .rpc_argp = &data->args,
6589 .rpc_resp = &data->res,
6590 .rpc_cred = data->cred,
6591 };
6592 struct rpc_task_setup task_setup_data = {
6593 .task = &data->task,
6594 .rpc_client = NFS_CLIENT(data->args.inode),
6595 .rpc_message = &msg,
6596 .callback_ops = &nfs4_layoutcommit_ops,
6597 .callback_data = data,
6598 .flags = RPC_TASK_ASYNC,
6599 };
6600 struct rpc_task *task;
6601 int status = 0;
6602
6603 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6604 "lbw: %llu inode %lu\n",
6605 data->task.tk_pid, sync,
6606 data->args.lastbytewritten,
6607 data->args.inode->i_ino);
6608
Trond Myklebust9d12b212012-01-17 22:04:25 -05006609 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006610 task = rpc_run_task(&task_setup_data);
6611 if (IS_ERR(task))
6612 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006613 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006614 goto out;
6615 status = nfs4_wait_for_completion_rpc_task(task);
6616 if (status != 0)
6617 goto out;
6618 status = task->tk_status;
6619out:
6620 dprintk("%s: status %d\n", __func__, status);
6621 rpc_put_task(task);
6622 return status;
6623}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006624
6625static int
6626_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6627 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6628{
6629 struct nfs41_secinfo_no_name_args args = {
6630 .style = SECINFO_STYLE_CURRENT_FH,
6631 };
6632 struct nfs4_secinfo_res res = {
6633 .flavors = flavors,
6634 };
6635 struct rpc_message msg = {
6636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6637 .rpc_argp = &args,
6638 .rpc_resp = &res,
6639 };
6640 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6641}
6642
6643static int
6644nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6645 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6646{
6647 struct nfs4_exception exception = { };
6648 int err;
6649 do {
6650 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6651 switch (err) {
6652 case 0:
6653 case -NFS4ERR_WRONGSEC:
6654 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006655 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006656 default:
6657 err = nfs4_handle_exception(server, err, &exception);
6658 }
6659 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006660out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006661 return err;
6662}
6663
6664static int
6665nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6666 struct nfs_fsinfo *info)
6667{
6668 int err;
6669 struct page *page;
6670 rpc_authflavor_t flavor;
6671 struct nfs4_secinfo_flavors *flavors;
6672
6673 page = alloc_page(GFP_KERNEL);
6674 if (!page) {
6675 err = -ENOMEM;
6676 goto out;
6677 }
6678
6679 flavors = page_address(page);
6680 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6681
6682 /*
6683 * Fall back on "guess and check" method if
6684 * the server doesn't support SECINFO_NO_NAME
6685 */
6686 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6687 err = nfs4_find_root_sec(server, fhandle, info);
6688 goto out_freepage;
6689 }
6690 if (err)
6691 goto out_freepage;
6692
6693 flavor = nfs_find_best_sec(flavors);
6694 if (err == 0)
6695 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6696
6697out_freepage:
6698 put_page(page);
6699 if (err == -EACCES)
6700 return -EPERM;
6701out:
6702 return err;
6703}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006704
6705static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006706{
6707 int status;
6708 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006709 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006710 };
6711 struct nfs41_test_stateid_res res;
6712 struct rpc_message msg = {
6713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6714 .rpc_argp = &args,
6715 .rpc_resp = &res,
6716 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006717
Chuck Lever38527b12012-07-11 16:30:23 -04006718 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006719 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006720 nfs4_set_sequence_privileged(&args.seq_args);
6721 status = nfs4_call_sync_sequence(server->client, server, &msg,
6722 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006723 if (status != NFS_OK) {
6724 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006725 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006726 }
6727 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006728 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006729}
6730
Chuck Lever38527b12012-07-11 16:30:23 -04006731/**
6732 * nfs41_test_stateid - perform a TEST_STATEID operation
6733 *
6734 * @server: server / transport on which to perform the operation
6735 * @stateid: state ID to test
6736 *
6737 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6738 * Otherwise a negative NFS4ERR value is returned if the operation
6739 * failed or the state ID is not currently valid.
6740 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006741static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006742{
6743 struct nfs4_exception exception = { };
6744 int err;
6745 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006746 err = _nfs41_test_stateid(server, stateid);
6747 if (err != -NFS4ERR_DELAY)
6748 break;
6749 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006750 } while (exception.retry);
6751 return err;
6752}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006753
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006754static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006755{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006756 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006757 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006758 };
6759 struct nfs41_free_stateid_res res;
6760 struct rpc_message msg = {
6761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6762 .rpc_argp = &args,
6763 .rpc_resp = &res,
6764 };
Chuck Lever38527b12012-07-11 16:30:23 -04006765 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006766
Chuck Lever38527b12012-07-11 16:30:23 -04006767 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006768 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006769 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006770 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006771 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006772 dprintk("NFS reply free_stateid: %d\n", status);
6773 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006774}
6775
Chuck Lever38527b12012-07-11 16:30:23 -04006776/**
6777 * nfs41_free_stateid - perform a FREE_STATEID operation
6778 *
6779 * @server: server / transport on which to perform the operation
6780 * @stateid: state ID to release
6781 *
6782 * Returns NFS_OK if the server freed "stateid". Otherwise a
6783 * negative NFS4ERR value is returned.
6784 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006785static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006786{
6787 struct nfs4_exception exception = { };
6788 int err;
6789 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006790 err = _nfs4_free_stateid(server, stateid);
6791 if (err != -NFS4ERR_DELAY)
6792 break;
6793 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006794 } while (exception.retry);
6795 return err;
6796}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006797
6798static bool nfs41_match_stateid(const nfs4_stateid *s1,
6799 const nfs4_stateid *s2)
6800{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006801 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006802 return false;
6803
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006804 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006805 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006806 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006807 return true;
6808
6809 return false;
6810}
6811
Andy Adamson557134a2009-04-01 09:21:53 -04006812#endif /* CONFIG_NFS_V4_1 */
6813
Trond Myklebust36281ca2012-03-04 18:13:56 -05006814static bool nfs4_match_stateid(const nfs4_stateid *s1,
6815 const nfs4_stateid *s2)
6816{
Trond Myklebustf597c532012-03-04 18:13:56 -05006817 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006818}
6819
6820
Trond Myklebust17280172012-03-11 13:11:00 -04006821static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006822 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006823 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 .recover_open = nfs4_open_reclaim,
6825 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006826 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006827 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006828 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829};
6830
Andy Adamson591d71c2009-04-01 09:22:47 -04006831#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006832static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006833 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6834 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6835 .recover_open = nfs4_open_reclaim,
6836 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006837 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006838 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006839 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006840 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006841};
6842#endif /* CONFIG_NFS_V4_1 */
6843
Trond Myklebust17280172012-03-11 13:11:00 -04006844static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006845 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006846 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 .recover_open = nfs4_open_expired,
6848 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006849 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006850 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851};
6852
Andy Adamson591d71c2009-04-01 09:22:47 -04006853#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006854static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006855 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6856 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006857 .recover_open = nfs41_open_expired,
6858 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006859 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006860 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006861};
6862#endif /* CONFIG_NFS_V4_1 */
6863
Trond Myklebust17280172012-03-11 13:11:00 -04006864static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006865 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006866 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006867 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006868};
6869
6870#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006871static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006872 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006873 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006874 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006875};
6876#endif
6877
Trond Myklebust97dc1352010-06-16 09:52:26 -04006878static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6879 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006880 .init_caps = NFS_CAP_READDIRPLUS
6881 | NFS_CAP_ATOMIC_OPEN
6882 | NFS_CAP_CHANGE_ATTR
6883 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006884 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006885 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006886 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006887 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6888 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6889 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006890};
6891
6892#if defined(CONFIG_NFS_V4_1)
6893static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6894 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006895 .init_caps = NFS_CAP_READDIRPLUS
6896 | NFS_CAP_ATOMIC_OPEN
6897 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006898 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006899 | NFS_CAP_STATEID_NFSV41
6900 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006901 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006902 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006903 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006904 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6905 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6906 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006907};
6908#endif
6909
Trond Myklebust97dc1352010-06-16 09:52:26 -04006910const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6911 [0] = &nfs_v4_0_minor_ops,
6912#if defined(CONFIG_NFS_V4_1)
6913 [1] = &nfs_v4_1_minor_ops,
6914#endif
6915};
6916
Bryan Schumaker73a79702012-07-16 16:39:12 -04006917const struct inode_operations nfs4_dir_inode_operations = {
6918 .create = nfs_create,
6919 .lookup = nfs_lookup,
6920 .atomic_open = nfs_atomic_open,
6921 .link = nfs_link,
6922 .unlink = nfs_unlink,
6923 .symlink = nfs_symlink,
6924 .mkdir = nfs_mkdir,
6925 .rmdir = nfs_rmdir,
6926 .mknod = nfs_mknod,
6927 .rename = nfs_rename,
6928 .permission = nfs_permission,
6929 .getattr = nfs_getattr,
6930 .setattr = nfs_setattr,
6931 .getxattr = generic_getxattr,
6932 .setxattr = generic_setxattr,
6933 .listxattr = generic_listxattr,
6934 .removexattr = generic_removexattr,
6935};
6936
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006937static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006938 .permission = nfs_permission,
6939 .getattr = nfs_getattr,
6940 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006941 .getxattr = generic_getxattr,
6942 .setxattr = generic_setxattr,
6943 .listxattr = generic_listxattr,
6944 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006945};
6946
David Howells509de812006-08-22 20:06:11 -04006947const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 .version = 4, /* protocol version */
6949 .dentry_ops = &nfs4_dentry_operations,
6950 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006951 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006952 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006954 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006955 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 .getattr = nfs4_proc_getattr,
6957 .setattr = nfs4_proc_setattr,
6958 .lookup = nfs4_proc_lookup,
6959 .access = nfs4_proc_access,
6960 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 .create = nfs4_proc_create,
6962 .remove = nfs4_proc_remove,
6963 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006964 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 .unlink_done = nfs4_proc_unlink_done,
6966 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006967 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006968 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006969 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 .link = nfs4_proc_link,
6971 .symlink = nfs4_proc_symlink,
6972 .mkdir = nfs4_proc_mkdir,
6973 .rmdir = nfs4_proc_remove,
6974 .readdir = nfs4_proc_readdir,
6975 .mknod = nfs4_proc_mknod,
6976 .statfs = nfs4_proc_statfs,
6977 .fsinfo = nfs4_proc_fsinfo,
6978 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006979 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 .decode_dirent = nfs4_decode_dirent,
6981 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006982 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006983 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006984 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006986 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006987 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006988 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006990 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006991 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006993 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006994 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006995 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006996 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006997 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006998 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006999 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007000 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007001 .create_server = nfs4_create_server,
7002 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003};
7004
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007005static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7006 .prefix = XATTR_NAME_NFSV4_ACL,
7007 .list = nfs4_xattr_list_nfs4_acl,
7008 .get = nfs4_xattr_get_nfs4_acl,
7009 .set = nfs4_xattr_set_nfs4_acl,
7010};
7011
7012const struct xattr_handler *nfs4_xattr_handlers[] = {
7013 &nfs4_xattr_nfs4_acl_handler,
7014 NULL
7015};
7016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017/*
7018 * Local variables:
7019 * c-basic-offset: 8
7020 * End:
7021 */