blob: 5f2010e0926ec839706aa2734b28c88fc18e81fc [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;
Trond Myklebust8b6cc4d2013-04-01 15:34:05 -04001435 case -NFS4ERR_DELAY:
1436 case -NFS4ERR_GRACE:
1437 set_bit(NFS_DELEGATED_STATE, &state->flags);
1438 ssleep(1);
1439 err = -EAGAIN;
1440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001442 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 err = nfs4_handle_exception(server, err, &exception);
1444 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447}
1448
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1450{
1451 struct nfs4_opendata *data = calldata;
1452
1453 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001455 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001456 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001458 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460}
1461
1462static void nfs4_open_confirm_release(void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
1465 struct nfs4_state *state = NULL;
1466
1467 /* If this request hasn't been cancelled, do nothing */
1468 if (data->cancelled == 0)
1469 goto out_free;
1470 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001471 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001474 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001475 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001477 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001478}
1479
1480static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001481 .rpc_call_done = nfs4_open_confirm_done,
1482 .rpc_release = nfs4_open_confirm_release,
1483};
1484
1485/*
1486 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1487 */
1488static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1489{
1490 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1491 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001492 struct rpc_message msg = {
1493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1494 .rpc_argp = &data->c_arg,
1495 .rpc_resp = &data->c_res,
1496 .rpc_cred = data->owner->so_cred,
1497 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001498 struct rpc_task_setup task_setup_data = {
1499 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001500 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001501 .callback_ops = &nfs4_open_confirm_ops,
1502 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001503 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001504 .flags = RPC_TASK_ASYNC,
1505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int status;
1507
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001508 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001509 data->rpc_done = 0;
1510 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001511 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001512 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001513 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001514 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001515 status = nfs4_wait_for_completion_rpc_task(task);
1516 if (status != 0) {
1517 data->cancelled = 1;
1518 smp_wmb();
1519 } else
1520 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001521 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return status;
1523}
1524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001529
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001531 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001532 /*
1533 * Check if we still need to send an OPEN call, or if we can use
1534 * a delegation instead.
1535 */
1536 if (data->state != NULL) {
1537 struct nfs_delegation *delegation;
1538
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001539 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001540 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001541 rcu_read_lock();
1542 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001543 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1544 can_open_delegated(delegation, data->o_arg.fmode))
1545 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001546 rcu_read_unlock();
1547 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001548 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001549 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001550 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001551 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001552 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001553 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1554 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001555 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001556 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001557 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001558 &data->o_res.seq_res,
1559 task) != 0)
1560 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001561 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001562unlock_no_action:
1563 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001564out_no_action:
1565 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001566out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001567 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001568}
1569
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570static void nfs4_open_done(struct rpc_task *task, void *calldata)
1571{
1572 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001575
Trond Myklebust14516c32010-07-31 14:29:06 -04001576 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1577 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001578
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001580 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1581 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001582 case S_IFREG:
1583 break;
1584 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001586 break;
1587 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001589 break;
1590 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001592 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001593 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001594 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001595 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1596 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001597 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001598 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001600
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601static void nfs4_open_release(void *calldata)
1602{
1603 struct nfs4_opendata *data = calldata;
1604 struct nfs4_state *state = NULL;
1605
1606 /* If this request hasn't been cancelled, do nothing */
1607 if (data->cancelled == 0)
1608 goto out_free;
1609 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001610 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 goto out_free;
1612 /* In case we need an open_confirm, no cleanup! */
1613 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1614 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001616 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001617 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001618out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001619 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620}
1621
1622static const struct rpc_call_ops nfs4_open_ops = {
1623 .rpc_call_prepare = nfs4_open_prepare,
1624 .rpc_call_done = nfs4_open_done,
1625 .rpc_release = nfs4_open_release,
1626};
1627
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001628static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629{
1630 struct inode *dir = data->dir->d_inode;
1631 struct nfs_server *server = NFS_SERVER(dir);
1632 struct nfs_openargs *o_arg = &data->o_arg;
1633 struct nfs_openres *o_res = &data->o_res;
1634 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001635 struct rpc_message msg = {
1636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1637 .rpc_argp = o_arg,
1638 .rpc_resp = o_res,
1639 .rpc_cred = data->owner->so_cred,
1640 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001641 struct rpc_task_setup task_setup_data = {
1642 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001643 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001644 .callback_ops = &nfs4_open_ops,
1645 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001646 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001647 .flags = RPC_TASK_ASYNC,
1648 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 int status;
1650
Trond Myklebust9d12b212012-01-17 22:04:25 -05001651 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001652 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001653 data->rpc_done = 0;
1654 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001655 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001656 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001657 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001658 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001659 if (IS_ERR(task))
1660 return PTR_ERR(task);
1661 status = nfs4_wait_for_completion_rpc_task(task);
1662 if (status != 0) {
1663 data->cancelled = 1;
1664 smp_wmb();
1665 } else
1666 status = data->rpc_status;
1667 rpc_put_task(task);
1668
1669 return status;
1670}
1671
1672static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1673{
1674 struct inode *dir = data->dir->d_inode;
1675 struct nfs_openres *o_res = &data->o_res;
1676 int status;
1677
1678 status = nfs4_run_open_task(data, 1);
1679 if (status != 0 || !data->rpc_done)
1680 return status;
1681
Trond Myklebust6926afd2012-01-07 13:22:46 -05001682 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1683
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001684 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1685 status = _nfs4_proc_open_confirm(data);
1686 if (status != 0)
1687 return status;
1688 }
1689
1690 return status;
1691}
1692
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001693static int nfs4_opendata_access(struct rpc_cred *cred,
1694 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001695 struct nfs4_state *state, fmode_t fmode,
1696 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001697{
1698 struct nfs_access_entry cache;
1699 u32 mask;
1700
1701 /* access call failed or for some reason the server doesn't
1702 * support any access modes -- defer access call until later */
1703 if (opendata->o_res.access_supported == 0)
1704 return 0;
1705
1706 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001707 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001708 * write mode bits, but POSIX allows the creating process to write.
1709 * use openflags to check for exec, because fmode won't
1710 * always have FMODE_EXEC set when file open for exec. */
1711 if (openflags & __FMODE_EXEC) {
1712 /* ONLY check for exec rights */
1713 mask = MAY_EXEC;
1714 } else if (fmode & FMODE_READ)
1715 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001716
1717 cache.cred = cred;
1718 cache.jiffies = jiffies;
1719 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1720 nfs_access_add_cache(state->inode, &cache);
1721
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001722 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001723 return 0;
1724
1725 /* even though OPEN succeeded, access is denied. Close the file */
1726 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001727 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001728}
1729
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001730/*
1731 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1732 */
1733static int _nfs4_proc_open(struct nfs4_opendata *data)
1734{
1735 struct inode *dir = data->dir->d_inode;
1736 struct nfs_server *server = NFS_SERVER(dir);
1737 struct nfs_openargs *o_arg = &data->o_arg;
1738 struct nfs_openres *o_res = &data->o_res;
1739 int status;
1740
1741 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001742 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001743 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001744 if (status != 0) {
1745 if (status == -NFS4ERR_BADNAME &&
1746 !(o_arg->open_flags & O_CREAT))
1747 return -ENOENT;
1748 return status;
1749 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001750
Trond Myklebust6926afd2012-01-07 13:22:46 -05001751 nfs_fattr_map_and_free_names(server, &data->f_attr);
1752
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001753 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001754 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001755 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1756 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001758 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001763 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Andy Adamsond83217c2011-03-01 01:34:17 +00001767static int nfs4_recover_expired_lease(struct nfs_server *server)
1768{
1769 return nfs4_client_recover_expired_lease(server->nfs_client);
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/*
1773 * OPEN_EXPIRED:
1774 * reclaim state on the server after a network partition.
1775 * Assumes caller holds the appropriate lock
1776 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001777static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001779 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001782 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001783 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001784 if (IS_ERR(opendata))
1785 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001786 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001787 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001788 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001789 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001790 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001793static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001794{
Trond Myklebust539cd032007-06-05 11:46:42 -04001795 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001796 struct nfs4_exception exception = { };
1797 int err;
1798
1799 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001800 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001801 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1802 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001803 switch (err) {
1804 default:
1805 goto out;
1806 case -NFS4ERR_GRACE:
1807 case -NFS4ERR_DELAY:
1808 nfs4_handle_exception(server, err, &exception);
1809 err = 0;
1810 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001811 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001812out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001813 return err;
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Trond Myklebust864472e2006-01-03 09:55:15 +01001821 ctx = nfs4_state_find_open_context(state);
1822 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001823 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001824 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001825 put_nfs_open_context(ctx);
1826 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001829#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001830static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001831{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001832 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001833 nfs4_stateid *stateid = &state->stateid;
1834 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001835
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001836 /* If a state reset has been done, test_stateid is unneeded */
1837 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1838 return;
1839
1840 status = nfs41_test_stateid(server, stateid);
1841 if (status != NFS_OK) {
1842 /* Free the stateid unless the server explicitly
1843 * informs us the stateid is unrecognized. */
1844 if (status != -NFS4ERR_BAD_STATEID)
1845 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001846 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001847
Bryan Schumaker69388672012-09-26 15:25:52 -04001848 write_seqlock(&state->seqlock);
1849 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1850 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001851 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1852 }
1853}
1854
1855/**
1856 * nfs41_check_open_stateid - possibly free an open stateid
1857 *
1858 * @state: NFSv4 state for an inode
1859 *
1860 * Returns NFS_OK if recovery for this stateid is now finished.
1861 * Otherwise a negative NFS4ERR value is returned.
1862 */
1863static int nfs41_check_open_stateid(struct nfs4_state *state)
1864{
1865 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001866 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001867 int status;
1868
1869 /* If a state reset has been done, test_stateid is unneeded */
1870 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1871 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1872 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1873 return -NFS4ERR_BAD_STATEID;
1874
1875 status = nfs41_test_stateid(server, stateid);
1876 if (status != NFS_OK) {
1877 /* Free the stateid unless the server explicitly
1878 * informs us the stateid is unrecognized. */
1879 if (status != -NFS4ERR_BAD_STATEID)
1880 nfs41_free_stateid(server, stateid);
1881
1882 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1883 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1884 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001885 }
1886 return status;
1887}
1888
1889static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1890{
Chuck Levereb64cf92012-07-11 16:30:05 -04001891 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001892
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001893 nfs41_clear_delegation_stateid(state);
1894 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001895 if (status != NFS_OK)
1896 status = nfs4_open_expired(sp, state);
1897 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001898}
1899#endif
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001902 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1903 * fields corresponding to attributes that were used to store the verifier.
1904 * Make sure we clobber those fields in the later setattr call
1905 */
1906static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1907{
1908 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1909 !(sattr->ia_valid & ATTR_ATIME_SET))
1910 sattr->ia_valid |= ATTR_ATIME;
1911
1912 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1913 !(sattr->ia_valid & ATTR_MTIME_SET))
1914 sattr->ia_valid |= ATTR_MTIME;
1915}
1916
Trond Myklebustc21443c2013-02-07 14:26:21 -05001917static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1918 fmode_t fmode,
1919 int flags,
1920 struct nfs4_state **res)
1921{
1922 struct nfs4_state_owner *sp = opendata->owner;
1923 struct nfs_server *server = sp->so_server;
1924 struct nfs4_state *state;
1925 unsigned int seq;
1926 int ret;
1927
1928 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1929
1930 ret = _nfs4_proc_open(opendata);
1931 if (ret != 0)
1932 goto out;
1933
1934 state = nfs4_opendata_to_nfs4_state(opendata);
1935 ret = PTR_ERR(state);
1936 if (IS_ERR(state))
1937 goto out;
1938 if (server->caps & NFS_CAP_POSIX_LOCK)
1939 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1940
1941 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1942 if (ret != 0)
1943 goto out;
1944
Trond Myklebust91876b12013-03-28 14:01:33 -04001945 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001946 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001947 *res = state;
1948out:
1949 return ret;
1950}
1951
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001952/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001953 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
Andy Adamson82be4172012-05-23 05:02:35 -04001955static int _nfs4_do_open(struct inode *dir,
1956 struct dentry *dentry,
1957 fmode_t fmode,
1958 int flags,
1959 struct iattr *sattr,
1960 struct rpc_cred *cred,
1961 struct nfs4_state **res,
1962 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct nfs4_state_owner *sp;
1965 struct nfs4_state *state = NULL;
1966 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001967 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001968 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001969 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001973 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1974 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1976 goto out_err;
1977 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001978 status = nfs4_recover_expired_lease(server);
1979 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001980 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001981 if (dentry->d_inode != NULL)
1982 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001983 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001984 if (dentry->d_inode)
1985 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001986 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001987 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001988 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001989 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Andy Adamson82be4172012-05-23 05:02:35 -04001991 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1992 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1993 if (!opendata->f_attr.mdsthreshold)
1994 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001995 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001996 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001997 if (dentry->d_inode != NULL)
1998 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001999
Trond Myklebustc21443c2013-02-07 14:26:21 -05002000 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002001 if (status != 0)
2002 goto err_opendata_put;
2003
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002004 if (opendata->o_arg.open_flags & O_EXCL) {
2005 nfs4_exclusive_attrset(opendata, sattr);
2006
2007 nfs_fattr_init(opendata->o_res.f_attr);
2008 status = nfs4_do_setattr(state->inode, cred,
2009 opendata->o_res.f_attr, sattr,
2010 state);
2011 if (status == 0)
2012 nfs_setattr_update_inode(state->inode, sattr);
2013 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2014 }
Andy Adamson82be4172012-05-23 05:02:35 -04002015
2016 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2017 *ctx_th = opendata->f_attr.mdsthreshold;
2018 else
2019 kfree(opendata->f_attr.mdsthreshold);
2020 opendata->f_attr.mdsthreshold = NULL;
2021
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002022 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 *res = state;
2025 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002026err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002027 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002028 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002029err_put_state_owner:
2030 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 *res = NULL;
2033 return status;
2034}
2035
2036
Andy Adamson82be4172012-05-23 05:02:35 -04002037static struct nfs4_state *nfs4_do_open(struct inode *dir,
2038 struct dentry *dentry,
2039 fmode_t fmode,
2040 int flags,
2041 struct iattr *sattr,
2042 struct rpc_cred *cred,
2043 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002045 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct nfs4_exception exception = { };
2047 struct nfs4_state *res;
2048 int status;
2049
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002050 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002052 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2053 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (status == 0)
2055 break;
2056 /* NOTE: BAD_SEQID means the server and client disagree about the
2057 * book-keeping w.r.t. state-changing operations
2058 * (OPEN/CLOSE/LOCK/LOCKU...)
2059 * It is actually a sign of a bug on the client or on the server.
2060 *
2061 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002062 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * have unhashed the old state_owner for us, and that we can
2064 * therefore safely retry using a new one. We should still warn
2065 * the user though...
2066 */
2067 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002068 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002069 " returned a bad sequence-id error!\n",
2070 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 exception.retry = 1;
2072 continue;
2073 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002074 /*
2075 * BAD_STATEID on OPEN means that the server cancelled our
2076 * state before it received the OPEN_CONFIRM.
2077 * Recover by retrying the request as per the discussion
2078 * on Page 181 of RFC3530.
2079 */
2080 if (status == -NFS4ERR_BAD_STATEID) {
2081 exception.retry = 1;
2082 continue;
2083 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002084 if (status == -EAGAIN) {
2085 /* We must have found a delegation */
2086 exception.retry = 1;
2087 continue;
2088 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002089 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2090 continue;
2091 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 status, &exception));
2093 } while (exception.retry);
2094 return res;
2095}
2096
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002097static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2098 struct nfs_fattr *fattr, struct iattr *sattr,
2099 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002101 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002103 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 .iap = sattr,
2105 .server = server,
2106 .bitmask = server->attr_bitmask,
2107 };
2108 struct nfs_setattrres res = {
2109 .fattr = fattr,
2110 .server = server,
2111 };
2112 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2114 .rpc_argp = &arg,
2115 .rpc_resp = &res,
2116 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002118 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002119 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Trond Myklebust0e574af2005-10-27 22:12:38 -04002121 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Trond Myklebust5d422302013-03-14 16:57:48 -04002123 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002124 struct nfs_lockowner lockowner = {
2125 .l_owner = current->files,
2126 .l_pid = current->tgid,
2127 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002128 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002129 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002130 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2131 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002132 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002133 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002134 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Bryan Schumaker7c513052011-03-24 17:12:24 +00002136 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002137 if (status == 0 && state != NULL)
2138 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002139 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002142static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2143 struct nfs_fattr *fattr, struct iattr *sattr,
2144 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002146 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002147 struct nfs4_exception exception = {
2148 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002149 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002150 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int err;
2152 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002153 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2154 switch (err) {
2155 case -NFS4ERR_OPENMODE:
2156 if (state && !(state->state & FMODE_WRITE)) {
2157 err = -EBADF;
2158 if (sattr->ia_valid & ATTR_OPEN)
2159 err = -EACCES;
2160 goto out;
2161 }
2162 }
2163 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002165out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return err;
2167}
2168
2169struct nfs4_closedata {
2170 struct inode *inode;
2171 struct nfs4_state *state;
2172 struct nfs_closeargs arg;
2173 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002174 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002175 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002176 bool roc;
2177 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
2179
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002180static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002181{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002182 struct nfs4_closedata *calldata = data;
2183 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002184 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002185
Fred Isamanf7e89172011-01-06 11:36:32 +00002186 if (calldata->roc)
2187 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002188 nfs4_put_open_state(calldata->state);
2189 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002190 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002191 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002192 kfree(calldata);
2193}
2194
Trond Myklebust88069f72009-12-08 08:33:16 -05002195static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2196 fmode_t fmode)
2197{
2198 spin_lock(&state->owner->so_lock);
2199 if (!(fmode & FMODE_READ))
2200 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2201 if (!(fmode & FMODE_WRITE))
2202 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2203 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2204 spin_unlock(&state->owner->so_lock);
2205}
2206
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002207static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002209 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct nfs_server *server = NFS_SERVER(calldata->inode);
2212
Chuck Levera3ca5652012-03-01 17:00:40 -05002213 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002214 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /* hmm. we are done with the inode, and in the process of freeing
2217 * the state_owner. we keep this around to process errors
2218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 switch (task->tk_status) {
2220 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002221 if (calldata->roc)
2222 pnfs_roc_set_barrier(state->inode,
2223 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002224 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002225 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002226 nfs4_close_clear_stateid_flags(state,
2227 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 break;
2229 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002230 case -NFS4ERR_OLD_STATEID:
2231 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002233 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002236 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2237 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002239 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002240 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002241 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002244static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002246 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002247 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002248 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002249 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002250
Chuck Levera3ca5652012-03-01 17:00:40 -05002251 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002252 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002253 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002254
Trond Myklebust88069f72009-12-08 08:33:16 -05002255 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2256 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002257 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002258 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002259 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002260 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002261 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2262 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2263 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002264 }
2265 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002266 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2267 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2268 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002269 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002270 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002271 if (!nfs4_valid_open_stateid(state))
2272 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002273 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002274
2275 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002276 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002277 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002278 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002279
Fred Isamanf7e89172011-01-06 11:36:32 +00002280 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002281 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002282 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002283 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002284 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002285 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002286
Trond Myklebust516a6af2005-10-27 22:12:41 -04002287 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002288 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002289 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002290 &calldata->arg.seq_args,
2291 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002292 task) != 0)
2293 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002294 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002295 return;
2296out_no_action:
2297 task->tk_action = NULL;
2298out_wait:
2299 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002302static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002303 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002304 .rpc_call_done = nfs4_close_done,
2305 .rpc_release = nfs4_free_closedata,
2306};
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308/*
2309 * It is possible for data to be read/written from a mem-mapped file
2310 * after the sys_close call (which hits the vfs layer as a flush).
2311 * This means that we can't safely call nfsv4 close on a file until
2312 * the inode is cleared. This in turn means that we are not good
2313 * NFSv4 citizens - we do not indicate to the server to update the file's
2314 * share state even when we are done with one of the three share
2315 * stateid's in the inode.
2316 *
2317 * NOTE: Caller must be holding the sp->so_owner semaphore!
2318 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002319int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002321 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002323 struct nfs4_state_owner *sp = state->owner;
2324 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2327 .rpc_cred = state->owner->so_cred,
2328 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002329 struct rpc_task_setup task_setup_data = {
2330 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002331 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002332 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002333 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .flags = RPC_TASK_ASYNC,
2335 };
Trond Myklebust95121352005-10-18 14:20:12 -07002336 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002338 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002340 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002341 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002342 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002344 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002345 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002347 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002348 if (calldata->arg.seqid == NULL)
2349 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002350 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002351 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002352 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002353 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002354 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002355 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002356 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002357
Trond Myklebust1174dd12010-12-21 10:52:24 -05002358 msg.rpc_argp = &calldata->arg;
2359 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002360 task_setup_data.callback_data = calldata;
2361 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002362 if (IS_ERR(task))
2363 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002364 status = 0;
2365 if (wait)
2366 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002367 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002368 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002369out_free_calldata:
2370 kfree(calldata);
2371out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002372 nfs4_put_open_state(state);
2373 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002374 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Trond Myklebust2b484292010-09-17 10:56:51 -04002377static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002378nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct nfs4_state *state;
2381
Trond Myklebust565277f2007-10-15 18:17:53 -04002382 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002383 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2384 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002385 if (IS_ERR(state))
2386 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002387 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002388 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Trond Myklebust1185a552009-12-03 15:54:02 -05002391static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002392{
2393 if (ctx->state == NULL)
2394 return;
2395 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002396 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002397 else
Al Viro643168c2011-06-22 18:20:23 -04002398 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002399}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2402{
Benny Halevy43652ad2009-04-01 09:21:54 -04002403 struct nfs4_server_caps_arg args = {
2404 .fhandle = fhandle,
2405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct nfs4_server_caps_res res = {};
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002409 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .rpc_resp = &res,
2411 };
2412 int status;
2413
Bryan Schumaker7c513052011-03-24 17:12:24 +00002414 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (status == 0) {
2416 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002417 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2418 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2419 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2420 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2421 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2423 server->caps |= NFS_CAP_ACLS;
2424 if (res.has_links != 0)
2425 server->caps |= NFS_CAP_HARDLINKS;
2426 if (res.has_symlinks != 0)
2427 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002428 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2429 server->caps |= NFS_CAP_FILEID;
2430 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2431 server->caps |= NFS_CAP_MODE;
2432 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2433 server->caps |= NFS_CAP_NLINK;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2435 server->caps |= NFS_CAP_OWNER;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2437 server->caps |= NFS_CAP_OWNER_GROUP;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2439 server->caps |= NFS_CAP_ATIME;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2441 server->caps |= NFS_CAP_CTIME;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2443 server->caps |= NFS_CAP_MTIME;
2444
Trond Myklebusta65318b2009-03-11 14:10:28 -04002445 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2446 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2447 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002449 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return status;
2453}
2454
Trond Myklebust55a97592006-06-09 09:34:19 -04002455int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 struct nfs4_exception exception = { };
2458 int err;
2459 do {
2460 err = nfs4_handle_exception(server,
2461 _nfs4_server_capabilities(server, fhandle),
2462 &exception);
2463 } while (exception.retry);
2464 return err;
2465}
2466
2467static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2468 struct nfs_fsinfo *info)
2469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct nfs4_lookup_root_arg args = {
2471 .bitmask = nfs4_fattr_bitmap,
2472 };
2473 struct nfs4_lookup_res res = {
2474 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002475 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .fh = fhandle,
2477 };
2478 struct rpc_message msg = {
2479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2480 .rpc_argp = &args,
2481 .rpc_resp = &res,
2482 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002483
Trond Myklebust0e574af2005-10-27 22:12:38 -04002484 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002485 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
2488static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2489 struct nfs_fsinfo *info)
2490{
2491 struct nfs4_exception exception = { };
2492 int err;
2493 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002494 err = _nfs4_lookup_root(server, fhandle, info);
2495 switch (err) {
2496 case 0:
2497 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002498 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002499 default:
2500 err = nfs4_handle_exception(server, err, &exception);
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002503out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return err;
2505}
2506
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002507static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2508 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2509{
2510 struct rpc_auth *auth;
2511 int ret;
2512
2513 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002514 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002515 ret = -EIO;
2516 goto out;
2517 }
2518 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002519out:
2520 return ret;
2521}
2522
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002523static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002524 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002526 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002527 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002529 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002530 if (len < 0)
2531 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002532
2533 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002534 /* AUTH_UNIX is the default flavor if none was specified,
2535 * thus has already been tried. */
2536 if (flav_array[i] == RPC_AUTH_UNIX)
2537 continue;
2538
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002539 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002540 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002541 continue;
2542 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002543 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002544 /*
2545 * -EACCESS could mean that the user doesn't have correct permissions
2546 * to access the mount. It could also mean that we tried to mount
2547 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2548 * existing mount programs don't handle -EACCES very well so it should
2549 * be mapped to -EPERM instead.
2550 */
2551 if (status == -EACCES)
2552 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002553 return status;
2554}
2555
2556/*
2557 * get the file handle for the "/" directory on the server
2558 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002559int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2560 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002561{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002562 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002563 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002564 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2565 /*
2566 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2567 * by nfs4_map_errors() as this function exits.
2568 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002569 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (status == 0)
2571 status = nfs4_server_capabilities(server, fhandle);
2572 if (status == 0)
2573 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002574 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Bryan Schumakerbae36242012-05-10 15:07:31 -04002577static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2578 struct nfs_fsinfo *info)
2579{
2580 int error;
2581 struct nfs_fattr *fattr = info->fattr;
2582
2583 error = nfs4_server_capabilities(server, mntfh);
2584 if (error < 0) {
2585 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2586 return error;
2587 }
2588
2589 error = nfs4_proc_getattr(server, mntfh, fattr);
2590 if (error < 0) {
2591 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2592 return error;
2593 }
2594
2595 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2596 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2597 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2598
2599 return error;
2600}
2601
Manoj Naik6b97fd32006-06-09 09:34:29 -04002602/*
2603 * Get locations and (maybe) other attributes of a referral.
2604 * Note that we'll actually follow the referral later when
2605 * we detect fsid mismatch in inode revalidation
2606 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002607static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2608 const struct qstr *name, struct nfs_fattr *fattr,
2609 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002610{
2611 int status = -ENOMEM;
2612 struct page *page = NULL;
2613 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002614
2615 page = alloc_page(GFP_KERNEL);
2616 if (page == NULL)
2617 goto out;
2618 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2619 if (locations == NULL)
2620 goto out;
2621
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002622 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002623 if (status != 0)
2624 goto out;
2625 /* Make sure server returned a different fsid for the referral */
2626 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002627 dprintk("%s: server did not return a different fsid for"
2628 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002629 status = -EIO;
2630 goto out;
2631 }
Andy Adamson533eb462011-06-13 18:25:56 -04002632 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2633 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002634
Andy Adamson533eb462011-06-13 18:25:56 -04002635 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002636 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 memset(fhandle, 0, sizeof(struct nfs_fh));
2638out:
2639 if (page)
2640 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002641 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002642 return status;
2643}
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2646{
2647 struct nfs4_getattr_arg args = {
2648 .fh = fhandle,
2649 .bitmask = server->attr_bitmask,
2650 };
2651 struct nfs4_getattr_res res = {
2652 .fattr = fattr,
2653 .server = server,
2654 };
2655 struct rpc_message msg = {
2656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2657 .rpc_argp = &args,
2658 .rpc_resp = &res,
2659 };
2660
Trond Myklebust0e574af2005-10-27 22:12:38 -04002661 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002662 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2666{
2667 struct nfs4_exception exception = { };
2668 int err;
2669 do {
2670 err = nfs4_handle_exception(server,
2671 _nfs4_proc_getattr(server, fhandle, fattr),
2672 &exception);
2673 } while (exception.retry);
2674 return err;
2675}
2676
2677/*
2678 * The file is not closed if it is opened due to the a request to change
2679 * the size of the file. The open call will not be needed once the
2680 * VFS layer lookup-intents are implemented.
2681 *
2682 * Close is called when the inode is destroyed.
2683 * If we haven't opened the file for O_WRONLY, we
2684 * need to in the size_change case to obtain a stateid.
2685 *
2686 * Got race?
2687 * Because OPEN is always done by name in nfsv4, it is
2688 * possible that we opened a different file by the same
2689 * name. We can recognize this race condition, but we
2690 * can't do anything about it besides returning an error.
2691 *
2692 * This will be fixed with VFS changes (lookup-intent).
2693 */
2694static int
2695nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2696 struct iattr *sattr)
2697{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002698 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002699 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002700 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int status;
2702
Benny Halevy8a1636c2010-07-14 15:43:57 -04002703 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002704 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002705
Trond Myklebust0e574af2005-10-27 22:12:38 -04002706 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Andy Adamson26699402012-05-30 16:12:24 -04002708 /* Deal with open(O_TRUNC) */
2709 if (sattr->ia_valid & ATTR_OPEN)
2710 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2711
2712 /* Optimization: if the end result is no change, don't RPC */
2713 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2714 return 0;
2715
Trond Myklebustd5308382005-11-04 15:33:38 -05002716 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002717 if (sattr->ia_valid & ATTR_FILE) {
2718 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002719
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002720 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002721 if (ctx) {
2722 cred = ctx->cred;
2723 state = ctx->state;
2724 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002725 }
2726
2727 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002728 if (status == 0)
2729 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return status;
2731}
2732
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002733static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2734 const struct qstr *name, struct nfs_fh *fhandle,
2735 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002736{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002737 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002738 int status;
2739 struct nfs4_lookup_arg args = {
2740 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002741 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002742 .name = name,
2743 };
2744 struct nfs4_lookup_res res = {
2745 .server = server,
2746 .fattr = fattr,
2747 .fh = fhandle,
2748 };
2749 struct rpc_message msg = {
2750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2751 .rpc_argp = &args,
2752 .rpc_resp = &res,
2753 };
2754
2755 nfs_fattr_init(fattr);
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002758 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 dprintk("NFS reply lookup: %d\n", status);
2760 return status;
2761}
2762
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002763static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002764{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002765 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002766 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002767 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2768 fattr->nlink = 2;
2769}
2770
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002771static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2772 struct qstr *name, struct nfs_fh *fhandle,
2773 struct nfs_fattr *fattr)
2774{
2775 struct nfs4_exception exception = { };
2776 struct rpc_clnt *client = *clnt;
2777 int err;
2778 do {
2779 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2780 switch (err) {
2781 case -NFS4ERR_BADNAME:
2782 err = -ENOENT;
2783 goto out;
2784 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002785 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002786 goto out;
2787 case -NFS4ERR_WRONGSEC:
2788 err = -EPERM;
2789 if (client != *clnt)
2790 goto out;
2791
2792 client = nfs4_create_sec_client(client, dir, name);
2793 if (IS_ERR(client))
2794 return PTR_ERR(client);
2795
2796 exception.retry = 1;
2797 break;
2798 default:
2799 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2800 }
2801 } while (exception.retry);
2802
2803out:
2804 if (err == 0)
2805 *clnt = client;
2806 else if (client != *clnt)
2807 rpc_shutdown_client(client);
2808
2809 return err;
2810}
2811
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002812static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002813 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002815 int status;
2816 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002817
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002818 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2819 if (client != NFS_CLIENT(dir)) {
2820 rpc_shutdown_client(client);
2821 nfs_fixup_secinfo_attributes(fattr);
2822 }
2823 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002826struct rpc_clnt *
2827nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2828 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2829{
2830 int status;
2831 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2832
2833 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2834 if (status < 0) {
2835 rpc_shutdown_client(client);
2836 return ERR_PTR(status);
2837 }
2838 return client;
2839}
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2842{
Trond Myklebust76b32992007-08-10 17:45:11 -04002843 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct nfs4_accessargs args = {
2845 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002846 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002848 struct nfs4_accessres res = {
2849 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002850 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct rpc_message msg = {
2852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2853 .rpc_argp = &args,
2854 .rpc_resp = &res,
2855 .rpc_cred = entry->cred,
2856 };
2857 int mode = entry->mask;
2858 int status;
2859
2860 /*
2861 * Determine which access bits we want to ask for...
2862 */
2863 if (mode & MAY_READ)
2864 args.access |= NFS4_ACCESS_READ;
2865 if (S_ISDIR(inode->i_mode)) {
2866 if (mode & MAY_WRITE)
2867 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2868 if (mode & MAY_EXEC)
2869 args.access |= NFS4_ACCESS_LOOKUP;
2870 } else {
2871 if (mode & MAY_WRITE)
2872 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2873 if (mode & MAY_EXEC)
2874 args.access |= NFS4_ACCESS_EXECUTE;
2875 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002876
2877 res.fattr = nfs_alloc_fattr();
2878 if (res.fattr == NULL)
2879 return -ENOMEM;
2880
Bryan Schumaker7c513052011-03-24 17:12:24 +00002881 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002883 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002884 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002886 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return status;
2888}
2889
2890static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2891{
2892 struct nfs4_exception exception = { };
2893 int err;
2894 do {
2895 err = nfs4_handle_exception(NFS_SERVER(inode),
2896 _nfs4_proc_access(inode, entry),
2897 &exception);
2898 } while (exception.retry);
2899 return err;
2900}
2901
2902/*
2903 * TODO: For the time being, we don't try to get any attributes
2904 * along with any of the zero-copy operations READ, READDIR,
2905 * READLINK, WRITE.
2906 *
2907 * In the case of the first three, we want to put the GETATTR
2908 * after the read-type operation -- this is because it is hard
2909 * to predict the length of a GETATTR response in v4, and thus
2910 * align the READ data correctly. This means that the GETATTR
2911 * may end up partially falling into the page cache, and we should
2912 * shift it into the 'tail' of the xdr_buf before processing.
2913 * To do this efficiently, we need to know the total length
2914 * of data received, which doesn't seem to be available outside
2915 * of the RPC layer.
2916 *
2917 * In the case of WRITE, we also want to put the GETATTR after
2918 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002919 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 *
2921 * Both of these changes to the XDR layer would in fact be quite
2922 * minor, but I decided to leave them for a subsequent patch.
2923 */
2924static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2925 unsigned int pgbase, unsigned int pglen)
2926{
2927 struct nfs4_readlink args = {
2928 .fh = NFS_FH(inode),
2929 .pgbase = pgbase,
2930 .pglen = pglen,
2931 .pages = &page,
2932 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002933 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct rpc_message msg = {
2935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2936 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002937 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 };
2939
Bryan Schumaker7c513052011-03-24 17:12:24 +00002940 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 -07002941}
2942
2943static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2944 unsigned int pgbase, unsigned int pglen)
2945{
2946 struct nfs4_exception exception = { };
2947 int err;
2948 do {
2949 err = nfs4_handle_exception(NFS_SERVER(inode),
2950 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2951 &exception);
2952 } while (exception.retry);
2953 return err;
2954}
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002957 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959static int
2960nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002961 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002963 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int status = 0;
2966
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002967 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2968 if (IS_ERR(ctx))
2969 return PTR_ERR(ctx);
2970
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002971 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002972 state = nfs4_do_open(dir, dentry, ctx->mode,
2973 flags, sattr, ctx->cred,
2974 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002975 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (IS_ERR(state)) {
2977 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002980 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002981 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002982 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002984 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return status;
2986}
2987
2988static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2989{
Trond Myklebust16e42952005-10-27 22:12:44 -04002990 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002991 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002993 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002995 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002996 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002997 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3000 .rpc_argp = &args,
3001 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003003 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003004
Bryan Schumaker7c513052011-03-24 17:12:24 +00003005 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003006 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003007 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return status;
3009}
3010
3011static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3012{
3013 struct nfs4_exception exception = { };
3014 int err;
3015 do {
3016 err = nfs4_handle_exception(NFS_SERVER(dir),
3017 _nfs4_proc_remove(dir, name),
3018 &exception);
3019 } while (exception.retry);
3020 return err;
3021}
3022
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003023static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003025 struct nfs_server *server = NFS_SERVER(dir);
3026 struct nfs_removeargs *args = msg->rpc_argp;
3027 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003029 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003031 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003034static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3035{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003036 nfs4_setup_sequence(NFS_SERVER(data->dir),
3037 &data->args.seq_args,
3038 &data->res.seq_res,
3039 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003042static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003044 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3045
Trond Myklebust14516c32010-07-31 14:29:06 -04003046 if (!nfs4_sequence_done(task, &res->seq_res))
3047 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003048 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003049 return 0;
3050 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003051 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Jeff Laytond3d41522010-09-17 17:31:57 -04003054static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3055{
3056 struct nfs_server *server = NFS_SERVER(dir);
3057 struct nfs_renameargs *arg = msg->rpc_argp;
3058 struct nfs_renameres *res = msg->rpc_resp;
3059
3060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003061 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003062 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003063}
3064
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003065static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3066{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003067 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3068 &data->args.seq_args,
3069 &data->res.seq_res,
3070 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003071}
3072
3073static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3074 struct inode *new_dir)
3075{
3076 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3077
3078 if (!nfs4_sequence_done(task, &res->seq_res))
3079 return 0;
3080 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3081 return 0;
3082
3083 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003084 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003085 return 1;
3086}
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3089 struct inode *new_dir, struct qstr *new_name)
3090{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003091 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003092 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 .old_dir = NFS_FH(old_dir),
3094 .new_dir = NFS_FH(new_dir),
3095 .old_name = old_name,
3096 .new_name = new_name,
3097 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003098 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003099 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 struct rpc_message msg = {
3102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3103 .rpc_argp = &arg,
3104 .rpc_resp = &res,
3105 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003106 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Bryan Schumaker7c513052011-03-24 17:12:24 +00003108 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (!status) {
3110 update_changeattr(old_dir, &res.old_cinfo);
3111 update_changeattr(new_dir, &res.new_cinfo);
3112 }
3113 return status;
3114}
3115
3116static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3117 struct inode *new_dir, struct qstr *new_name)
3118{
3119 struct nfs4_exception exception = { };
3120 int err;
3121 do {
3122 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3123 _nfs4_proc_rename(old_dir, old_name,
3124 new_dir, new_name),
3125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3131{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003132 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct nfs4_link_arg arg = {
3134 .fh = NFS_FH(inode),
3135 .dir_fh = NFS_FH(dir),
3136 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003137 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003139 struct nfs4_link_res res = {
3140 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003141 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct rpc_message msg = {
3143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3144 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003145 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003147 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Trond Myklebust136f2622010-04-16 16:22:49 -04003149 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003150 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003151 goto out;
3152
Bryan Schumaker7c513052011-03-24 17:12:24 +00003153 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003154 if (!status) {
3155 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003156 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003157 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003158out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003159 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 return status;
3161}
3162
3163static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3164{
3165 struct nfs4_exception exception = { };
3166 int err;
3167 do {
3168 err = nfs4_handle_exception(NFS_SERVER(inode),
3169 _nfs4_proc_link(inode, dir, name),
3170 &exception);
3171 } while (exception.retry);
3172 return err;
3173}
3174
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003175struct nfs4_createdata {
3176 struct rpc_message msg;
3177 struct nfs4_create_arg arg;
3178 struct nfs4_create_res res;
3179 struct nfs_fh fh;
3180 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003181};
3182
3183static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3184 struct qstr *name, struct iattr *sattr, u32 ftype)
3185{
3186 struct nfs4_createdata *data;
3187
3188 data = kzalloc(sizeof(*data), GFP_KERNEL);
3189 if (data != NULL) {
3190 struct nfs_server *server = NFS_SERVER(dir);
3191
3192 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3193 data->msg.rpc_argp = &data->arg;
3194 data->msg.rpc_resp = &data->res;
3195 data->arg.dir_fh = NFS_FH(dir);
3196 data->arg.server = server;
3197 data->arg.name = name;
3198 data->arg.attrs = sattr;
3199 data->arg.ftype = ftype;
3200 data->arg.bitmask = server->attr_bitmask;
3201 data->res.server = server;
3202 data->res.fh = &data->fh;
3203 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003204 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003205 }
3206 return data;
3207}
3208
3209static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3210{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003211 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003212 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003213 if (status == 0) {
3214 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003215 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3216 }
3217 return status;
3218}
3219
3220static void nfs4_free_createdata(struct nfs4_createdata *data)
3221{
3222 kfree(data);
3223}
3224
Chuck Lever4f390c12006-08-22 20:06:22 -04003225static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003226 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 struct nfs4_createdata *data;
3229 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Chuck Lever94a6d752006-08-22 20:06:23 -04003231 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003232 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003233
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003234 status = -ENOMEM;
3235 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3236 if (data == NULL)
3237 goto out;
3238
3239 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3240 data->arg.u.symlink.pages = &page;
3241 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243 status = nfs4_do_create(dir, dentry, data);
3244
3245 nfs4_free_createdata(data);
3246out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return status;
3248}
3249
Chuck Lever4f390c12006-08-22 20:06:22 -04003250static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003251 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct nfs4_exception exception = { };
3254 int err;
3255 do {
3256 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003257 _nfs4_proc_symlink(dir, dentry, page,
3258 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 &exception);
3260 } while (exception.retry);
3261 return err;
3262}
3263
3264static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3265 struct iattr *sattr)
3266{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 struct nfs4_createdata *data;
3268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3271 if (data == NULL)
3272 goto out;
3273
3274 status = nfs4_do_create(dir, dentry, data);
3275
3276 nfs4_free_createdata(data);
3277out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return status;
3279}
3280
3281static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3282 struct iattr *sattr)
3283{
3284 struct nfs4_exception exception = { };
3285 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003286
3287 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 do {
3289 err = nfs4_handle_exception(NFS_SERVER(dir),
3290 _nfs4_proc_mkdir(dir, dentry, sattr),
3291 &exception);
3292 } while (exception.retry);
3293 return err;
3294}
3295
3296static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003297 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 struct inode *dir = dentry->d_inode;
3300 struct nfs4_readdir_arg args = {
3301 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003302 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 .pgbase = 0,
3304 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003305 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003306 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 };
3308 struct nfs4_readdir_res res;
3309 struct rpc_message msg = {
3310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3311 .rpc_argp = &args,
3312 .rpc_resp = &res,
3313 .rpc_cred = cred,
3314 };
3315 int status;
3316
Harvey Harrison3110ff82008-05-02 13:42:44 -07003317 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003318 dentry->d_parent->d_name.name,
3319 dentry->d_name.name,
3320 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003321 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003323 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 -05003324 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003325 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003326 status += args.pgbase;
3327 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003328
3329 nfs_invalidate_atime(dir);
3330
Harvey Harrison3110ff82008-05-02 13:42:44 -07003331 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return status;
3333}
3334
3335static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003336 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
3338 struct nfs4_exception exception = { };
3339 int err;
3340 do {
3341 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3342 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003343 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 &exception);
3345 } while (exception.retry);
3346 return err;
3347}
3348
3349static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3350 struct iattr *sattr, dev_t rdev)
3351{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003352 struct nfs4_createdata *data;
3353 int mode = sattr->ia_mode;
3354 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003356 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3357 if (data == NULL)
3358 goto out;
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003361 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003363 data->arg.ftype = NF4BLK;
3364 data->arg.u.device.specdata1 = MAJOR(rdev);
3365 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003368 data->arg.ftype = NF4CHR;
3369 data->arg.u.device.specdata1 = MAJOR(rdev);
3370 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003371 } else if (!S_ISSOCK(mode)) {
3372 status = -EINVAL;
3373 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003376 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003377out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003378 nfs4_free_createdata(data);
3379out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return status;
3381}
3382
3383static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3384 struct iattr *sattr, dev_t rdev)
3385{
3386 struct nfs4_exception exception = { };
3387 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003388
3389 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 do {
3391 err = nfs4_handle_exception(NFS_SERVER(dir),
3392 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3393 &exception);
3394 } while (exception.retry);
3395 return err;
3396}
3397
3398static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3399 struct nfs_fsstat *fsstat)
3400{
3401 struct nfs4_statfs_arg args = {
3402 .fh = fhandle,
3403 .bitmask = server->attr_bitmask,
3404 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003405 struct nfs4_statfs_res res = {
3406 .fsstat = fsstat,
3407 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 struct rpc_message msg = {
3409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3410 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003411 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 };
3413
Trond Myklebust0e574af2005-10-27 22:12:38 -04003414 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003415 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3419{
3420 struct nfs4_exception exception = { };
3421 int err;
3422 do {
3423 err = nfs4_handle_exception(server,
3424 _nfs4_proc_statfs(server, fhandle, fsstat),
3425 &exception);
3426 } while (exception.retry);
3427 return err;
3428}
3429
3430static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3431 struct nfs_fsinfo *fsinfo)
3432{
3433 struct nfs4_fsinfo_arg args = {
3434 .fh = fhandle,
3435 .bitmask = server->attr_bitmask,
3436 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003437 struct nfs4_fsinfo_res res = {
3438 .fsinfo = fsinfo,
3439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 struct rpc_message msg = {
3441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3442 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003443 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 };
3445
Bryan Schumaker7c513052011-03-24 17:12:24 +00003446 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
3449static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3450{
3451 struct nfs4_exception exception = { };
3452 int err;
3453
3454 do {
3455 err = nfs4_handle_exception(server,
3456 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3457 &exception);
3458 } while (exception.retry);
3459 return err;
3460}
3461
3462static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3463{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003464 int error;
3465
Trond Myklebust0e574af2005-10-27 22:12:38 -04003466 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003467 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003468 if (error == 0) {
3469 /* block layout checks this! */
3470 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003471 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003472 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003473
3474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
3477static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3478 struct nfs_pathconf *pathconf)
3479{
3480 struct nfs4_pathconf_arg args = {
3481 .fh = fhandle,
3482 .bitmask = server->attr_bitmask,
3483 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003484 struct nfs4_pathconf_res res = {
3485 .pathconf = pathconf,
3486 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 struct rpc_message msg = {
3488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3489 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003490 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 };
3492
3493 /* None of the pathconf attributes are mandatory to implement */
3494 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3495 memset(pathconf, 0, sizeof(*pathconf));
3496 return 0;
3497 }
3498
Trond Myklebust0e574af2005-10-27 22:12:38 -04003499 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003500 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
3502
3503static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3504 struct nfs_pathconf *pathconf)
3505{
3506 struct nfs4_exception exception = { };
3507 int err;
3508
3509 do {
3510 err = nfs4_handle_exception(server,
3511 _nfs4_proc_pathconf(server, fhandle, pathconf),
3512 &exception);
3513 } while (exception.retry);
3514 return err;
3515}
3516
Trond Myklebust5521abf2013-03-16 20:54:34 -04003517int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003518 const struct nfs_open_context *ctx,
3519 const struct nfs_lock_context *l_ctx,
3520 fmode_t fmode)
3521{
3522 const struct nfs_lockowner *lockowner = NULL;
3523
3524 if (l_ctx != NULL)
3525 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003526 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003527}
3528EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3529
Trond Myklebust5521abf2013-03-16 20:54:34 -04003530static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3531 const struct nfs_open_context *ctx,
3532 const struct nfs_lock_context *l_ctx,
3533 fmode_t fmode)
3534{
3535 nfs4_stateid current_stateid;
3536
3537 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3538 return false;
3539 return nfs4_stateid_match(stateid, &current_stateid);
3540}
3541
3542static bool nfs4_error_stateid_expired(int err)
3543{
3544 switch (err) {
3545 case -NFS4ERR_DELEG_REVOKED:
3546 case -NFS4ERR_ADMIN_REVOKED:
3547 case -NFS4ERR_BAD_STATEID:
3548 case -NFS4ERR_STALE_STATEID:
3549 case -NFS4ERR_OLD_STATEID:
3550 case -NFS4ERR_OPENMODE:
3551 case -NFS4ERR_EXPIRED:
3552 return true;
3553 }
3554 return false;
3555}
3556
Benny Halevyd20581a2011-05-22 19:52:03 +03003557void __nfs4_read_done_cb(struct nfs_read_data *data)
3558{
Fred Isamancd841602012-04-20 14:47:44 -04003559 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003560}
3561
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003562static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Fred Isamancd841602012-04-20 14:47:44 -04003564 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003566 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003567 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003568 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003570
Benny Halevyd20581a2011-05-22 19:52:03 +03003571 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003573 renew_lease(server, data->timestamp);
3574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Trond Myklebust5521abf2013-03-16 20:54:34 -04003577static bool nfs4_read_stateid_changed(struct rpc_task *task,
3578 struct nfs_readargs *args)
3579{
3580
3581 if (!nfs4_error_stateid_expired(task->tk_status) ||
3582 nfs4_stateid_is_current(&args->stateid,
3583 args->context,
3584 args->lock_context,
3585 FMODE_READ))
3586 return false;
3587 rpc_restart_call_prepare(task);
3588 return true;
3589}
3590
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003591static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3592{
3593
3594 dprintk("--> %s\n", __func__);
3595
3596 if (!nfs4_sequence_done(task, &data->res.seq_res))
3597 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003598 if (nfs4_read_stateid_changed(task, &data->args))
3599 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003600 return data->read_done_cb ? data->read_done_cb(task, data) :
3601 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003602}
3603
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003604static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003607 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003609 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003612static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3613{
Trond Myklebust9b206142013-03-17 15:52:00 -04003614 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003615 &data->args.seq_args,
3616 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003617 task))
3618 return;
3619 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3620 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Fred Isamanb029bc92011-03-03 15:13:42 +00003623static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Fred Isamancd841602012-04-20 14:47:44 -04003625 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003627 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003628 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003629 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003631 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003633 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003634 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
3637
Trond Myklebust5521abf2013-03-16 20:54:34 -04003638static bool nfs4_write_stateid_changed(struct rpc_task *task,
3639 struct nfs_writeargs *args)
3640{
3641
3642 if (!nfs4_error_stateid_expired(task->tk_status) ||
3643 nfs4_stateid_is_current(&args->stateid,
3644 args->context,
3645 args->lock_context,
3646 FMODE_WRITE))
3647 return false;
3648 rpc_restart_call_prepare(task);
3649 return true;
3650}
3651
Fred Isamanb029bc92011-03-03 15:13:42 +00003652static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3653{
3654 if (!nfs4_sequence_done(task, &data->res.seq_res))
3655 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003656 if (nfs4_write_stateid_changed(task, &data->args))
3657 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003658 return data->write_done_cb ? data->write_done_cb(task, data) :
3659 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003660}
3661
Trond Myklebust5a37f852012-04-28 14:55:16 -04003662static
3663bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003664{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003665 const struct nfs_pgio_header *hdr = data->header;
3666
3667 /* Don't request attributes for pNFS or O_DIRECT writes */
3668 if (data->ds_clp != NULL || hdr->dreq != NULL)
3669 return false;
3670 /* Otherwise, request attributes if and only if we don't hold
3671 * a delegation
3672 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003673 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003674}
Fred Isamana69aef12011-03-03 15:13:47 +00003675
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003676static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Fred Isamancd841602012-04-20 14:47:44 -04003678 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003679
Trond Myklebust5a37f852012-04-28 14:55:16 -04003680 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003681 data->args.bitmask = NULL;
3682 data->res.fattr = NULL;
3683 } else
3684 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003685
Fred Isamanb029bc92011-03-03 15:13:42 +00003686 if (!data->write_done_cb)
3687 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003688 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 data->timestamp = jiffies;
3690
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003691 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003692 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003695static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3696{
Trond Myklebust9b206142013-03-17 15:52:00 -04003697 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003698 &data->args.seq_args,
3699 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003700 task))
3701 return;
3702 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3703 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
Fred Isaman0b7c0152012-04-20 14:47:39 -04003706static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3707{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003708 nfs4_setup_sequence(NFS_SERVER(data->inode),
3709 &data->args.seq_args,
3710 &data->res.seq_res,
3711 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
Fred Isaman0b7c0152012-04-20 14:47:39 -04003714static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003717
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003718 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003719 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003720 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
Fred Isaman0b7c0152012-04-20 14:47:39 -04003725static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003726{
3727 if (!nfs4_sequence_done(task, &data->res.seq_res))
3728 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003729 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003730}
3731
Fred Isaman0b7c0152012-04-20 14:47:39 -04003732static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003734 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003735
Fred Isaman0b7c0152012-04-20 14:47:39 -04003736 if (data->commit_done_cb == NULL)
3737 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003738 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003740 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003743struct nfs4_renewdata {
3744 struct nfs_client *client;
3745 unsigned long timestamp;
3746};
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748/*
3749 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3750 * standalone procedure for queueing an asynchronous RENEW.
3751 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003752static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003753{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003754 struct nfs4_renewdata *data = calldata;
3755 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003756
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003757 if (atomic_read(&clp->cl_count) > 1)
3758 nfs4_schedule_state_renewal(clp);
3759 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003760 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003761}
3762
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003763static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003765 struct nfs4_renewdata *data = calldata;
3766 struct nfs_client *clp = data->client;
3767 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003770 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003771 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3772 return;
3773 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003774 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003775 return;
3776 }
3777 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003779 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003782static const struct rpc_call_ops nfs4_renew_ops = {
3783 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003784 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003785};
3786
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003787static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 struct rpc_message msg = {
3790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3791 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003792 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003794 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003796 if (renew_flags == 0)
3797 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003798 if (!atomic_inc_not_zero(&clp->cl_count))
3799 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003800 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003801 if (data == NULL)
3802 return -ENOMEM;
3803 data->client = clp;
3804 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003806 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003809static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3813 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003814 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 };
3816 unsigned long now = jiffies;
3817 int status;
3818
3819 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3820 if (status < 0)
3821 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003822 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return 0;
3824}
3825
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003826static inline int nfs4_server_supports_acls(struct nfs_server *server)
3827{
3828 return (server->caps & NFS_CAP_ACLS)
3829 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3830 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3831}
3832
Trond Myklebust21f498c2012-08-24 10:59:25 -04003833/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3834 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003835 * the stack.
3836 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003837#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003838
Neil Hormane9e3d722011-03-04 19:26:03 -05003839static int buf_to_pages_noslab(const void *buf, size_t buflen,
3840 struct page **pages, unsigned int *pgbase)
3841{
3842 struct page *newpage, **spages;
3843 int rc = 0;
3844 size_t len;
3845 spages = pages;
3846
3847 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003848 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003849 newpage = alloc_page(GFP_KERNEL);
3850
3851 if (newpage == NULL)
3852 goto unwind;
3853 memcpy(page_address(newpage), buf, len);
3854 buf += len;
3855 buflen -= len;
3856 *pages++ = newpage;
3857 rc++;
3858 } while (buflen != 0);
3859
3860 return rc;
3861
3862unwind:
3863 for(; rc > 0; rc--)
3864 __free_page(spages[rc-1]);
3865 return -ENOMEM;
3866}
3867
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003868struct nfs4_cached_acl {
3869 int cached;
3870 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003871 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003872};
3873
3874static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003875{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003876 struct nfs_inode *nfsi = NFS_I(inode);
3877
3878 spin_lock(&inode->i_lock);
3879 kfree(nfsi->nfs4_acl);
3880 nfsi->nfs4_acl = acl;
3881 spin_unlock(&inode->i_lock);
3882}
3883
3884static void nfs4_zap_acl_attr(struct inode *inode)
3885{
3886 nfs4_set_cached_acl(inode, NULL);
3887}
3888
3889static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3890{
3891 struct nfs_inode *nfsi = NFS_I(inode);
3892 struct nfs4_cached_acl *acl;
3893 int ret = -ENOENT;
3894
3895 spin_lock(&inode->i_lock);
3896 acl = nfsi->nfs4_acl;
3897 if (acl == NULL)
3898 goto out;
3899 if (buf == NULL) /* user is just asking for length */
3900 goto out_len;
3901 if (acl->cached == 0)
3902 goto out;
3903 ret = -ERANGE; /* see getxattr(2) man page */
3904 if (acl->len > buflen)
3905 goto out;
3906 memcpy(buf, acl->data, acl->len);
3907out_len:
3908 ret = acl->len;
3909out:
3910 spin_unlock(&inode->i_lock);
3911 return ret;
3912}
3913
Sachin Prabhu5794d212012-04-17 14:36:40 +01003914static 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 +00003915{
3916 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003917 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003918
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003919 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003920 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003921 if (acl == NULL)
3922 goto out;
3923 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003924 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003925 } else {
3926 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3927 if (acl == NULL)
3928 goto out;
3929 acl->cached = 0;
3930 }
3931 acl->len = acl_len;
3932out:
3933 nfs4_set_cached_acl(inode, acl);
3934}
3935
Andy Adamsonbf118a32011-12-07 11:55:27 -05003936/*
3937 * The getxattr API returns the required buffer length when called with a
3938 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3939 * the required buf. On a NULL buf, we send a page of data to the server
3940 * guessing that the ACL request can be serviced by a page. If so, we cache
3941 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3942 * the cache. If not so, we throw away the page, and cache the required
3943 * length. The next getxattr call will then produce another round trip to
3944 * the server, this time with the input buf of the required size.
3945 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003946static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003947{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003948 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003949 struct nfs_getaclargs args = {
3950 .fh = NFS_FH(inode),
3951 .acl_pages = pages,
3952 .acl_len = buflen,
3953 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003954 struct nfs_getaclres res = {
3955 .acl_len = buflen,
3956 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3959 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003960 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003961 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003962 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3963 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003964
Andy Adamsonbf118a32011-12-07 11:55:27 -05003965 /* As long as we're doing a round trip to the server anyway,
3966 * let's be prepared for a page of acl data. */
3967 if (npages == 0)
3968 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003969 if (npages > ARRAY_SIZE(pages))
3970 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003971
Andy Adamsonbf118a32011-12-07 11:55:27 -05003972 for (i = 0; i < npages; i++) {
3973 pages[i] = alloc_page(GFP_KERNEL);
3974 if (!pages[i])
3975 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003976 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003977
3978 /* for decoding across pages */
3979 res.acl_scratch = alloc_page(GFP_KERNEL);
3980 if (!res.acl_scratch)
3981 goto out_free;
3982
Andy Adamsonbf118a32011-12-07 11:55:27 -05003983 args.acl_len = npages * PAGE_SIZE;
3984 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003985
Peng Taode040be2012-01-10 22:42:47 +08003986 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003987 __func__, buf, buflen, npages, args.acl_len);
3988 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3989 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003990 if (ret)
3991 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003992
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003993 /* Handle the case where the passed-in buffer is too short */
3994 if (res.acl_flags & NFS4_ACL_TRUNC) {
3995 /* Did the user only issue a request for the acl length? */
3996 if (buf == NULL)
3997 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003998 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003999 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004000 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004001 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004002 if (buf) {
4003 if (res.acl_len > buflen) {
4004 ret = -ERANGE;
4005 goto out_free;
4006 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004007 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004008 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004009out_ok:
4010 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004011out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004012 for (i = 0; i < npages; i++)
4013 if (pages[i])
4014 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004015 if (res.acl_scratch)
4016 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004017 return ret;
4018}
4019
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004020static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4021{
4022 struct nfs4_exception exception = { };
4023 ssize_t ret;
4024 do {
4025 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4026 if (ret >= 0)
4027 break;
4028 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4029 } while (exception.retry);
4030 return ret;
4031}
4032
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004033static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4034{
4035 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004036 int ret;
4037
4038 if (!nfs4_server_supports_acls(server))
4039 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004040 ret = nfs_revalidate_inode(server, inode);
4041 if (ret < 0)
4042 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004043 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4044 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004045 ret = nfs4_read_cached_acl(inode, buf, buflen);
4046 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004047 /* -ENOENT is returned if there is no ACL or if there is an ACL
4048 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004049 return ret;
4050 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004051}
4052
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004053static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004054{
4055 struct nfs_server *server = NFS_SERVER(inode);
4056 struct page *pages[NFS4ACL_MAXPAGES];
4057 struct nfs_setaclargs arg = {
4058 .fh = NFS_FH(inode),
4059 .acl_pages = pages,
4060 .acl_len = buflen,
4061 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004062 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4065 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004066 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004067 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004068 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004069 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004070
4071 if (!nfs4_server_supports_acls(server))
4072 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004073 if (npages > ARRAY_SIZE(pages))
4074 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004075 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4076 if (i < 0)
4077 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004078 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004079 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004080
4081 /*
4082 * Free each page after tx, so the only ref left is
4083 * held by the network stack
4084 */
4085 for (; i > 0; i--)
4086 put_page(pages[i-1]);
4087
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004088 /*
4089 * Acl update can result in inode attribute update.
4090 * so mark the attribute cache invalid.
4091 */
4092 spin_lock(&inode->i_lock);
4093 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4094 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004095 nfs_access_zap_cache(inode);
4096 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004097 return ret;
4098}
4099
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004100static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4101{
4102 struct nfs4_exception exception = { };
4103 int err;
4104 do {
4105 err = nfs4_handle_exception(NFS_SERVER(inode),
4106 __nfs4_proc_set_acl(inode, buf, buflen),
4107 &exception);
4108 } while (exception.retry);
4109 return err;
4110}
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004113nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004115 struct nfs_client *clp = server->nfs_client;
4116
4117 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return 0;
4119 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004120 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004121 case -NFS4ERR_ADMIN_REVOKED:
4122 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004123 if (state == NULL)
4124 break;
4125 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004126 case -NFS4ERR_OPENMODE:
4127 if (state == NULL)
4128 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004129 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4130 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004131 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004132 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004133 if (state != NULL) {
4134 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4135 goto stateid_invalid;
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004138 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004139 nfs4_schedule_lease_recovery(clp);
4140 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004141#if defined(CONFIG_NFS_V4_1)
4142 case -NFS4ERR_BADSESSION:
4143 case -NFS4ERR_BADSLOT:
4144 case -NFS4ERR_BAD_HIGH_SLOT:
4145 case -NFS4ERR_DEADSESSION:
4146 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4147 case -NFS4ERR_SEQ_FALSE_RETRY:
4148 case -NFS4ERR_SEQ_MISORDERED:
4149 dprintk("%s ERROR %d, Reset session\n", __func__,
4150 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004151 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004152 task->tk_status = 0;
4153 return -EAGAIN;
4154#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004156 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004157 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4159 task->tk_status = 0;
4160 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004161 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 case -NFS4ERR_OLD_STATEID:
4163 task->tk_status = 0;
4164 return -EAGAIN;
4165 }
4166 task->tk_status = nfs4_map_errors(task->tk_status);
4167 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004168stateid_invalid:
4169 task->tk_status = -EIO;
4170 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004171wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004172 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004173 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4174 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4175 task->tk_status = 0;
4176 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Chuck Leverf0920752012-05-21 22:45:41 -04004179static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4180 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004181{
4182 __be32 verf[2];
4183
Chuck Lever2c820d92012-05-21 22:45:33 -04004184 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4185 /* An impossible timestamp guarantees this value
4186 * will never match a generated boot time. */
4187 verf[0] = 0;
4188 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4189 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004190 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4191 verf[0] = (__be32)nn->boot_time.tv_sec;
4192 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004193 }
Chuck Levercd937102012-03-02 17:14:31 -05004194 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4195}
4196
Chuck Levere984a552012-09-14 17:24:21 -04004197static unsigned int
4198nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4199 char *buf, size_t len)
4200{
4201 unsigned int result;
4202
4203 rcu_read_lock();
4204 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4205 clp->cl_ipaddr,
4206 rpc_peeraddr2str(clp->cl_rpcclient,
4207 RPC_DISPLAY_ADDR),
4208 rpc_peeraddr2str(clp->cl_rpcclient,
4209 RPC_DISPLAY_PROTO));
4210 rcu_read_unlock();
4211 return result;
4212}
4213
4214static unsigned int
4215nfs4_init_uniform_client_string(const struct nfs_client *clp,
4216 char *buf, size_t len)
4217{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004218 char *nodename = clp->cl_rpcclient->cl_nodename;
4219
4220 if (nfs4_client_id_uniquifier[0] != '\0')
4221 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004222 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4223 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004224 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004225}
4226
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004227/**
4228 * nfs4_proc_setclientid - Negotiate client ID
4229 * @clp: state data structure
4230 * @program: RPC program for NFSv4 callback service
4231 * @port: IP port number for NFS4 callback service
4232 * @cred: RPC credential to use for this call
4233 * @res: where to place the result
4234 *
4235 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4236 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004237int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4238 unsigned short port, struct rpc_cred *cred,
4239 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 nfs4_verifier sc_verifier;
4242 struct nfs4_setclientid setclientid = {
4243 .sc_verifier = &sc_verifier,
4244 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004245 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 };
4247 struct rpc_message msg = {
4248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4249 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004250 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004251 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004253 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Chuck Leverde734832012-07-11 16:30:50 -04004255 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004256 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004257 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4258 setclientid.sc_name_len =
4259 nfs4_init_uniform_client_string(clp,
4260 setclientid.sc_name,
4261 sizeof(setclientid.sc_name));
4262 else
4263 setclientid.sc_name_len =
4264 nfs4_init_nonuniform_client_string(clp,
4265 setclientid.sc_name,
4266 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004267 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004268 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004269 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004270 sizeof(setclientid.sc_netid),
4271 rpc_peeraddr2str(clp->cl_rpcclient,
4272 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004273 rcu_read_unlock();
4274 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004275 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 clp->cl_ipaddr, port >> 8, port & 255);
4277
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004278 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4279 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4280 setclientid.sc_name_len, setclientid.sc_name);
4281 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4282 dprintk("NFS reply setclientid: %d\n", status);
4283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004286/**
4287 * nfs4_proc_setclientid_confirm - Confirm client ID
4288 * @clp: state data structure
4289 * @res: result of a previous SETCLIENTID
4290 * @cred: RPC credential to use for this call
4291 *
4292 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4293 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004294int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004295 struct nfs4_setclientid_res *arg,
4296 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 struct nfs_fsinfo fsinfo;
4299 struct rpc_message msg = {
4300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004301 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004303 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 };
4305 unsigned long now;
4306 int status;
4307
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004308 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4309 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4310 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004312 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (status == 0) {
4314 spin_lock(&clp->cl_lock);
4315 clp->cl_lease_time = fsinfo.lease_time * HZ;
4316 clp->cl_last_renewal = now;
4317 spin_unlock(&clp->cl_lock);
4318 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004319 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return status;
4321}
4322
Trond Myklebustfe650402006-01-03 09:55:18 +01004323struct nfs4_delegreturndata {
4324 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004325 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004326 struct nfs_fh fh;
4327 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004328 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004329 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004330 int rpc_status;
4331};
4332
Trond Myklebustfe650402006-01-03 09:55:18 +01004333static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4334{
4335 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004336
Trond Myklebust14516c32010-07-31 14:29:06 -04004337 if (!nfs4_sequence_done(task, &data->res.seq_res))
4338 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004339
Ricardo Labiaga79708862009-12-07 09:23:21 -05004340 switch (task->tk_status) {
4341 case -NFS4ERR_STALE_STATEID:
4342 case -NFS4ERR_EXPIRED:
4343 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004344 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004345 break;
4346 default:
4347 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4348 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004349 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004350 return;
4351 }
4352 }
4353 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004354}
4355
4356static void nfs4_delegreturn_release(void *calldata)
4357{
Trond Myklebustfe650402006-01-03 09:55:18 +01004358 kfree(calldata);
4359}
4360
Andy Adamson938e1012009-04-01 09:22:28 -04004361#if defined(CONFIG_NFS_V4_1)
4362static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4363{
4364 struct nfs4_delegreturndata *d_data;
4365
4366 d_data = (struct nfs4_delegreturndata *)data;
4367
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004368 nfs4_setup_sequence(d_data->res.server,
4369 &d_data->args.seq_args,
4370 &d_data->res.seq_res,
4371 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004372}
4373#endif /* CONFIG_NFS_V4_1 */
4374
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004375static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004376#if defined(CONFIG_NFS_V4_1)
4377 .rpc_call_prepare = nfs4_delegreturn_prepare,
4378#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004379 .rpc_call_done = nfs4_delegreturn_done,
4380 .rpc_release = nfs4_delegreturn_release,
4381};
4382
Trond Myklebuste6f81072008-01-24 18:14:34 -05004383static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004384{
4385 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004386 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004387 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004388 struct rpc_message msg = {
4389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4390 .rpc_cred = cred,
4391 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004392 struct rpc_task_setup task_setup_data = {
4393 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004394 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004395 .callback_ops = &nfs4_delegreturn_ops,
4396 .flags = RPC_TASK_ASYNC,
4397 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004398 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004399
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004400 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004401 if (data == NULL)
4402 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004403 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004404 data->args.fhandle = &data->fh;
4405 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004406 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004407 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004408 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004409 data->res.fattr = &data->fattr;
4410 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004411 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004412 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004413 data->rpc_status = 0;
4414
Trond Myklebustc970aa82007-07-14 15:39:59 -04004415 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004416 msg.rpc_argp = &data->args;
4417 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004418 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004419 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004420 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004421 if (!issync)
4422 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004423 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004424 if (status != 0)
4425 goto out;
4426 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004427 if (status == 0)
4428 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4429 else
4430 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004431out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004432 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Trond Myklebuste6f81072008-01-24 18:14:34 -05004436int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 struct nfs_server *server = NFS_SERVER(inode);
4439 struct nfs4_exception exception = { };
4440 int err;
4441 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004442 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 switch (err) {
4444 case -NFS4ERR_STALE_STATEID:
4445 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 case 0:
4447 return 0;
4448 }
4449 err = nfs4_handle_exception(server, err, &exception);
4450 } while (exception.retry);
4451 return err;
4452}
4453
4454#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4455#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4456
4457/*
4458 * sleep, with exponential backoff, and retry the LOCK operation.
4459 */
4460static unsigned long
4461nfs4_set_lock_task_retry(unsigned long timeout)
4462{
Jeff Laytond3103102011-12-01 22:44:39 +01004463 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 timeout <<= 1;
4465 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4466 return NFS4_LOCK_MAXTIMEOUT;
4467 return timeout;
4468}
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4471{
4472 struct inode *inode = state->inode;
4473 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004474 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004475 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004477 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004479 struct nfs_lockt_res res = {
4480 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 };
4482 struct rpc_message msg = {
4483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4484 .rpc_argp = &arg,
4485 .rpc_resp = &res,
4486 .rpc_cred = state->owner->so_cred,
4487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 struct nfs4_lock_state *lsp;
4489 int status;
4490
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004491 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004492 status = nfs4_set_lock_state(state, request);
4493 if (status != 0)
4494 goto out;
4495 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004496 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004497 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004498 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004499 switch (status) {
4500 case 0:
4501 request->fl_type = F_UNLCK;
4502 break;
4503 case -NFS4ERR_DENIED:
4504 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004506 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004507out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return status;
4509}
4510
4511static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4512{
4513 struct nfs4_exception exception = { };
4514 int err;
4515
4516 do {
4517 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4518 _nfs4_proc_getlk(state, cmd, request),
4519 &exception);
4520 } while (exception.retry);
4521 return err;
4522}
4523
4524static int do_vfs_lock(struct file *file, struct file_lock *fl)
4525{
4526 int res = 0;
4527 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4528 case FL_POSIX:
4529 res = posix_lock_file_wait(file, fl);
4530 break;
4531 case FL_FLOCK:
4532 res = flock_lock_file_wait(file, fl);
4533 break;
4534 default:
4535 BUG();
4536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return res;
4538}
4539
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004540struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004541 struct nfs_locku_args arg;
4542 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004543 struct nfs4_lock_state *lsp;
4544 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004545 struct file_lock fl;
4546 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004547 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004548};
4549
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004550static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4551 struct nfs_open_context *ctx,
4552 struct nfs4_lock_state *lsp,
4553 struct nfs_seqid *seqid)
4554{
4555 struct nfs4_unlockdata *p;
4556 struct inode *inode = lsp->ls_state->inode;
4557
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004558 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004559 if (p == NULL)
4560 return NULL;
4561 p->arg.fh = NFS_FH(inode);
4562 p->arg.fl = &p->fl;
4563 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004564 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004565 p->arg.stateid = &lsp->ls_stateid;
4566 p->lsp = lsp;
4567 atomic_inc(&lsp->ls_count);
4568 /* Ensure we don't close file until we're done freeing locks! */
4569 p->ctx = get_nfs_open_context(ctx);
4570 memcpy(&p->fl, fl, sizeof(p->fl));
4571 p->server = NFS_SERVER(inode);
4572 return p;
4573}
4574
Trond Myklebust06f814a2006-01-03 09:55:07 +01004575static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004576{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004577 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004578 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004579 nfs4_put_lock_state(calldata->lsp);
4580 put_nfs_open_context(calldata->ctx);
4581 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004582}
4583
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004584static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004585{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004586 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004587
Trond Myklebust14516c32010-07-31 14:29:06 -04004588 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4589 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004590 switch (task->tk_status) {
4591 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004592 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4593 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004594 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004595 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004596 case -NFS4ERR_BAD_STATEID:
4597 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004598 case -NFS4ERR_STALE_STATEID:
4599 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600 break;
4601 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004602 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004603 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004605 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004606}
4607
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004608static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004609{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004610 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004612 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004613 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004614 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004615 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004616 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004617 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004618 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004619 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004620 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004621 &calldata->res.seq_res,
4622 task) != 0)
4623 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004624 return;
4625out_no_action:
4626 task->tk_action = NULL;
4627out_wait:
4628 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004631static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004632 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004633 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004634 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004635};
4636
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004637static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4638 struct nfs_open_context *ctx,
4639 struct nfs4_lock_state *lsp,
4640 struct nfs_seqid *seqid)
4641{
4642 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004643 struct rpc_message msg = {
4644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4645 .rpc_cred = ctx->cred,
4646 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004647 struct rpc_task_setup task_setup_data = {
4648 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004649 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004650 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004651 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004652 .flags = RPC_TASK_ASYNC,
4653 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004654
Frank Filz137d6ac2007-07-09 15:32:29 -07004655 /* Ensure this is an unlock - when canceling a lock, the
4656 * canceled lock is passed in, and it won't be an unlock.
4657 */
4658 fl->fl_type = F_UNLCK;
4659
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004660 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4661 if (data == NULL) {
4662 nfs_free_seqid(seqid);
4663 return ERR_PTR(-ENOMEM);
4664 }
4665
Trond Myklebust9d12b212012-01-17 22:04:25 -05004666 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004667 msg.rpc_argp = &data->arg;
4668 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004669 task_setup_data.callback_data = data;
4670 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004671}
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4674{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004675 struct inode *inode = state->inode;
4676 struct nfs4_state_owner *sp = state->owner;
4677 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004678 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004679 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004680 struct rpc_task *task;
4681 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004682 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Trond Myklebust9b073572006-06-29 16:38:34 -04004684 status = nfs4_set_lock_state(state, request);
4685 /* Unlock _before_ we do the RPC call */
4686 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004687 /* Exclude nfs_delegation_claim_locks() */
4688 mutex_lock(&sp->so_delegreturn_mutex);
4689 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004690 down_read(&nfsi->rwsem);
4691 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4692 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004693 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004694 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004695 }
4696 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004697 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004698 if (status != 0)
4699 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004700 /* Is this a delegated lock? */
4701 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004702 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004703 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004704 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004705 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004706 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004707 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004708 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004709 status = PTR_ERR(task);
4710 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004711 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004712 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004713 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004714out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004715 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
4718
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719struct nfs4_lockdata {
4720 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004721 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722 struct nfs4_lock_state *lsp;
4723 struct nfs_open_context *ctx;
4724 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004725 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726 int rpc_status;
4727 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004728 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004729};
4730
4731static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004732 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4733 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004734{
4735 struct nfs4_lockdata *p;
4736 struct inode *inode = lsp->ls_state->inode;
4737 struct nfs_server *server = NFS_SERVER(inode);
4738
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004739 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004740 if (p == NULL)
4741 return NULL;
4742
4743 p->arg.fh = NFS_FH(inode);
4744 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004745 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004746 if (p->arg.open_seqid == NULL)
4747 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004748 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004749 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004750 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004752 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004753 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004754 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004755 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004757 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758 atomic_inc(&lsp->ls_count);
4759 p->ctx = get_nfs_open_context(ctx);
4760 memcpy(&p->fl, fl, sizeof(p->fl));
4761 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004762out_free_seqid:
4763 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764out_free:
4765 kfree(p);
4766 return NULL;
4767}
4768
4769static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4770{
4771 struct nfs4_lockdata *data = calldata;
4772 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Harvey Harrison3110ff82008-05-02 13:42:44 -07004774 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004775 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004776 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004777 /* Do we need to do an open_to_lock_owner? */
4778 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004779 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004780 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004781 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004782 data->arg.open_stateid = &state->stateid;
4783 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004784 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004785 } else
4786 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004787 if (!nfs4_valid_open_stateid(state)) {
4788 data->rpc_status = -EBADF;
4789 task->tk_action = NULL;
4790 goto out_release_open_seqid;
4791 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004792 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004793 if (nfs4_setup_sequence(data->server,
4794 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004795 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004796 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004797 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004798out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004799 nfs_release_seqid(data->arg.open_seqid);
4800out_release_lock_seqid:
4801 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004802out_wait:
4803 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004804 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004805}
4806
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004807static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4808{
4809 struct nfs4_lockdata *data = calldata;
4810
Harvey Harrison3110ff82008-05-02 13:42:44 -07004811 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812
Trond Myklebust14516c32010-07-31 14:29:06 -04004813 if (!nfs4_sequence_done(task, &data->res.seq_res))
4814 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004815
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004816 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004817 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 if (data->rpc_status == 0)
4819 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4820 else
4821 goto out;
4822 }
4823 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004824 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004825 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004826 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004827 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004828out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004829 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004830}
4831
4832static void nfs4_lock_release(void *calldata)
4833{
4834 struct nfs4_lockdata *data = calldata;
4835
Harvey Harrison3110ff82008-05-02 13:42:44 -07004836 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004837 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004838 if (data->cancelled != 0) {
4839 struct rpc_task *task;
4840 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4841 data->arg.lock_seqid);
4842 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004843 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004844 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845 } else
4846 nfs_free_seqid(data->arg.lock_seqid);
4847 nfs4_put_lock_state(data->lsp);
4848 put_nfs_open_context(data->ctx);
4849 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004850 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851}
4852
4853static const struct rpc_call_ops nfs4_lock_ops = {
4854 .rpc_call_prepare = nfs4_lock_prepare,
4855 .rpc_call_done = nfs4_lock_done,
4856 .rpc_release = nfs4_lock_release,
4857};
4858
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004859static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4860{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004861 switch (error) {
4862 case -NFS4ERR_ADMIN_REVOKED:
4863 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004864 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004865 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004866 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004867 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004868 break;
4869 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004870 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004871 case -NFS4ERR_EXPIRED:
4872 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004873 };
4874}
4875
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004876static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004877{
4878 struct nfs4_lockdata *data;
4879 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4882 .rpc_cred = state->owner->so_cred,
4883 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004884 struct rpc_task_setup task_setup_data = {
4885 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004886 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004887 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004888 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004889 .flags = RPC_TASK_ASYNC,
4890 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891 int ret;
4892
Harvey Harrison3110ff82008-05-02 13:42:44 -07004893 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004894 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004895 fl->fl_u.nfs4_fl.owner,
4896 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004897 if (data == NULL)
4898 return -ENOMEM;
4899 if (IS_SETLKW(cmd))
4900 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004901 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004902 msg.rpc_argp = &data->arg;
4903 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004904 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004905 if (recovery_type > NFS_LOCK_NEW) {
4906 if (recovery_type == NFS_LOCK_RECLAIM)
4907 data->arg.reclaim = NFS_LOCK_RECLAIM;
4908 nfs4_set_sequence_privileged(&data->arg.seq_args);
4909 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004910 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004911 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004912 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004913 ret = nfs4_wait_for_completion_rpc_task(task);
4914 if (ret == 0) {
4915 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004916 if (ret)
4917 nfs4_handle_setlk_error(data->server, data->lsp,
4918 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004919 } else
4920 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004921 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004922 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
4926static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4927{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004928 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004929 struct nfs4_exception exception = {
4930 .inode = state->inode,
4931 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004932 int err;
4933
4934 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004935 /* Cache the lock if possible... */
4936 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4937 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004938 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004939 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004940 break;
4941 nfs4_handle_exception(server, err, &exception);
4942 } while (exception.retry);
4943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
4946static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4947{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004948 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004949 struct nfs4_exception exception = {
4950 .inode = state->inode,
4951 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004952 int err;
4953
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004954 err = nfs4_set_lock_state(state, request);
4955 if (err != 0)
4956 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004957 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004958 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4959 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004960 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004961 switch (err) {
4962 default:
4963 goto out;
4964 case -NFS4ERR_GRACE:
4965 case -NFS4ERR_DELAY:
4966 nfs4_handle_exception(server, err, &exception);
4967 err = 0;
4968 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004969 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004970out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004971 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972}
4973
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004974#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004975/**
4976 * nfs41_check_expired_locks - possibly free a lock stateid
4977 *
4978 * @state: NFSv4 state for an inode
4979 *
4980 * Returns NFS_OK if recovery for this stateid is now finished.
4981 * Otherwise a negative NFS4ERR value is returned.
4982 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004983static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004984{
Chuck Levereb64cf92012-07-11 16:30:05 -04004985 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004986 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004987 struct nfs_server *server = NFS_SERVER(state->inode);
4988
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004989 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004990 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004991 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4992 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004993 /* Free the stateid unless the server
4994 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004995 if (status != -NFS4ERR_BAD_STATEID)
4996 nfs41_free_stateid(server,
4997 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004998 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004999 ret = status;
5000 }
5001 }
5002 };
5003
5004 return ret;
5005}
5006
5007static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5008{
5009 int status = NFS_OK;
5010
5011 if (test_bit(LK_STATE_IN_USE, &state->flags))
5012 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005013 if (status != NFS_OK)
5014 status = nfs4_lock_expired(state, request);
5015 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005016}
5017#endif
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5020{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005021 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005022 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005023 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005024 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005025 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005027 if ((fl_flags & FL_POSIX) &&
5028 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5029 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005030 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005031 status = nfs4_set_lock_state(state, request);
5032 if (status != 0)
5033 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005034 request->fl_flags |= FL_ACCESS;
5035 status = do_vfs_lock(request->fl_file, request);
5036 if (status < 0)
5037 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005038 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005039 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005040 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005041 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005042 request->fl_flags = fl_flags & ~FL_SLEEP;
5043 status = do_vfs_lock(request->fl_file, request);
5044 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005045 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005046 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5047 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005048 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005049 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005050 goto out;
5051 down_read(&nfsi->rwsem);
5052 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5053 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005054 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005055 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005056 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005057 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005058 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005059 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5060 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005061out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005062 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005063out:
5064 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return status;
5066}
5067
5068static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5069{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005070 struct nfs4_exception exception = {
5071 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005072 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005073 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 int err;
5075
5076 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005077 err = _nfs4_proc_setlk(state, cmd, request);
5078 if (err == -NFS4ERR_DENIED)
5079 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005081 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 } while (exception.retry);
5083 return err;
5084}
5085
5086static int
5087nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5088{
5089 struct nfs_open_context *ctx;
5090 struct nfs4_state *state;
5091 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5092 int status;
5093
5094 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005095 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 state = ctx->state;
5097
5098 if (request->fl_start < 0 || request->fl_end < 0)
5099 return -EINVAL;
5100
Trond Myklebustd9531262009-07-21 19:22:38 -04005101 if (IS_GETLK(cmd)) {
5102 if (state != NULL)
5103 return nfs4_proc_getlk(state, F_GETLK, request);
5104 return 0;
5105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
5107 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5108 return -EINVAL;
5109
Trond Myklebustd9531262009-07-21 19:22:38 -04005110 if (request->fl_type == F_UNLCK) {
5111 if (state != NULL)
5112 return nfs4_proc_unlck(state, cmd, request);
5113 return 0;
5114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
Trond Myklebustd9531262009-07-21 19:22:38 -04005116 if (state == NULL)
5117 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005118 /*
5119 * Don't rely on the VFS having checked the file open mode,
5120 * since it won't do this for flock() locks.
5121 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005122 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005123 case F_RDLCK:
5124 if (!(filp->f_mode & FMODE_READ))
5125 return -EBADF;
5126 break;
5127 case F_WRLCK:
5128 if (!(filp->f_mode & FMODE_WRITE))
5129 return -EBADF;
5130 }
5131
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 do {
5133 status = nfs4_proc_setlk(state, cmd, request);
5134 if ((status != -EAGAIN) || IS_SETLK(cmd))
5135 break;
5136 timeout = nfs4_set_lock_task_retry(timeout);
5137 status = -ERESTARTSYS;
5138 if (signalled())
5139 break;
5140 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 return status;
5142}
5143
Trond Myklebust888e6942005-11-04 15:38:11 -05005144int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5145{
5146 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005147 int err;
5148
5149 err = nfs4_set_lock_state(state, fl);
5150 if (err != 0)
5151 goto out;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005152 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5153 switch (err) {
5154 default:
5155 printk(KERN_ERR "NFS: %s: unhandled error "
5156 "%d.\n", __func__, err);
5157 case 0:
5158 case -ESTALE:
5159 goto out;
5160 case -NFS4ERR_STALE_CLIENTID:
5161 case -NFS4ERR_STALE_STATEID:
5162 set_bit(NFS_DELEGATED_STATE, &state->flags);
5163 case -NFS4ERR_EXPIRED:
5164 nfs4_schedule_lease_recovery(server->nfs_client);
5165 return -EAGAIN;
5166 case -NFS4ERR_BADSESSION:
5167 case -NFS4ERR_BADSLOT:
5168 case -NFS4ERR_BAD_HIGH_SLOT:
5169 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5170 case -NFS4ERR_DEADSESSION:
5171 set_bit(NFS_DELEGATED_STATE, &state->flags);
5172 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5173 return -EAGAIN;
5174 case -NFS4ERR_DELEG_REVOKED:
5175 case -NFS4ERR_ADMIN_REVOKED:
5176 case -NFS4ERR_BAD_STATEID:
5177 case -NFS4ERR_OPENMODE:
5178 nfs4_schedule_stateid_recovery(server, state);
5179 return 0;
5180 case -NFS4ERR_DELAY:
5181 case -NFS4ERR_GRACE:
5182 set_bit(NFS_DELEGATED_STATE, &state->flags);
5183 ssleep(1);
5184 return -EAGAIN;
5185 case -ENOMEM:
5186 case -NFS4ERR_DENIED:
5187 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5188 return 0;
5189 }
Trond Myklebust888e6942005-11-04 15:38:11 -05005190out:
5191 return err;
5192}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005193
Trond Myklebustcf470c32012-03-07 13:49:12 -05005194struct nfs_release_lockowner_data {
5195 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005196 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005197 struct nfs_release_lockowner_args args;
5198};
5199
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005200static void nfs4_release_lockowner_release(void *calldata)
5201{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005202 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005203 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005204 kfree(calldata);
5205}
5206
Trond Myklebust17280172012-03-11 13:11:00 -04005207static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005208 .rpc_release = nfs4_release_lockowner_release,
5209};
5210
Trond Myklebustcf470c32012-03-07 13:49:12 -05005211int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005212{
5213 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005214 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005215 struct rpc_message msg = {
5216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5217 };
5218
5219 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005220 return -EINVAL;
5221 data = kmalloc(sizeof(*data), GFP_NOFS);
5222 if (!data)
5223 return -ENOMEM;
5224 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005225 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005226 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5227 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5228 data->args.lock_owner.s_dev = server->s_dev;
5229 msg.rpc_argp = &data->args;
5230 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5231 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005232}
5233
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005234#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5235
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005236static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5237 const void *buf, size_t buflen,
5238 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005239{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005240 if (strcmp(key, "") != 0)
5241 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005242
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005243 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005244}
5245
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005246static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5247 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005248{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005249 if (strcmp(key, "") != 0)
5250 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005251
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005252 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005253}
5254
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005255static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5256 size_t list_len, const char *name,
5257 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005258{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005259 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005260
J. Bruce Fields096455a2006-03-20 23:23:42 -05005261 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5262 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005263
5264 if (list && len <= list_len)
5265 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005266 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005267}
5268
Andy Adamson533eb462011-06-13 18:25:56 -04005269/*
5270 * nfs_fhget will use either the mounted_on_fileid or the fileid
5271 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005272static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5273{
Andy Adamson533eb462011-06-13 18:25:56 -04005274 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5275 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5276 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005277 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005278 return;
5279
5280 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005281 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005282 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5283 fattr->nlink = 2;
5284}
5285
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005286static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5287 const struct qstr *name,
5288 struct nfs4_fs_locations *fs_locations,
5289 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005290{
5291 struct nfs_server *server = NFS_SERVER(dir);
5292 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005293 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005294 };
5295 struct nfs4_fs_locations_arg args = {
5296 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005297 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005298 .page = page,
5299 .bitmask = bitmask,
5300 };
Benny Halevy22958462009-04-01 09:22:02 -04005301 struct nfs4_fs_locations_res res = {
5302 .fs_locations = fs_locations,
5303 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005304 struct rpc_message msg = {
5305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5306 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005307 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005308 };
5309 int status;
5310
Harvey Harrison3110ff82008-05-02 13:42:44 -07005311 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005312
5313 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5314 * is not supported */
5315 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5316 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5317 else
5318 bitmask[0] |= FATTR4_WORD0_FILEID;
5319
Trond Myklebustc228fd32007-01-13 02:28:11 -05005320 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005321 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005322 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005323 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005324 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005325 return status;
5326}
5327
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005328int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5329 const struct qstr *name,
5330 struct nfs4_fs_locations *fs_locations,
5331 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005332{
5333 struct nfs4_exception exception = { };
5334 int err;
5335 do {
5336 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005337 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005338 &exception);
5339 } while (exception.retry);
5340 return err;
5341}
5342
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005343static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5344{
5345 int status;
5346 struct nfs4_secinfo_arg args = {
5347 .dir_fh = NFS_FH(dir),
5348 .name = name,
5349 };
5350 struct nfs4_secinfo_res res = {
5351 .flavors = flavors,
5352 };
5353 struct rpc_message msg = {
5354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5355 .rpc_argp = &args,
5356 .rpc_resp = &res,
5357 };
5358
5359 dprintk("NFS call secinfo %s\n", name->name);
5360 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5361 dprintk("NFS reply secinfo: %d\n", status);
5362 return status;
5363}
5364
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005365int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5366 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005367{
5368 struct nfs4_exception exception = { };
5369 int err;
5370 do {
5371 err = nfs4_handle_exception(NFS_SERVER(dir),
5372 _nfs4_proc_secinfo(dir, name, flavors),
5373 &exception);
5374 } while (exception.retry);
5375 return err;
5376}
5377
Andy Adamson557134a2009-04-01 09:21:53 -04005378#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005379/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005380 * Check the exchange flags returned by the server for invalid flags, having
5381 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5382 * DS flags set.
5383 */
5384static int nfs4_check_cl_exchange_flags(u32 flags)
5385{
5386 if (flags & ~EXCHGID4_FLAG_MASK_R)
5387 goto out_inval;
5388 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5389 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5390 goto out_inval;
5391 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5392 goto out_inval;
5393 return NFS_OK;
5394out_inval:
5395 return -NFS4ERR_INVAL;
5396}
5397
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005398static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005399nfs41_same_server_scope(struct nfs41_server_scope *a,
5400 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005401{
5402 if (a->server_scope_sz == b->server_scope_sz &&
5403 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5404 return true;
5405
5406 return false;
5407}
5408
Andy Adamson357f54d2010-12-14 10:11:57 -05005409/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005410 * nfs4_proc_bind_conn_to_session()
5411 *
5412 * The 4.1 client currently uses the same TCP connection for the
5413 * fore and backchannel.
5414 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005415int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005416{
5417 int status;
5418 struct nfs41_bind_conn_to_session_res res;
5419 struct rpc_message msg = {
5420 .rpc_proc =
5421 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5422 .rpc_argp = clp,
5423 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005424 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005425 };
5426
5427 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005428
5429 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5430 if (unlikely(res.session == NULL)) {
5431 status = -ENOMEM;
5432 goto out;
5433 }
5434
5435 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5436 if (status == 0) {
5437 if (memcmp(res.session->sess_id.data,
5438 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5439 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5440 status = -EIO;
5441 goto out_session;
5442 }
5443 if (res.dir != NFS4_CDFS4_BOTH) {
5444 dprintk("NFS: %s: Unexpected direction from server\n",
5445 __func__);
5446 status = -EIO;
5447 goto out_session;
5448 }
5449 if (res.use_conn_in_rdma_mode) {
5450 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5451 __func__);
5452 status = -EIO;
5453 goto out_session;
5454 }
5455 }
5456out_session:
5457 kfree(res.session);
5458out:
5459 dprintk("<-- %s status= %d\n", __func__, status);
5460 return status;
5461}
5462
5463/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005464 * nfs4_proc_exchange_id()
5465 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005466 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5467 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005468 * Since the clientid has expired, all compounds using sessions
5469 * associated with the stale clientid will be returning
5470 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5471 * be in some phase of session reset.
5472 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005473int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005474{
5475 nfs4_verifier verifier;
5476 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005477 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005478 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005479 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005480 };
5481 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005482 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005483 };
5484 int status;
5485 struct rpc_message msg = {
5486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5487 .rpc_argp = &args,
5488 .rpc_resp = &res,
5489 .rpc_cred = cred,
5490 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491
Chuck Leverf0920752012-05-21 22:45:41 -04005492 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005493 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5494 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005495 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5496 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5497 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005498
Chuck Leveracdeb692012-05-21 22:46:16 -04005499 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005500 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005501 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005502 status = -ENOMEM;
5503 goto out;
5504 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005505
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005506 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005507 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005508 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005509 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005510 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005511 }
5512
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005513 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005514 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005515 status = -ENOMEM;
5516 goto out_server_scope;
5517 }
5518
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005519 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005520 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005521 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005522
Chuck Lever177313f2012-05-21 22:44:58 -04005523 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005524 clp->cl_clientid = res.clientid;
5525 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5526 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5527 clp->cl_seqid = res.seqid;
5528
Chuck Leveracdeb692012-05-21 22:46:16 -04005529 kfree(clp->cl_serverowner);
5530 clp->cl_serverowner = res.server_owner;
5531 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005532
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005533 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005534 kfree(clp->cl_implid);
5535 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005536
Chuck Lever177313f2012-05-21 22:44:58 -04005537 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005538 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005539 res.server_scope)) {
5540 dprintk("%s: server_scope mismatch detected\n",
5541 __func__);
5542 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005543 kfree(clp->cl_serverscope);
5544 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005545 }
5546
Chuck Lever177313f2012-05-21 22:44:58 -04005547 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005548 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005549 goto out;
5550 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005551 } else
5552 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005553
Chuck Leveracdeb692012-05-21 22:46:16 -04005554out_server_owner:
5555 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005556out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005557 kfree(res.server_scope);
5558out:
Chuck Lever177313f2012-05-21 22:44:58 -04005559 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005560 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005561 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005562 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005563 clp->cl_implid->date.seconds,
5564 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005565 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005566 return status;
5567}
5568
Trond Myklebust66245532012-05-25 17:18:09 -04005569static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5570 struct rpc_cred *cred)
5571{
5572 struct rpc_message msg = {
5573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5574 .rpc_argp = clp,
5575 .rpc_cred = cred,
5576 };
5577 int status;
5578
5579 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5580 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005581 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005582 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5583 return status;
5584}
5585
5586static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5587 struct rpc_cred *cred)
5588{
5589 unsigned int loop;
5590 int ret;
5591
5592 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5593 ret = _nfs4_proc_destroy_clientid(clp, cred);
5594 switch (ret) {
5595 case -NFS4ERR_DELAY:
5596 case -NFS4ERR_CLIENTID_BUSY:
5597 ssleep(1);
5598 break;
5599 default:
5600 return ret;
5601 }
5602 }
5603 return 0;
5604}
5605
5606int nfs4_destroy_clientid(struct nfs_client *clp)
5607{
5608 struct rpc_cred *cred;
5609 int ret = 0;
5610
5611 if (clp->cl_mvops->minor_version < 1)
5612 goto out;
5613 if (clp->cl_exchange_flags == 0)
5614 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005615 if (clp->cl_preserve_clid)
5616 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005617 cred = nfs4_get_exchange_id_cred(clp);
5618 ret = nfs4_proc_destroy_clientid(clp, cred);
5619 if (cred)
5620 put_rpccred(cred);
5621 switch (ret) {
5622 case 0:
5623 case -NFS4ERR_STALE_CLIENTID:
5624 clp->cl_exchange_flags = 0;
5625 }
5626out:
5627 return ret;
5628}
5629
Andy Adamson2050f0c2009-04-01 09:22:30 -04005630struct nfs4_get_lease_time_data {
5631 struct nfs4_get_lease_time_args *args;
5632 struct nfs4_get_lease_time_res *res;
5633 struct nfs_client *clp;
5634};
5635
5636static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5637 void *calldata)
5638{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005639 struct nfs4_get_lease_time_data *data =
5640 (struct nfs4_get_lease_time_data *)calldata;
5641
5642 dprintk("--> %s\n", __func__);
5643 /* just setup sequence, do not trigger session recovery
5644 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005645 nfs41_setup_sequence(data->clp->cl_session,
5646 &data->args->la_seq_args,
5647 &data->res->lr_seq_res,
5648 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005649 dprintk("<-- %s\n", __func__);
5650}
5651
5652/*
5653 * Called from nfs4_state_manager thread for session setup, so don't recover
5654 * from sequence operation or clientid errors.
5655 */
5656static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5657{
5658 struct nfs4_get_lease_time_data *data =
5659 (struct nfs4_get_lease_time_data *)calldata;
5660
5661 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005662 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5663 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005664 switch (task->tk_status) {
5665 case -NFS4ERR_DELAY:
5666 case -NFS4ERR_GRACE:
5667 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5668 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5669 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005670 /* fall through */
5671 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005672 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005673 return;
5674 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005675 dprintk("<-- %s\n", __func__);
5676}
5677
Trond Myklebust17280172012-03-11 13:11:00 -04005678static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005679 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5680 .rpc_call_done = nfs4_get_lease_time_done,
5681};
5682
5683int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5684{
5685 struct rpc_task *task;
5686 struct nfs4_get_lease_time_args args;
5687 struct nfs4_get_lease_time_res res = {
5688 .lr_fsinfo = fsinfo,
5689 };
5690 struct nfs4_get_lease_time_data data = {
5691 .args = &args,
5692 .res = &res,
5693 .clp = clp,
5694 };
5695 struct rpc_message msg = {
5696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5697 .rpc_argp = &args,
5698 .rpc_resp = &res,
5699 };
5700 struct rpc_task_setup task_setup = {
5701 .rpc_client = clp->cl_rpcclient,
5702 .rpc_message = &msg,
5703 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005704 .callback_data = &data,
5705 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005706 };
5707 int status;
5708
Trond Myklebust9d12b212012-01-17 22:04:25 -05005709 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005710 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005711 dprintk("--> %s\n", __func__);
5712 task = rpc_run_task(&task_setup);
5713
5714 if (IS_ERR(task))
5715 status = PTR_ERR(task);
5716 else {
5717 status = task->tk_status;
5718 rpc_put_task(task);
5719 }
5720 dprintk("<-- %s return %d\n", __func__, status);
5721
5722 return status;
5723}
5724
Andy Adamsonfc931582009-04-01 09:22:31 -04005725/*
5726 * Initialize the values to be used by the client in CREATE_SESSION
5727 * If nfs4_init_session set the fore channel request and response sizes,
5728 * use them.
5729 *
5730 * Set the back channel max_resp_sz_cached to zero to force the client to
5731 * always set csa_cachethis to FALSE because the current implementation
5732 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5733 */
5734static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5735{
5736 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005737 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5738 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005739
5740 if (mxrqst_sz == 0)
5741 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5742 if (mxresp_sz == 0)
5743 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5744 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005745 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5746 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005747 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005748 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005749
5750 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005751 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005752 __func__,
5753 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005754 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005755
5756 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005757 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5758 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5759 args->bc_attrs.max_resp_sz_cached = 0;
5760 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5761 args->bc_attrs.max_reqs = 1;
5762
5763 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5764 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5765 __func__,
5766 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5767 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5768 args->bc_attrs.max_reqs);
5769}
5770
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005771static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005772{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005773 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5774 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5775
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005776 if (rcvd->max_resp_sz > sent->max_resp_sz)
5777 return -EINVAL;
5778 /*
5779 * Our requested max_ops is the minimum we need; we're not
5780 * prepared to break up compounds into smaller pieces than that.
5781 * So, no point even trying to continue if the server won't
5782 * cooperate:
5783 */
5784 if (rcvd->max_ops < sent->max_ops)
5785 return -EINVAL;
5786 if (rcvd->max_reqs == 0)
5787 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005788 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5789 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005790 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005791}
5792
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005793static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5794{
5795 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5796 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005797
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005798 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5799 return -EINVAL;
5800 if (rcvd->max_resp_sz < sent->max_resp_sz)
5801 return -EINVAL;
5802 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5803 return -EINVAL;
5804 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005805 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005806 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005807 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005808 return -EINVAL;
5809 return 0;
5810}
Andy Adamson8d353012009-04-01 09:22:32 -04005811
Andy Adamson8d353012009-04-01 09:22:32 -04005812static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5813 struct nfs4_session *session)
5814{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005815 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005816
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005817 ret = nfs4_verify_fore_channel_attrs(args, session);
5818 if (ret)
5819 return ret;
5820 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005821}
5822
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005823static int _nfs4_proc_create_session(struct nfs_client *clp,
5824 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005825{
5826 struct nfs4_session *session = clp->cl_session;
5827 struct nfs41_create_session_args args = {
5828 .client = clp,
5829 .cb_program = NFS4_CALLBACK,
5830 };
5831 struct nfs41_create_session_res res = {
5832 .client = clp,
5833 };
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5836 .rpc_argp = &args,
5837 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005838 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005839 };
5840 int status;
5841
5842 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005843 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005844
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005845 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005846
Trond Myklebust43095d32012-11-20 11:13:12 -05005847 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005848 /* Verify the session's negotiated channel_attrs values */
5849 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005850 /* Increment the clientid slot sequence id */
5851 clp->cl_seqid++;
5852 }
5853
5854 return status;
5855}
5856
5857/*
5858 * Issues a CREATE_SESSION operation to the server.
5859 * It is the responsibility of the caller to verify the session is
5860 * expired before calling this routine.
5861 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005862int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005863{
5864 int status;
5865 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005866 struct nfs4_session *session = clp->cl_session;
5867
5868 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5869
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005870 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005871 if (status)
5872 goto out;
5873
Andy Adamsonaacd5532011-11-09 13:58:21 -05005874 /* Init or reset the session slot tables */
5875 status = nfs4_setup_session_slot_tables(session);
5876 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005877 if (status)
5878 goto out;
5879
5880 ptr = (unsigned *)&session->sess_id.data[0];
5881 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5882 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005883out:
5884 dprintk("<-- %s\n", __func__);
5885 return status;
5886}
5887
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005888/*
5889 * Issue the over-the-wire RPC DESTROY_SESSION.
5890 * The caller must serialize access to this routine.
5891 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005892int nfs4_proc_destroy_session(struct nfs4_session *session,
5893 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005894{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5897 .rpc_argp = session,
5898 .rpc_cred = cred,
5899 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005900 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005901
5902 dprintk("--> nfs4_proc_destroy_session\n");
5903
5904 /* session is still being setup */
5905 if (session->clp->cl_cons_state != NFS_CS_READY)
5906 return status;
5907
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005908 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005909
5910 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005911 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005912 "Session has been destroyed regardless...\n", status);
5913
5914 dprintk("<-- nfs4_proc_destroy_session\n");
5915 return status;
5916}
5917
Trond Myklebust7b38c362012-05-23 13:23:31 -04005918/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005919 * Renew the cl_session lease.
5920 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005921struct nfs4_sequence_data {
5922 struct nfs_client *clp;
5923 struct nfs4_sequence_args args;
5924 struct nfs4_sequence_res res;
5925};
5926
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005927static void nfs41_sequence_release(void *data)
5928{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005929 struct nfs4_sequence_data *calldata = data;
5930 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005931
Alexandros Batsakis71358402010-02-05 03:45:05 -08005932 if (atomic_read(&clp->cl_count) > 1)
5933 nfs4_schedule_state_renewal(clp);
5934 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005935 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005936}
5937
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005938static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5939{
5940 switch(task->tk_status) {
5941 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005942 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5943 return -EAGAIN;
5944 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005945 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005946 }
5947 return 0;
5948}
5949
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005950static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005952 struct nfs4_sequence_data *calldata = data;
5953 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954
Trond Myklebust14516c32010-07-31 14:29:06 -04005955 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5956 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005957
5958 if (task->tk_status < 0) {
5959 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005960 if (atomic_read(&clp->cl_count) == 1)
5961 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005962
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005963 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5964 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005965 return;
5966 }
5967 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005969out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005970 dprintk("<-- %s\n", __func__);
5971}
5972
5973static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5974{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005975 struct nfs4_sequence_data *calldata = data;
5976 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977 struct nfs4_sequence_args *args;
5978 struct nfs4_sequence_res *res;
5979
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005980 args = task->tk_msg.rpc_argp;
5981 res = task->tk_msg.rpc_resp;
5982
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005983 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005984}
5985
5986static const struct rpc_call_ops nfs41_sequence_ops = {
5987 .rpc_call_done = nfs41_sequence_call_done,
5988 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005989 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005990};
5991
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005992static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5993 struct rpc_cred *cred,
5994 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005995{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005996 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005997 struct rpc_message msg = {
5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5999 .rpc_cred = cred,
6000 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006001 struct rpc_task_setup task_setup_data = {
6002 .rpc_client = clp->cl_rpcclient,
6003 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006004 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006005 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6006 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006007
Alexandros Batsakis71358402010-02-05 03:45:05 -08006008 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006009 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006010 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006011 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006012 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006013 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006014 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006015 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006016 if (is_privileged)
6017 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006018 msg.rpc_argp = &calldata->args;
6019 msg.rpc_resp = &calldata->res;
6020 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006021 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006022
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006023 return rpc_run_task(&task_setup_data);
6024}
6025
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006026static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006027{
6028 struct rpc_task *task;
6029 int ret = 0;
6030
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006031 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6032 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006033 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006034 if (IS_ERR(task))
6035 ret = PTR_ERR(task);
6036 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006037 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006038 dprintk("<-- %s status=%d\n", __func__, ret);
6039 return ret;
6040}
6041
6042static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6043{
6044 struct rpc_task *task;
6045 int ret;
6046
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006047 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006048 if (IS_ERR(task)) {
6049 ret = PTR_ERR(task);
6050 goto out;
6051 }
6052 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006053 if (!ret) {
6054 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6055
6056 if (task->tk_status == 0)
6057 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006058 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006059 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006060 rpc_put_task(task);
6061out:
6062 dprintk("<-- %s status=%d\n", __func__, ret);
6063 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006064}
6065
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006066struct nfs4_reclaim_complete_data {
6067 struct nfs_client *clp;
6068 struct nfs41_reclaim_complete_args arg;
6069 struct nfs41_reclaim_complete_res res;
6070};
6071
6072static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6073{
6074 struct nfs4_reclaim_complete_data *calldata = data;
6075
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006076 nfs41_setup_sequence(calldata->clp->cl_session,
6077 &calldata->arg.seq_args,
6078 &calldata->res.seq_res,
6079 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006080}
6081
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006082static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6083{
6084 switch(task->tk_status) {
6085 case 0:
6086 case -NFS4ERR_COMPLETE_ALREADY:
6087 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6088 break;
6089 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006090 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006091 /* fall through */
6092 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006093 return -EAGAIN;
6094 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006095 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006096 }
6097 return 0;
6098}
6099
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006100static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6101{
6102 struct nfs4_reclaim_complete_data *calldata = data;
6103 struct nfs_client *clp = calldata->clp;
6104 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6105
6106 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006107 if (!nfs41_sequence_done(task, res))
6108 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006109
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006110 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6111 rpc_restart_call_prepare(task);
6112 return;
6113 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114 dprintk("<-- %s\n", __func__);
6115}
6116
6117static void nfs4_free_reclaim_complete_data(void *data)
6118{
6119 struct nfs4_reclaim_complete_data *calldata = data;
6120
6121 kfree(calldata);
6122}
6123
6124static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6125 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6126 .rpc_call_done = nfs4_reclaim_complete_done,
6127 .rpc_release = nfs4_free_reclaim_complete_data,
6128};
6129
6130/*
6131 * Issue a global reclaim complete.
6132 */
6133static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6134{
6135 struct nfs4_reclaim_complete_data *calldata;
6136 struct rpc_task *task;
6137 struct rpc_message msg = {
6138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6139 };
6140 struct rpc_task_setup task_setup_data = {
6141 .rpc_client = clp->cl_rpcclient,
6142 .rpc_message = &msg,
6143 .callback_ops = &nfs4_reclaim_complete_call_ops,
6144 .flags = RPC_TASK_ASYNC,
6145 };
6146 int status = -ENOMEM;
6147
6148 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006149 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006150 if (calldata == NULL)
6151 goto out;
6152 calldata->clp = clp;
6153 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154
Trond Myklebust9d12b212012-01-17 22:04:25 -05006155 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006156 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006157 msg.rpc_argp = &calldata->arg;
6158 msg.rpc_resp = &calldata->res;
6159 task_setup_data.callback_data = calldata;
6160 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006161 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006162 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006163 goto out;
6164 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006165 status = nfs4_wait_for_completion_rpc_task(task);
6166 if (status == 0)
6167 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006168 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006169 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006170out:
6171 dprintk("<-- %s status=%d\n", __func__, status);
6172 return status;
6173}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006174
6175static void
6176nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6177{
6178 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006179 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006180 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006181
6182 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006183 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6184 * right now covering the LAYOUTGET we are about to send.
6185 * However, that is not so catastrophic, and there seems
6186 * to be no way to prevent it completely.
6187 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006188 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006189 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006191 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6192 NFS_I(lgp->args.inode)->layout,
6193 lgp->args.ctx->state)) {
6194 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006195 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006196}
6197
6198static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6199{
6200 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006201 struct inode *inode = lgp->args.inode;
6202 struct nfs_server *server = NFS_SERVER(inode);
6203 struct pnfs_layout_hdr *lo;
6204 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006205 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206
6207 dprintk("--> %s\n", __func__);
6208
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006209 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006210 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006211
6212 switch (task->tk_status) {
6213 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006214 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006215 case -NFS4ERR_LAYOUTTRYLATER:
6216 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006217 timeo = rpc_get_timeout(task->tk_client);
6218 giveup = lgp->args.timestamp + timeo;
6219 if (time_after(giveup, jiffies))
6220 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006221 break;
6222 case -NFS4ERR_EXPIRED:
6223 case -NFS4ERR_BAD_STATEID:
6224 spin_lock(&inode->i_lock);
6225 lo = NFS_I(inode)->layout;
6226 if (!lo || list_empty(&lo->plh_segs)) {
6227 spin_unlock(&inode->i_lock);
6228 /* If the open stateid was bad, then recover it. */
6229 state = lgp->args.ctx->state;
6230 } else {
6231 LIST_HEAD(head);
6232
6233 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6234 spin_unlock(&inode->i_lock);
6235 /* Mark the bad layout state as invalid, then
6236 * retry using the open stateid. */
6237 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006238 }
6239 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006240 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6241 rpc_restart_call_prepare(task);
6242out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006243 dprintk("<-- %s\n", __func__);
6244}
6245
Idan Kedar85541162012-08-02 11:47:10 +03006246static size_t max_response_pages(struct nfs_server *server)
6247{
6248 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6249 return nfs_page_array_len(0, max_resp_sz);
6250}
6251
6252static void nfs4_free_pages(struct page **pages, size_t size)
6253{
6254 int i;
6255
6256 if (!pages)
6257 return;
6258
6259 for (i = 0; i < size; i++) {
6260 if (!pages[i])
6261 break;
6262 __free_page(pages[i]);
6263 }
6264 kfree(pages);
6265}
6266
6267static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6268{
6269 struct page **pages;
6270 int i;
6271
6272 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6273 if (!pages) {
6274 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6275 return NULL;
6276 }
6277
6278 for (i = 0; i < size; i++) {
6279 pages[i] = alloc_page(gfp_flags);
6280 if (!pages[i]) {
6281 dprintk("%s: failed to allocate page\n", __func__);
6282 nfs4_free_pages(pages, size);
6283 return NULL;
6284 }
6285 }
6286
6287 return pages;
6288}
6289
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006290static void nfs4_layoutget_release(void *calldata)
6291{
6292 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006293 struct inode *inode = lgp->args.inode;
6294 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006295 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006296
6297 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006298 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006299 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006300 put_nfs_open_context(lgp->args.ctx);
6301 kfree(calldata);
6302 dprintk("<-- %s\n", __func__);
6303}
6304
6305static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6306 .rpc_call_prepare = nfs4_layoutget_prepare,
6307 .rpc_call_done = nfs4_layoutget_done,
6308 .rpc_release = nfs4_layoutget_release,
6309};
6310
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006311struct pnfs_layout_segment *
6312nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006313{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006314 struct inode *inode = lgp->args.inode;
6315 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006316 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006317 struct rpc_task *task;
6318 struct rpc_message msg = {
6319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6320 .rpc_argp = &lgp->args,
6321 .rpc_resp = &lgp->res,
6322 };
6323 struct rpc_task_setup task_setup_data = {
6324 .rpc_client = server->client,
6325 .rpc_message = &msg,
6326 .callback_ops = &nfs4_layoutget_call_ops,
6327 .callback_data = lgp,
6328 .flags = RPC_TASK_ASYNC,
6329 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006330 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006331 int status = 0;
6332
6333 dprintk("--> %s\n", __func__);
6334
Idan Kedar85541162012-08-02 11:47:10 +03006335 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6336 if (!lgp->args.layout.pages) {
6337 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006338 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006339 }
6340 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006341 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006342
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006343 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006344 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006345 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006346
6347 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6348 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6349
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 task = rpc_run_task(&task_setup_data);
6351 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006352 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006353 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006354 if (status == 0)
6355 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006356 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6357 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006358 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006359 rpc_put_task(task);
6360 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006361 if (status)
6362 return ERR_PTR(status);
6363 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006364}
6365
Benny Halevycbe82602011-05-22 19:52:37 +03006366static void
6367nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6368{
6369 struct nfs4_layoutreturn *lrp = calldata;
6370
6371 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006372 nfs41_setup_sequence(lrp->clp->cl_session,
6373 &lrp->args.seq_args,
6374 &lrp->res.seq_res,
6375 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006376}
6377
6378static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6379{
6380 struct nfs4_layoutreturn *lrp = calldata;
6381 struct nfs_server *server;
6382
6383 dprintk("--> %s\n", __func__);
6384
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006385 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006386 return;
6387
6388 server = NFS_SERVER(lrp->args.inode);
6389 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006390 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006391 return;
6392 }
Benny Halevycbe82602011-05-22 19:52:37 +03006393 dprintk("<-- %s\n", __func__);
6394}
6395
6396static void nfs4_layoutreturn_release(void *calldata)
6397{
6398 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006399 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006400
6401 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006402 spin_lock(&lo->plh_inode->i_lock);
6403 if (lrp->res.lrs_present)
6404 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6405 lo->plh_block_lgets--;
6406 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006407 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006408 kfree(calldata);
6409 dprintk("<-- %s\n", __func__);
6410}
6411
6412static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6413 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6414 .rpc_call_done = nfs4_layoutreturn_done,
6415 .rpc_release = nfs4_layoutreturn_release,
6416};
6417
6418int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6419{
6420 struct rpc_task *task;
6421 struct rpc_message msg = {
6422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6423 .rpc_argp = &lrp->args,
6424 .rpc_resp = &lrp->res,
6425 };
6426 struct rpc_task_setup task_setup_data = {
6427 .rpc_client = lrp->clp->cl_rpcclient,
6428 .rpc_message = &msg,
6429 .callback_ops = &nfs4_layoutreturn_call_ops,
6430 .callback_data = lrp,
6431 };
6432 int status;
6433
6434 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006435 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006436 task = rpc_run_task(&task_setup_data);
6437 if (IS_ERR(task))
6438 return PTR_ERR(task);
6439 status = task->tk_status;
6440 dprintk("<-- %s status=%d\n", __func__, status);
6441 rpc_put_task(task);
6442 return status;
6443}
6444
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006445/*
6446 * Retrieve the list of Data Server devices from the MDS.
6447 */
6448static int _nfs4_getdevicelist(struct nfs_server *server,
6449 const struct nfs_fh *fh,
6450 struct pnfs_devicelist *devlist)
6451{
6452 struct nfs4_getdevicelist_args args = {
6453 .fh = fh,
6454 .layoutclass = server->pnfs_curr_ld->id,
6455 };
6456 struct nfs4_getdevicelist_res res = {
6457 .devlist = devlist,
6458 };
6459 struct rpc_message msg = {
6460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6461 .rpc_argp = &args,
6462 .rpc_resp = &res,
6463 };
6464 int status;
6465
6466 dprintk("--> %s\n", __func__);
6467 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6468 &res.seq_res, 0);
6469 dprintk("<-- %s status=%d\n", __func__, status);
6470 return status;
6471}
6472
6473int nfs4_proc_getdevicelist(struct nfs_server *server,
6474 const struct nfs_fh *fh,
6475 struct pnfs_devicelist *devlist)
6476{
6477 struct nfs4_exception exception = { };
6478 int err;
6479
6480 do {
6481 err = nfs4_handle_exception(server,
6482 _nfs4_getdevicelist(server, fh, devlist),
6483 &exception);
6484 } while (exception.retry);
6485
6486 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6487 err, devlist->num_devs);
6488
6489 return err;
6490}
6491EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6492
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006493static int
6494_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6495{
6496 struct nfs4_getdeviceinfo_args args = {
6497 .pdev = pdev,
6498 };
6499 struct nfs4_getdeviceinfo_res res = {
6500 .pdev = pdev,
6501 };
6502 struct rpc_message msg = {
6503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6504 .rpc_argp = &args,
6505 .rpc_resp = &res,
6506 };
6507 int status;
6508
6509 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006510 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006511 dprintk("<-- %s status=%d\n", __func__, status);
6512
6513 return status;
6514}
6515
6516int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6517{
6518 struct nfs4_exception exception = { };
6519 int err;
6520
6521 do {
6522 err = nfs4_handle_exception(server,
6523 _nfs4_proc_getdeviceinfo(server, pdev),
6524 &exception);
6525 } while (exception.retry);
6526 return err;
6527}
6528EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6529
Andy Adamson863a3c62011-03-23 13:27:54 +00006530static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6531{
6532 struct nfs4_layoutcommit_data *data = calldata;
6533 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006534 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006535
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006536 nfs41_setup_sequence(session,
6537 &data->args.seq_args,
6538 &data->res.seq_res,
6539 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006540}
6541
6542static void
6543nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6544{
6545 struct nfs4_layoutcommit_data *data = calldata;
6546 struct nfs_server *server = NFS_SERVER(data->args.inode);
6547
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006548 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006549 return;
6550
6551 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006552 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6553 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6554 case -NFS4ERR_BADLAYOUT: /* no layout */
6555 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006556 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006557 break;
6558 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006559 nfs_post_op_update_inode_force_wcc(data->args.inode,
6560 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006561 break;
6562 default:
6563 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6564 rpc_restart_call_prepare(task);
6565 return;
6566 }
6567 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006568}
6569
6570static void nfs4_layoutcommit_release(void *calldata)
6571{
6572 struct nfs4_layoutcommit_data *data = calldata;
6573
Andy Adamsondb29c082011-07-30 20:52:38 -04006574 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006575 put_rpccred(data->cred);
6576 kfree(data);
6577}
6578
6579static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6580 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6581 .rpc_call_done = nfs4_layoutcommit_done,
6582 .rpc_release = nfs4_layoutcommit_release,
6583};
6584
6585int
Andy Adamsonef311532011-03-12 02:58:10 -05006586nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006587{
6588 struct rpc_message msg = {
6589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6590 .rpc_argp = &data->args,
6591 .rpc_resp = &data->res,
6592 .rpc_cred = data->cred,
6593 };
6594 struct rpc_task_setup task_setup_data = {
6595 .task = &data->task,
6596 .rpc_client = NFS_CLIENT(data->args.inode),
6597 .rpc_message = &msg,
6598 .callback_ops = &nfs4_layoutcommit_ops,
6599 .callback_data = data,
6600 .flags = RPC_TASK_ASYNC,
6601 };
6602 struct rpc_task *task;
6603 int status = 0;
6604
6605 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6606 "lbw: %llu inode %lu\n",
6607 data->task.tk_pid, sync,
6608 data->args.lastbytewritten,
6609 data->args.inode->i_ino);
6610
Trond Myklebust9d12b212012-01-17 22:04:25 -05006611 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006612 task = rpc_run_task(&task_setup_data);
6613 if (IS_ERR(task))
6614 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006615 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006616 goto out;
6617 status = nfs4_wait_for_completion_rpc_task(task);
6618 if (status != 0)
6619 goto out;
6620 status = task->tk_status;
6621out:
6622 dprintk("%s: status %d\n", __func__, status);
6623 rpc_put_task(task);
6624 return status;
6625}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006626
6627static int
6628_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6629 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6630{
6631 struct nfs41_secinfo_no_name_args args = {
6632 .style = SECINFO_STYLE_CURRENT_FH,
6633 };
6634 struct nfs4_secinfo_res res = {
6635 .flavors = flavors,
6636 };
6637 struct rpc_message msg = {
6638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6639 .rpc_argp = &args,
6640 .rpc_resp = &res,
6641 };
6642 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6643}
6644
6645static int
6646nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6647 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6648{
6649 struct nfs4_exception exception = { };
6650 int err;
6651 do {
6652 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6653 switch (err) {
6654 case 0:
6655 case -NFS4ERR_WRONGSEC:
6656 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006657 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006658 default:
6659 err = nfs4_handle_exception(server, err, &exception);
6660 }
6661 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006662out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006663 return err;
6664}
6665
6666static int
6667nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6668 struct nfs_fsinfo *info)
6669{
6670 int err;
6671 struct page *page;
6672 rpc_authflavor_t flavor;
6673 struct nfs4_secinfo_flavors *flavors;
6674
6675 page = alloc_page(GFP_KERNEL);
6676 if (!page) {
6677 err = -ENOMEM;
6678 goto out;
6679 }
6680
6681 flavors = page_address(page);
6682 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6683
6684 /*
6685 * Fall back on "guess and check" method if
6686 * the server doesn't support SECINFO_NO_NAME
6687 */
6688 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6689 err = nfs4_find_root_sec(server, fhandle, info);
6690 goto out_freepage;
6691 }
6692 if (err)
6693 goto out_freepage;
6694
6695 flavor = nfs_find_best_sec(flavors);
6696 if (err == 0)
6697 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6698
6699out_freepage:
6700 put_page(page);
6701 if (err == -EACCES)
6702 return -EPERM;
6703out:
6704 return err;
6705}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006706
6707static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006708{
6709 int status;
6710 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006711 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006712 };
6713 struct nfs41_test_stateid_res res;
6714 struct rpc_message msg = {
6715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6716 .rpc_argp = &args,
6717 .rpc_resp = &res,
6718 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006719
Chuck Lever38527b12012-07-11 16:30:23 -04006720 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006721 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006722 nfs4_set_sequence_privileged(&args.seq_args);
6723 status = nfs4_call_sync_sequence(server->client, server, &msg,
6724 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006725 if (status != NFS_OK) {
6726 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006727 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006728 }
6729 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006730 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006731}
6732
Chuck Lever38527b12012-07-11 16:30:23 -04006733/**
6734 * nfs41_test_stateid - perform a TEST_STATEID operation
6735 *
6736 * @server: server / transport on which to perform the operation
6737 * @stateid: state ID to test
6738 *
6739 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6740 * Otherwise a negative NFS4ERR value is returned if the operation
6741 * failed or the state ID is not currently valid.
6742 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006743static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006744{
6745 struct nfs4_exception exception = { };
6746 int err;
6747 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006748 err = _nfs41_test_stateid(server, stateid);
6749 if (err != -NFS4ERR_DELAY)
6750 break;
6751 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006752 } while (exception.retry);
6753 return err;
6754}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006755
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006756static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006757{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006758 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006759 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006760 };
6761 struct nfs41_free_stateid_res res;
6762 struct rpc_message msg = {
6763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6764 .rpc_argp = &args,
6765 .rpc_resp = &res,
6766 };
Chuck Lever38527b12012-07-11 16:30:23 -04006767 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006768
Chuck Lever38527b12012-07-11 16:30:23 -04006769 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006770 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006771 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006772 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006773 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006774 dprintk("NFS reply free_stateid: %d\n", status);
6775 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006776}
6777
Chuck Lever38527b12012-07-11 16:30:23 -04006778/**
6779 * nfs41_free_stateid - perform a FREE_STATEID operation
6780 *
6781 * @server: server / transport on which to perform the operation
6782 * @stateid: state ID to release
6783 *
6784 * Returns NFS_OK if the server freed "stateid". Otherwise a
6785 * negative NFS4ERR value is returned.
6786 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006787static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006788{
6789 struct nfs4_exception exception = { };
6790 int err;
6791 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006792 err = _nfs4_free_stateid(server, stateid);
6793 if (err != -NFS4ERR_DELAY)
6794 break;
6795 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006796 } while (exception.retry);
6797 return err;
6798}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006799
6800static bool nfs41_match_stateid(const nfs4_stateid *s1,
6801 const nfs4_stateid *s2)
6802{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006803 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006804 return false;
6805
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006806 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006807 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006808 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006809 return true;
6810
6811 return false;
6812}
6813
Andy Adamson557134a2009-04-01 09:21:53 -04006814#endif /* CONFIG_NFS_V4_1 */
6815
Trond Myklebust36281ca2012-03-04 18:13:56 -05006816static bool nfs4_match_stateid(const nfs4_stateid *s1,
6817 const nfs4_stateid *s2)
6818{
Trond Myklebustf597c532012-03-04 18:13:56 -05006819 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006820}
6821
6822
Trond Myklebust17280172012-03-11 13:11:00 -04006823static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006824 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006825 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 .recover_open = nfs4_open_reclaim,
6827 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006828 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006829 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006830 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831};
6832
Andy Adamson591d71c2009-04-01 09:22:47 -04006833#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006834static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006835 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6836 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6837 .recover_open = nfs4_open_reclaim,
6838 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006839 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006840 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006841 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006842 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006843};
6844#endif /* CONFIG_NFS_V4_1 */
6845
Trond Myklebust17280172012-03-11 13:11:00 -04006846static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006847 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006848 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 .recover_open = nfs4_open_expired,
6850 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006851 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006852 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853};
6854
Andy Adamson591d71c2009-04-01 09:22:47 -04006855#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006856static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006857 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6858 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006859 .recover_open = nfs41_open_expired,
6860 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006861 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006862 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006863};
6864#endif /* CONFIG_NFS_V4_1 */
6865
Trond Myklebust17280172012-03-11 13:11:00 -04006866static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006867 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006868 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006869 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006870};
6871
6872#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006873static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006874 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006875 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006876 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006877};
6878#endif
6879
Trond Myklebust97dc1352010-06-16 09:52:26 -04006880static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6881 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006882 .init_caps = NFS_CAP_READDIRPLUS
6883 | NFS_CAP_ATOMIC_OPEN
6884 | NFS_CAP_CHANGE_ATTR
6885 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006886 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006887 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006888 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006889 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6890 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6891 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006892};
6893
6894#if defined(CONFIG_NFS_V4_1)
6895static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6896 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006897 .init_caps = NFS_CAP_READDIRPLUS
6898 | NFS_CAP_ATOMIC_OPEN
6899 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006900 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006901 | NFS_CAP_STATEID_NFSV41
6902 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006903 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006904 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006905 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006906 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6907 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6908 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006909};
6910#endif
6911
Trond Myklebust97dc1352010-06-16 09:52:26 -04006912const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6913 [0] = &nfs_v4_0_minor_ops,
6914#if defined(CONFIG_NFS_V4_1)
6915 [1] = &nfs_v4_1_minor_ops,
6916#endif
6917};
6918
Bryan Schumaker73a79702012-07-16 16:39:12 -04006919const struct inode_operations nfs4_dir_inode_operations = {
6920 .create = nfs_create,
6921 .lookup = nfs_lookup,
6922 .atomic_open = nfs_atomic_open,
6923 .link = nfs_link,
6924 .unlink = nfs_unlink,
6925 .symlink = nfs_symlink,
6926 .mkdir = nfs_mkdir,
6927 .rmdir = nfs_rmdir,
6928 .mknod = nfs_mknod,
6929 .rename = nfs_rename,
6930 .permission = nfs_permission,
6931 .getattr = nfs_getattr,
6932 .setattr = nfs_setattr,
6933 .getxattr = generic_getxattr,
6934 .setxattr = generic_setxattr,
6935 .listxattr = generic_listxattr,
6936 .removexattr = generic_removexattr,
6937};
6938
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006939static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006940 .permission = nfs_permission,
6941 .getattr = nfs_getattr,
6942 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006943 .getxattr = generic_getxattr,
6944 .setxattr = generic_setxattr,
6945 .listxattr = generic_listxattr,
6946 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006947};
6948
David Howells509de812006-08-22 20:06:11 -04006949const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 .version = 4, /* protocol version */
6951 .dentry_ops = &nfs4_dentry_operations,
6952 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006953 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006954 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006956 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006957 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 .getattr = nfs4_proc_getattr,
6959 .setattr = nfs4_proc_setattr,
6960 .lookup = nfs4_proc_lookup,
6961 .access = nfs4_proc_access,
6962 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 .create = nfs4_proc_create,
6964 .remove = nfs4_proc_remove,
6965 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006966 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 .unlink_done = nfs4_proc_unlink_done,
6968 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006969 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006970 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006971 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 .link = nfs4_proc_link,
6973 .symlink = nfs4_proc_symlink,
6974 .mkdir = nfs4_proc_mkdir,
6975 .rmdir = nfs4_proc_remove,
6976 .readdir = nfs4_proc_readdir,
6977 .mknod = nfs4_proc_mknod,
6978 .statfs = nfs4_proc_statfs,
6979 .fsinfo = nfs4_proc_fsinfo,
6980 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006981 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 .decode_dirent = nfs4_decode_dirent,
6983 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006984 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006985 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006986 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006988 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006989 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006990 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006992 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006993 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006995 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006996 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006997 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006998 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006999 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007000 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007001 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007002 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007003 .create_server = nfs4_create_server,
7004 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005};
7006
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007007static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7008 .prefix = XATTR_NAME_NFSV4_ACL,
7009 .list = nfs4_xattr_list_nfs4_acl,
7010 .get = nfs4_xattr_get_nfs4_acl,
7011 .set = nfs4_xattr_set_nfs4_acl,
7012};
7013
7014const struct xattr_handler *nfs4_xattr_handlers[] = {
7015 &nfs4_xattr_nfs4_acl_handler,
7016 NULL
7017};
7018
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019/*
7020 * Local variables:
7021 * c-basic-offset: 8
7022 * End:
7023 */