blob: dd3a43792df08e430ecf113a0ba2d0a075a053d9 [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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-06-16 09:52:26 -0400676 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .seq_args = args,
678 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 };
680 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000681 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
685 };
686
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 task = rpc_run_task(&task_setup);
688 if (IS_ERR(task))
689 ret = PTR_ERR(task);
690 else {
691 ret = task->tk_status;
692 rpc_put_task(task);
693 }
694 return ret;
695}
696
Trond Myklebustdf896452010-06-16 09:52:26 -0400697#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400698static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500699void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
701{
702}
703
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400704static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
705{
706}
707
708
Trond Myklebust14516c32010-07-31 14:29:06 -0400709static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400711{
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714#endif /* CONFIG_NFS_V4_1 */
715
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400716static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724}
725
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400734 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400736 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400738
Trond Myklebust38478b22006-05-25 01:40:57 -0400739static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700745 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400746 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700747 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000748 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400753 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400762 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100766 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400767 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100768 int rpc_status;
769 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770};
771
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400772static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
774{
775 if (err != -EINVAL)
776 return false;
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
778 return false;
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
781 return true;
782}
783
784static enum open_claim_type4
785nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
787{
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
789 return claim;
790 switch (claim) {
791 default:
792 return claim;
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
799 }
800}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801
802static void nfs4_init_opendata_res(struct nfs4_opendata *p)
803{
804 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400807 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400808 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400809 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811}
812
Al Viro82a2c1b2011-06-22 18:30:55 -0400813static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400815 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400816 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818{
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
823
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (p == NULL)
826 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p->o_arg.seqid == NULL)
829 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
841 * are cached */
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700844 }
David Howells7539bba2006-08-22 20:06:09 -0400845 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
858 break;
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
864 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500865 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500866 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400867
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500870
871 verf[0] = jiffies;
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400879 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 return p;
882err_free:
883 kfree(p);
884err:
885 dput(parent);
886 return NULL;
887}
888
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400889static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400893 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894
895 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 nfs4_put_state_owner(p->owner);
899 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400900 dput(p->dentry);
901 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500902 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kfree(p);
904}
905
906static void nfs4_opendata_put(struct nfs4_opendata *p)
907{
908 if (p != NULL)
909 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910}
911
Trond Myklebust06f814a2006-01-03 09:55:07 +0100912static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
913{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100914 int ret;
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917 return ret;
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921{
922 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923
Trond Myklebust536e43d2012-01-17 22:04:26 -0500924 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 goto out;
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400930 break;
931 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500945 if (delegation == NULL)
946 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
952 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500953 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 1;
955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100958{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
968 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
986 }
987}
988
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400990{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 /*
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1001 */
1002 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1006 }
1007 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001012 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001016{
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001022
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1027
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001032 goto no_delegation_unlock;
1033
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
Trond Myklebustb7391f42008-12-23 15:21:52 -05001039 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001041 ret = 1;
1042no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044no_delegation:
1045 rcu_read_unlock();
1046
1047 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050 }
1051
1052 return ret;
1053}
1054
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057{
1058 struct nfs_delegation *delegation;
1059
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 rcu_read_unlock();
1064 return;
1065 }
1066 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001067 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068}
1069
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1079
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 goto out_return_state;
1087 }
1088 spin_unlock(&state->owner->so_lock);
1089 }
Trond Myklebust34310432008-12-23 15:21:38 -05001090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001092 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001097 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001106 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
1109 return ERR_PTR(ret);
1110out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1113}
1114
Andy Adamsone23008e2012-10-02 21:07:32 -04001115static void
1116nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1117{
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1121
1122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1131 clp->cl_hostname);
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140}
1141
1142/*
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1145 */
1146static struct nfs4_state *
1147_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1148{
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1151 int ret;
1152
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1155 goto err;
1156 }
1157
1158 ret = -ESTALE;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1162 goto err;
1163
1164 ret = -ENOMEM;
1165 state = nfs4_get_open_state(inode, data->owner);
1166 if (state == NULL)
1167 goto err;
1168
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1170 if (ret)
1171 goto err;
1172
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1176 data->o_arg.fmode);
1177
1178 return state;
1179err:
1180 return ERR_PTR(ret);
1181
1182}
1183
1184static struct nfs4_state *
1185_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001186{
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001189 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001192 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 goto out;
1194 }
1195
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001201 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
1203 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 state = nfs4_get_open_state(inode, data->owner);
1205 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001209 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001213 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215err_put_inode:
1216 iput(inode);
1217err:
1218 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219}
1220
Andy Adamsone23008e2012-10-02 21:07:32 -04001221static struct nfs4_state *
1222nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1223{
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1227}
1228
Trond Myklebust864472e2006-01-03 09:55:15 +01001229static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1230{
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1233
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1237 continue;
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1240 return ctx;
1241 }
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1244}
1245
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001246static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001248{
1249 struct nfs4_opendata *opendata;
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1257 return opendata;
1258}
1259
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001261{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 int ret;
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001270 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 if (ret != 0)
1272 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001273 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001276 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 return 0;
1279}
1280
1281static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1282{
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 int ret;
1285
1286 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 smp_rmb();
1289 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 if (ret != 0)
1293 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 if (newstate != state)
1295 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 }
1297 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 if (ret != 0)
1301 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001302 if (newstate != state)
1303 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 }
1305 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 if (ret != 0)
1309 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 if (newstate != state)
1311 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001313 /*
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1316 */
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001319 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001322 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001323 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * OPEN_RECLAIM:
1329 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001331static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001333 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int status;
1337
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001345 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001349 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return status;
1351}
1352
Trond Myklebust539cd032007-06-05 11:46:42 -04001353static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1357 int err;
1358 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001359 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1361 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001362 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001363 break;
1364 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } while (exception.retry);
1366 return err;
1367}
1368
Trond Myklebust864472e2006-01-03 09:55:15 +01001369static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1370{
1371 struct nfs_open_context *ctx;
1372 int ret;
1373
1374 ctx = nfs4_state_find_open_context(state);
1375 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001376 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 put_nfs_open_context(ctx);
1379 return ret;
1380}
1381
Trond Myklebust13437e12007-07-06 15:10:43 -04001382static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001384 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001387 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebustccb46e22013-03-18 12:50:59 -04001388 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001389 if (IS_ERR(opendata))
1390 return PTR_ERR(opendata);
Trond Myklebustf597c532012-03-04 18:13:56 -05001391 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001392 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Trond Myklebust13437e12007-07-06 15:10:43 -04001397int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001400 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int err;
1402 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001403 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 switch (err) {
1405 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001406 case -ENOENT:
1407 case -ESTALE:
1408 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001409 case -NFS4ERR_BADSESSION:
1410 case -NFS4ERR_BADSLOT:
1411 case -NFS4ERR_BAD_HIGH_SLOT:
1412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1413 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001414 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001415 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001416 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case -NFS4ERR_STALE_CLIENTID:
1419 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001420 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case -NFS4ERR_EXPIRED:
1422 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001423 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001424 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001425 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001426 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001427 case -NFS4ERR_ADMIN_REVOKED:
1428 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001429 nfs_inode_find_state_and_recover(state->inode,
1430 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001431 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001432 case -ENOMEM:
1433 err = 0;
1434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001436 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 err = nfs4_handle_exception(server, err, &exception);
1438 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return err;
1441}
1442
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001443static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1444{
1445 struct nfs4_opendata *data = calldata;
1446
1447 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001448 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001449 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001451 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001452 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001453 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001454}
1455
1456static void nfs4_open_confirm_release(void *calldata)
1457{
1458 struct nfs4_opendata *data = calldata;
1459 struct nfs4_state *state = NULL;
1460
1461 /* If this request hasn't been cancelled, do nothing */
1462 if (data->cancelled == 0)
1463 goto out_free;
1464 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001465 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001466 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001468 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001469 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001470out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001471 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472}
1473
1474static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001475 .rpc_call_done = nfs4_open_confirm_done,
1476 .rpc_release = nfs4_open_confirm_release,
1477};
1478
1479/*
1480 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1481 */
1482static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1483{
1484 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1485 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001486 struct rpc_message msg = {
1487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1488 .rpc_argp = &data->c_arg,
1489 .rpc_resp = &data->c_res,
1490 .rpc_cred = data->owner->so_cred,
1491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001492 struct rpc_task_setup task_setup_data = {
1493 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001495 .callback_ops = &nfs4_open_confirm_ops,
1496 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001497 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001498 .flags = RPC_TASK_ASYNC,
1499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int status;
1501
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001502 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001503 data->rpc_done = 0;
1504 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001505 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001506 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001507 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001508 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001509 status = nfs4_wait_for_completion_rpc_task(task);
1510 if (status != 0) {
1511 data->cancelled = 1;
1512 smp_wmb();
1513 } else
1514 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001515 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return status;
1517}
1518
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521 struct nfs4_opendata *data = calldata;
1522 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001525 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001526 /*
1527 * Check if we still need to send an OPEN call, or if we can use
1528 * a delegation instead.
1529 */
1530 if (data->state != NULL) {
1531 struct nfs_delegation *delegation;
1532
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001533 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001534 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001535 rcu_read_lock();
1536 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001537 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1538 can_open_delegated(delegation, data->o_arg.fmode))
1539 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 rcu_read_unlock();
1541 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001542 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001543 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001544 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001546 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001547 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1548 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001549 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001550 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001551 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001552 &data->o_res.seq_res,
1553 task) != 0)
1554 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001555 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001556unlock_no_action:
1557 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001558out_no_action:
1559 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001560out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001561 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001562}
1563
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564static void nfs4_open_done(struct rpc_task *task, void *calldata)
1565{
1566 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001569
Trond Myklebust14516c32010-07-31 14:29:06 -04001570 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1571 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001572
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001574 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1575 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001576 case S_IFREG:
1577 break;
1578 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001580 break;
1581 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001583 break;
1584 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001586 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001587 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001588 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001589 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1590 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001591 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595static void nfs4_open_release(void *calldata)
1596{
1597 struct nfs4_opendata *data = calldata;
1598 struct nfs4_state *state = NULL;
1599
1600 /* If this request hasn't been cancelled, do nothing */
1601 if (data->cancelled == 0)
1602 goto out_free;
1603 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001604 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 goto out_free;
1606 /* In case we need an open_confirm, no cleanup! */
1607 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1608 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001610 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001611 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001613 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614}
1615
1616static const struct rpc_call_ops nfs4_open_ops = {
1617 .rpc_call_prepare = nfs4_open_prepare,
1618 .rpc_call_done = nfs4_open_done,
1619 .rpc_release = nfs4_open_release,
1620};
1621
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001622static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623{
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1628 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001629 struct rpc_message msg = {
1630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1631 .rpc_argp = o_arg,
1632 .rpc_resp = o_res,
1633 .rpc_cred = data->owner->so_cred,
1634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001635 struct rpc_task_setup task_setup_data = {
1636 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001638 .callback_ops = &nfs4_open_ops,
1639 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001640 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001641 .flags = RPC_TASK_ASYNC,
1642 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643 int status;
1644
Trond Myklebust9d12b212012-01-17 22:04:25 -05001645 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001646 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001647 data->rpc_done = 0;
1648 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001649 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001650 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001651 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001652 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001653 if (IS_ERR(task))
1654 return PTR_ERR(task);
1655 status = nfs4_wait_for_completion_rpc_task(task);
1656 if (status != 0) {
1657 data->cancelled = 1;
1658 smp_wmb();
1659 } else
1660 status = data->rpc_status;
1661 rpc_put_task(task);
1662
1663 return status;
1664}
1665
1666static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1667{
1668 struct inode *dir = data->dir->d_inode;
1669 struct nfs_openres *o_res = &data->o_res;
1670 int status;
1671
1672 status = nfs4_run_open_task(data, 1);
1673 if (status != 0 || !data->rpc_done)
1674 return status;
1675
Trond Myklebust6926afd2012-01-07 13:22:46 -05001676 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1677
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1679 status = _nfs4_proc_open_confirm(data);
1680 if (status != 0)
1681 return status;
1682 }
1683
1684 return status;
1685}
1686
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001687static int nfs4_opendata_access(struct rpc_cred *cred,
1688 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001689 struct nfs4_state *state, fmode_t fmode,
1690 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001691{
1692 struct nfs_access_entry cache;
1693 u32 mask;
1694
1695 /* access call failed or for some reason the server doesn't
1696 * support any access modes -- defer access call until later */
1697 if (opendata->o_res.access_supported == 0)
1698 return 0;
1699
1700 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001701 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001702 * write mode bits, but POSIX allows the creating process to write.
1703 * use openflags to check for exec, because fmode won't
1704 * always have FMODE_EXEC set when file open for exec. */
1705 if (openflags & __FMODE_EXEC) {
1706 /* ONLY check for exec rights */
1707 mask = MAY_EXEC;
1708 } else if (fmode & FMODE_READ)
1709 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001710
1711 cache.cred = cred;
1712 cache.jiffies = jiffies;
1713 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1714 nfs_access_add_cache(state->inode, &cache);
1715
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001716 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001717 return 0;
1718
1719 /* even though OPEN succeeded, access is denied. Close the file */
1720 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001721 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001722}
1723
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001724/*
1725 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1726 */
1727static int _nfs4_proc_open(struct nfs4_opendata *data)
1728{
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 int status;
1734
1735 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001736 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001738 if (status != 0) {
1739 if (status == -NFS4ERR_BADNAME &&
1740 !(o_arg->open_flags & O_CREAT))
1741 return -ENOENT;
1742 return status;
1743 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744
Trond Myklebust6926afd2012-01-07 13:22:46 -05001745 nfs_fattr_map_and_free_names(server, &data->f_attr);
1746
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001747 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001748 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001749 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1750 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001752 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001757 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Andy Adamsond83217c2011-03-01 01:34:17 +00001761static int nfs4_recover_expired_lease(struct nfs_server *server)
1762{
1763 return nfs4_client_recover_expired_lease(server->nfs_client);
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
1767 * OPEN_EXPIRED:
1768 * reclaim state on the server after a network partition.
1769 * Assumes caller holds the appropriate lock
1770 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001771static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001773 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001777 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001781 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001782 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001783 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001787static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001788{
Trond Myklebust539cd032007-06-05 11:46:42 -04001789 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001790 struct nfs4_exception exception = { };
1791 int err;
1792
1793 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001794 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001795 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1796 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001797 switch (err) {
1798 default:
1799 goto out;
1800 case -NFS4ERR_GRACE:
1801 case -NFS4ERR_DELAY:
1802 nfs4_handle_exception(server, err, &exception);
1803 err = 0;
1804 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001805 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001806out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001807 return err;
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001813 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Trond Myklebust864472e2006-01-03 09:55:15 +01001815 ctx = nfs4_state_find_open_context(state);
1816 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001817 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001818 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 put_nfs_open_context(ctx);
1820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001823#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001824static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001825{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001826 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001827 nfs4_stateid *stateid = &state->stateid;
1828 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001829
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001830 /* If a state reset has been done, test_stateid is unneeded */
1831 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1832 return;
1833
1834 status = nfs41_test_stateid(server, stateid);
1835 if (status != NFS_OK) {
1836 /* Free the stateid unless the server explicitly
1837 * informs us the stateid is unrecognized. */
1838 if (status != -NFS4ERR_BAD_STATEID)
1839 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001840 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001841
Bryan Schumaker69388672012-09-26 15:25:52 -04001842 write_seqlock(&state->seqlock);
1843 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1844 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001845 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1846 }
1847}
1848
1849/**
1850 * nfs41_check_open_stateid - possibly free an open stateid
1851 *
1852 * @state: NFSv4 state for an inode
1853 *
1854 * Returns NFS_OK if recovery for this stateid is now finished.
1855 * Otherwise a negative NFS4ERR value is returned.
1856 */
1857static int nfs41_check_open_stateid(struct nfs4_state *state)
1858{
1859 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001860 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001861 int status;
1862
1863 /* If a state reset has been done, test_stateid is unneeded */
1864 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1865 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1867 return -NFS4ERR_BAD_STATEID;
1868
1869 status = nfs41_test_stateid(server, stateid);
1870 if (status != NFS_OK) {
1871 /* Free the stateid unless the server explicitly
1872 * informs us the stateid is unrecognized. */
1873 if (status != -NFS4ERR_BAD_STATEID)
1874 nfs41_free_stateid(server, stateid);
1875
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001879 }
1880 return status;
1881}
1882
1883static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1884{
Chuck Levereb64cf92012-07-11 16:30:05 -04001885 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001886
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001887 nfs41_clear_delegation_stateid(state);
1888 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001889 if (status != NFS_OK)
1890 status = nfs4_open_expired(sp, state);
1891 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001892}
1893#endif
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001896 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1897 * fields corresponding to attributes that were used to store the verifier.
1898 * Make sure we clobber those fields in the later setattr call
1899 */
1900static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1901{
1902 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1903 !(sattr->ia_valid & ATTR_ATIME_SET))
1904 sattr->ia_valid |= ATTR_ATIME;
1905
1906 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1907 !(sattr->ia_valid & ATTR_MTIME_SET))
1908 sattr->ia_valid |= ATTR_MTIME;
1909}
1910
Trond Myklebustc21443c2013-02-07 14:26:21 -05001911static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1912 fmode_t fmode,
1913 int flags,
1914 struct nfs4_state **res)
1915{
1916 struct nfs4_state_owner *sp = opendata->owner;
1917 struct nfs_server *server = sp->so_server;
1918 struct nfs4_state *state;
1919 unsigned int seq;
1920 int ret;
1921
1922 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1923
1924 ret = _nfs4_proc_open(opendata);
1925 if (ret != 0)
1926 goto out;
1927
1928 state = nfs4_opendata_to_nfs4_state(opendata);
1929 ret = PTR_ERR(state);
1930 if (IS_ERR(state))
1931 goto out;
1932 if (server->caps & NFS_CAP_POSIX_LOCK)
1933 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1934
1935 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1936 if (ret != 0)
1937 goto out;
1938
Trond Myklebust91876b12013-03-28 14:01:33 -04001939 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001940 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001941 *res = state;
1942out:
1943 return ret;
1944}
1945
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001946/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001947 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Andy Adamson82be4172012-05-23 05:02:35 -04001949static int _nfs4_do_open(struct inode *dir,
1950 struct dentry *dentry,
1951 fmode_t fmode,
1952 int flags,
1953 struct iattr *sattr,
1954 struct rpc_cred *cred,
1955 struct nfs4_state **res,
1956 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 struct nfs4_state_owner *sp;
1959 struct nfs4_state *state = NULL;
1960 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001961 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001962 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001963 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001967 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1968 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1970 goto out_err;
1971 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001972 status = nfs4_recover_expired_lease(server);
1973 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001974 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001975 if (dentry->d_inode != NULL)
1976 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001977 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001978 if (dentry->d_inode)
1979 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001980 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001981 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001982 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001983 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Andy Adamson82be4172012-05-23 05:02:35 -04001985 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1986 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1987 if (!opendata->f_attr.mdsthreshold)
1988 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001989 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001990 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001991 if (dentry->d_inode != NULL)
1992 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001993
Trond Myklebustc21443c2013-02-07 14:26:21 -05001994 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001995 if (status != 0)
1996 goto err_opendata_put;
1997
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001998 if (opendata->o_arg.open_flags & O_EXCL) {
1999 nfs4_exclusive_attrset(opendata, sattr);
2000
2001 nfs_fattr_init(opendata->o_res.f_attr);
2002 status = nfs4_do_setattr(state->inode, cred,
2003 opendata->o_res.f_attr, sattr,
2004 state);
2005 if (status == 0)
2006 nfs_setattr_update_inode(state->inode, sattr);
2007 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2008 }
Andy Adamson82be4172012-05-23 05:02:35 -04002009
2010 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2011 *ctx_th = opendata->f_attr.mdsthreshold;
2012 else
2013 kfree(opendata->f_attr.mdsthreshold);
2014 opendata->f_attr.mdsthreshold = NULL;
2015
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002016 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *res = state;
2019 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002021 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002022 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002023err_put_state_owner:
2024 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 *res = NULL;
2027 return status;
2028}
2029
2030
Andy Adamson82be4172012-05-23 05:02:35 -04002031static struct nfs4_state *nfs4_do_open(struct inode *dir,
2032 struct dentry *dentry,
2033 fmode_t fmode,
2034 int flags,
2035 struct iattr *sattr,
2036 struct rpc_cred *cred,
2037 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002039 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs4_exception exception = { };
2041 struct nfs4_state *res;
2042 int status;
2043
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002044 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002046 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2047 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (status == 0)
2049 break;
2050 /* NOTE: BAD_SEQID means the server and client disagree about the
2051 * book-keeping w.r.t. state-changing operations
2052 * (OPEN/CLOSE/LOCK/LOCKU...)
2053 * It is actually a sign of a bug on the client or on the server.
2054 *
2055 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002056 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 * have unhashed the old state_owner for us, and that we can
2058 * therefore safely retry using a new one. We should still warn
2059 * the user though...
2060 */
2061 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002062 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002063 " returned a bad sequence-id error!\n",
2064 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 exception.retry = 1;
2066 continue;
2067 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002068 /*
2069 * BAD_STATEID on OPEN means that the server cancelled our
2070 * state before it received the OPEN_CONFIRM.
2071 * Recover by retrying the request as per the discussion
2072 * on Page 181 of RFC3530.
2073 */
2074 if (status == -NFS4ERR_BAD_STATEID) {
2075 exception.retry = 1;
2076 continue;
2077 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002078 if (status == -EAGAIN) {
2079 /* We must have found a delegation */
2080 exception.retry = 1;
2081 continue;
2082 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002083 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2084 continue;
2085 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 status, &exception));
2087 } while (exception.retry);
2088 return res;
2089}
2090
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002091static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2092 struct nfs_fattr *fattr, struct iattr *sattr,
2093 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002095 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002097 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 .iap = sattr,
2099 .server = server,
2100 .bitmask = server->attr_bitmask,
2101 };
2102 struct nfs_setattrres res = {
2103 .fattr = fattr,
2104 .server = server,
2105 };
2106 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2108 .rpc_argp = &arg,
2109 .rpc_resp = &res,
2110 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002112 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002113 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Trond Myklebust0e574af2005-10-27 22:12:38 -04002115 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Trond Myklebust5d422302013-03-14 16:57:48 -04002117 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002118 struct nfs_lockowner lockowner = {
2119 .l_owner = current->files,
2120 .l_pid = current->tgid,
2121 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002122 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002123 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002124 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2125 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002126 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002127 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002128 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Bryan Schumaker7c513052011-03-24 17:12:24 +00002130 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 if (status == 0 && state != NULL)
2132 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002133 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002136static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2137 struct nfs_fattr *fattr, struct iattr *sattr,
2138 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002140 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002141 struct nfs4_exception exception = {
2142 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002143 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002144 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int err;
2146 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002147 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2148 switch (err) {
2149 case -NFS4ERR_OPENMODE:
2150 if (state && !(state->state & FMODE_WRITE)) {
2151 err = -EBADF;
2152 if (sattr->ia_valid & ATTR_OPEN)
2153 err = -EACCES;
2154 goto out;
2155 }
2156 }
2157 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002159out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return err;
2161}
2162
2163struct nfs4_closedata {
2164 struct inode *inode;
2165 struct nfs4_state *state;
2166 struct nfs_closeargs arg;
2167 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002168 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002169 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002170 bool roc;
2171 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172};
2173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002174static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002175{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002176 struct nfs4_closedata *calldata = data;
2177 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002178 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002179
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 if (calldata->roc)
2181 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002182 nfs4_put_open_state(calldata->state);
2183 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002184 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002185 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002186 kfree(calldata);
2187}
2188
Trond Myklebust88069f72009-12-08 08:33:16 -05002189static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2190 fmode_t fmode)
2191{
2192 spin_lock(&state->owner->so_lock);
2193 if (!(fmode & FMODE_READ))
2194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2195 if (!(fmode & FMODE_WRITE))
2196 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2198 spin_unlock(&state->owner->so_lock);
2199}
2200
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002203 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs_server *server = NFS_SERVER(calldata->inode);
2206
Chuck Levera3ca5652012-03-01 17:00:40 -05002207 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2209 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* hmm. we are done with the inode, and in the process of freeing
2211 * the state_owner. we keep this around to process errors
2212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 switch (task->tk_status) {
2214 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002215 if (calldata->roc)
2216 pnfs_roc_set_barrier(state->inode,
2217 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002218 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002219 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002220 nfs4_close_clear_stateid_flags(state,
2221 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002224 case -NFS4ERR_OLD_STATEID:
2225 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002227 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002230 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2231 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002233 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002234 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002235 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002238static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002240 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002241 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002242 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002243 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002244
Chuck Levera3ca5652012-03-01 17:00:40 -05002245 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002247 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002248
Trond Myklebust88069f72009-12-08 08:33:16 -05002249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2250 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002251 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002252 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002253 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002254 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002255 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2256 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2257 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002258 }
2259 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002260 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2262 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002263 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002264 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002265 if (!nfs4_valid_open_stateid(state))
2266 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002267 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002268
2269 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002270 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002271 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002272 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002273
Fred Isamanf7e89172011-01-06 11:36:32 +00002274 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002275 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002276 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002277 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002278 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002279 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002280
Trond Myklebust516a6af2005-10-27 22:12:41 -04002281 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002282 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002283 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002284 &calldata->arg.seq_args,
2285 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002286 task) != 0)
2287 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002288 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002289 return;
2290out_no_action:
2291 task->tk_action = NULL;
2292out_wait:
2293 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002296static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002297 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002298 .rpc_call_done = nfs4_close_done,
2299 .rpc_release = nfs4_free_closedata,
2300};
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/*
2303 * It is possible for data to be read/written from a mem-mapped file
2304 * after the sys_close call (which hits the vfs layer as a flush).
2305 * This means that we can't safely call nfsv4 close on a file until
2306 * the inode is cleared. This in turn means that we are not good
2307 * NFSv4 citizens - we do not indicate to the server to update the file's
2308 * share state even when we are done with one of the three share
2309 * stateid's in the inode.
2310 *
2311 * NOTE: Caller must be holding the sp->so_owner semaphore!
2312 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002313int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002315 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002317 struct nfs4_state_owner *sp = state->owner;
2318 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002319 struct rpc_message msg = {
2320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2321 .rpc_cred = state->owner->so_cred,
2322 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002323 struct rpc_task_setup task_setup_data = {
2324 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002326 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002327 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002328 .flags = RPC_TASK_ASYNC,
2329 };
Trond Myklebust95121352005-10-18 14:20:12 -07002330 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002332 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002334 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002335 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002336 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002338 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002339 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002341 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002342 if (calldata->arg.seqid == NULL)
2343 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002344 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002345 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002346 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002347 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002348 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002349 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002350 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002351
Trond Myklebust1174dd12010-12-21 10:52:24 -05002352 msg.rpc_argp = &calldata->arg;
2353 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002354 task_setup_data.callback_data = calldata;
2355 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002356 if (IS_ERR(task))
2357 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002358 status = 0;
2359 if (wait)
2360 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002361 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002362 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002363out_free_calldata:
2364 kfree(calldata);
2365out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002366 nfs4_put_open_state(state);
2367 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002368 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Trond Myklebust2b484292010-09-17 10:56:51 -04002371static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002372nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs4_state *state;
2375
Trond Myklebust565277f2007-10-15 18:17:53 -04002376 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002377 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2378 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002379 if (IS_ERR(state))
2380 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002381 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002382 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust1185a552009-12-03 15:54:02 -05002385static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002386{
2387 if (ctx->state == NULL)
2388 return;
2389 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002390 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002391 else
Al Viro643168c2011-06-22 18:20:23 -04002392 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2396{
Benny Halevy43652ad2009-04-01 09:21:54 -04002397 struct nfs4_server_caps_arg args = {
2398 .fhandle = fhandle,
2399 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct nfs4_server_caps_res res = {};
2401 struct rpc_message msg = {
2402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002403 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .rpc_resp = &res,
2405 };
2406 int status;
2407
Bryan Schumaker7c513052011-03-24 17:12:24 +00002408 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (status == 0) {
2410 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002411 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2412 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2413 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2414 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2415 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2417 server->caps |= NFS_CAP_ACLS;
2418 if (res.has_links != 0)
2419 server->caps |= NFS_CAP_HARDLINKS;
2420 if (res.has_symlinks != 0)
2421 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002422 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2423 server->caps |= NFS_CAP_FILEID;
2424 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2425 server->caps |= NFS_CAP_MODE;
2426 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2427 server->caps |= NFS_CAP_NLINK;
2428 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2429 server->caps |= NFS_CAP_OWNER;
2430 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2431 server->caps |= NFS_CAP_OWNER_GROUP;
2432 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2433 server->caps |= NFS_CAP_ATIME;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2435 server->caps |= NFS_CAP_CTIME;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2437 server->caps |= NFS_CAP_MTIME;
2438
Trond Myklebusta65318b2009-03-11 14:10:28 -04002439 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2440 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2441 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002443 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return status;
2447}
2448
Trond Myklebust55a97592006-06-09 09:34:19 -04002449int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(server,
2455 _nfs4_server_capabilities(server, fhandle),
2456 &exception);
2457 } while (exception.retry);
2458 return err;
2459}
2460
2461static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2462 struct nfs_fsinfo *info)
2463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs4_lookup_root_arg args = {
2465 .bitmask = nfs4_fattr_bitmap,
2466 };
2467 struct nfs4_lookup_res res = {
2468 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002469 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .fh = fhandle,
2471 };
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2474 .rpc_argp = &args,
2475 .rpc_resp = &res,
2476 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002477
Trond Myklebust0e574af2005-10-27 22:12:38 -04002478 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002479 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2483 struct nfs_fsinfo *info)
2484{
2485 struct nfs4_exception exception = { };
2486 int err;
2487 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002488 err = _nfs4_lookup_root(server, fhandle, info);
2489 switch (err) {
2490 case 0:
2491 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002492 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002493 default:
2494 err = nfs4_handle_exception(server, err, &exception);
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return err;
2499}
2500
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002501static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2502 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2503{
2504 struct rpc_auth *auth;
2505 int ret;
2506
2507 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002508 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002509 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002510 goto out;
2511 }
2512 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002513out:
2514 return ret;
2515}
2516
Chuck Lever9a744ba2013-03-16 15:56:02 -04002517/*
2518 * Retry pseudoroot lookup with various security flavors. We do this when:
2519 *
2520 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2521 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2522 *
2523 * Returns zero on success, or a negative NFS4ERR value, or a
2524 * negative errno value.
2525 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002526static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002527 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002529 /* Per 3530bis 15.33.5 */
2530 static const rpc_authflavor_t flav_array[] = {
2531 RPC_AUTH_GSS_KRB5P,
2532 RPC_AUTH_GSS_KRB5I,
2533 RPC_AUTH_GSS_KRB5,
2534 RPC_AUTH_NULL,
2535 };
2536 int status = -EPERM;
2537 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Chuck Lever9a744ba2013-03-16 15:56:02 -04002539 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002540 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002541 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002542 continue;
2543 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002544 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002545
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002546 /*
2547 * -EACCESS could mean that the user doesn't have correct permissions
2548 * to access the mount. It could also mean that we tried to mount
2549 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2550 * existing mount programs don't handle -EACCES very well so it should
2551 * be mapped to -EPERM instead.
2552 */
2553 if (status == -EACCES)
2554 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002555 return status;
2556}
2557
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002558static int nfs4_do_find_root_sec(struct nfs_server *server,
2559 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2560{
2561 int mv = server->nfs_client->cl_minorversion;
2562 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2563}
2564
2565/**
2566 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2567 * @server: initialized nfs_server handle
2568 * @fhandle: we fill in the pseudo-fs root file handle
2569 * @info: we fill in an FSINFO struct
2570 *
2571 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002572 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002573int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2574 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002575{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002576 int status;
2577
2578 status = nfs4_lookup_root(server, fhandle, info);
2579 if ((status == -NFS4ERR_WRONGSEC) &&
2580 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2581 status = nfs4_do_find_root_sec(server, fhandle, info);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (status == 0)
2584 status = nfs4_server_capabilities(server, fhandle);
2585 if (status == 0)
2586 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002587
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002588 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
Bryan Schumakerbae36242012-05-10 15:07:31 -04002591static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2592 struct nfs_fsinfo *info)
2593{
2594 int error;
2595 struct nfs_fattr *fattr = info->fattr;
2596
2597 error = nfs4_server_capabilities(server, mntfh);
2598 if (error < 0) {
2599 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2600 return error;
2601 }
2602
2603 error = nfs4_proc_getattr(server, mntfh, fattr);
2604 if (error < 0) {
2605 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2606 return error;
2607 }
2608
2609 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2610 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2611 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2612
2613 return error;
2614}
2615
Manoj Naik6b97fd32006-06-09 09:34:29 -04002616/*
2617 * Get locations and (maybe) other attributes of a referral.
2618 * Note that we'll actually follow the referral later when
2619 * we detect fsid mismatch in inode revalidation
2620 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002621static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2622 const struct qstr *name, struct nfs_fattr *fattr,
2623 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002624{
2625 int status = -ENOMEM;
2626 struct page *page = NULL;
2627 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002628
2629 page = alloc_page(GFP_KERNEL);
2630 if (page == NULL)
2631 goto out;
2632 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2633 if (locations == NULL)
2634 goto out;
2635
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002636 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 if (status != 0)
2638 goto out;
2639 /* Make sure server returned a different fsid for the referral */
2640 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002641 dprintk("%s: server did not return a different fsid for"
2642 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643 status = -EIO;
2644 goto out;
2645 }
Andy Adamson533eb462011-06-13 18:25:56 -04002646 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2647 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002648
Andy Adamson533eb462011-06-13 18:25:56 -04002649 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002650 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002651 memset(fhandle, 0, sizeof(struct nfs_fh));
2652out:
2653 if (page)
2654 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002655 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002656 return status;
2657}
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2660{
2661 struct nfs4_getattr_arg args = {
2662 .fh = fhandle,
2663 .bitmask = server->attr_bitmask,
2664 };
2665 struct nfs4_getattr_res res = {
2666 .fattr = fattr,
2667 .server = server,
2668 };
2669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2671 .rpc_argp = &args,
2672 .rpc_resp = &res,
2673 };
2674
Trond Myklebust0e574af2005-10-27 22:12:38 -04002675 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002676 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2680{
2681 struct nfs4_exception exception = { };
2682 int err;
2683 do {
2684 err = nfs4_handle_exception(server,
2685 _nfs4_proc_getattr(server, fhandle, fattr),
2686 &exception);
2687 } while (exception.retry);
2688 return err;
2689}
2690
2691/*
2692 * The file is not closed if it is opened due to the a request to change
2693 * the size of the file. The open call will not be needed once the
2694 * VFS layer lookup-intents are implemented.
2695 *
2696 * Close is called when the inode is destroyed.
2697 * If we haven't opened the file for O_WRONLY, we
2698 * need to in the size_change case to obtain a stateid.
2699 *
2700 * Got race?
2701 * Because OPEN is always done by name in nfsv4, it is
2702 * possible that we opened a different file by the same
2703 * name. We can recognize this race condition, but we
2704 * can't do anything about it besides returning an error.
2705 *
2706 * This will be fixed with VFS changes (lookup-intent).
2707 */
2708static int
2709nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2710 struct iattr *sattr)
2711{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002712 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002713 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002714 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 int status;
2716
Benny Halevy8a1636c2010-07-14 15:43:57 -04002717 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002718 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002719
Trond Myklebust0e574af2005-10-27 22:12:38 -04002720 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Andy Adamson26699402012-05-30 16:12:24 -04002722 /* Deal with open(O_TRUNC) */
2723 if (sattr->ia_valid & ATTR_OPEN)
2724 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2725
2726 /* Optimization: if the end result is no change, don't RPC */
2727 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2728 return 0;
2729
Trond Myklebustd5308382005-11-04 15:33:38 -05002730 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002731 if (sattr->ia_valid & ATTR_FILE) {
2732 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002733
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002734 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002735 if (ctx) {
2736 cred = ctx->cred;
2737 state = ctx->state;
2738 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002739 }
2740
2741 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002742 if (status == 0)
2743 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return status;
2745}
2746
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002747static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2748 const struct qstr *name, struct nfs_fh *fhandle,
2749 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002750{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002751 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002752 int status;
2753 struct nfs4_lookup_arg args = {
2754 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002755 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002756 .name = name,
2757 };
2758 struct nfs4_lookup_res res = {
2759 .server = server,
2760 .fattr = fattr,
2761 .fh = fhandle,
2762 };
2763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2765 .rpc_argp = &args,
2766 .rpc_resp = &res,
2767 };
2768
2769 nfs_fattr_init(fattr);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002772 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 dprintk("NFS reply lookup: %d\n", status);
2774 return status;
2775}
2776
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002777static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002778{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002779 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002780 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002781 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2782 fattr->nlink = 2;
2783}
2784
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002785static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2786 struct qstr *name, struct nfs_fh *fhandle,
2787 struct nfs_fattr *fattr)
2788{
2789 struct nfs4_exception exception = { };
2790 struct rpc_clnt *client = *clnt;
2791 int err;
2792 do {
2793 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2794 switch (err) {
2795 case -NFS4ERR_BADNAME:
2796 err = -ENOENT;
2797 goto out;
2798 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002799 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002800 goto out;
2801 case -NFS4ERR_WRONGSEC:
2802 err = -EPERM;
2803 if (client != *clnt)
2804 goto out;
2805
2806 client = nfs4_create_sec_client(client, dir, name);
2807 if (IS_ERR(client))
2808 return PTR_ERR(client);
2809
2810 exception.retry = 1;
2811 break;
2812 default:
2813 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2814 }
2815 } while (exception.retry);
2816
2817out:
2818 if (err == 0)
2819 *clnt = client;
2820 else if (client != *clnt)
2821 rpc_shutdown_client(client);
2822
2823 return err;
2824}
2825
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002826static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002829 int status;
2830 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002831
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2833 if (client != NFS_CLIENT(dir)) {
2834 rpc_shutdown_client(client);
2835 nfs_fixup_secinfo_attributes(fattr);
2836 }
2837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002840struct rpc_clnt *
2841nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2842 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2843{
2844 int status;
2845 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2846
2847 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2848 if (status < 0) {
2849 rpc_shutdown_client(client);
2850 return ERR_PTR(status);
2851 }
2852 return client;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2856{
Trond Myklebust76b32992007-08-10 17:45:11 -04002857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct nfs4_accessargs args = {
2859 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002860 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002862 struct nfs4_accessres res = {
2863 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2867 .rpc_argp = &args,
2868 .rpc_resp = &res,
2869 .rpc_cred = entry->cred,
2870 };
2871 int mode = entry->mask;
2872 int status;
2873
2874 /*
2875 * Determine which access bits we want to ask for...
2876 */
2877 if (mode & MAY_READ)
2878 args.access |= NFS4_ACCESS_READ;
2879 if (S_ISDIR(inode->i_mode)) {
2880 if (mode & MAY_WRITE)
2881 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2882 if (mode & MAY_EXEC)
2883 args.access |= NFS4_ACCESS_LOOKUP;
2884 } else {
2885 if (mode & MAY_WRITE)
2886 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2887 if (mode & MAY_EXEC)
2888 args.access |= NFS4_ACCESS_EXECUTE;
2889 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002890
2891 res.fattr = nfs_alloc_fattr();
2892 if (res.fattr == NULL)
2893 return -ENOMEM;
2894
Bryan Schumaker7c513052011-03-24 17:12:24 +00002895 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002897 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002898 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002900 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(inode),
2910 _nfs4_proc_access(inode, entry),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916/*
2917 * TODO: For the time being, we don't try to get any attributes
2918 * along with any of the zero-copy operations READ, READDIR,
2919 * READLINK, WRITE.
2920 *
2921 * In the case of the first three, we want to put the GETATTR
2922 * after the read-type operation -- this is because it is hard
2923 * to predict the length of a GETATTR response in v4, and thus
2924 * align the READ data correctly. This means that the GETATTR
2925 * may end up partially falling into the page cache, and we should
2926 * shift it into the 'tail' of the xdr_buf before processing.
2927 * To do this efficiently, we need to know the total length
2928 * of data received, which doesn't seem to be available outside
2929 * of the RPC layer.
2930 *
2931 * In the case of WRITE, we also want to put the GETATTR after
2932 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002933 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 *
2935 * Both of these changes to the XDR layer would in fact be quite
2936 * minor, but I decided to leave them for a subsequent patch.
2937 */
2938static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2939 unsigned int pgbase, unsigned int pglen)
2940{
2941 struct nfs4_readlink args = {
2942 .fh = NFS_FH(inode),
2943 .pgbase = pgbase,
2944 .pglen = pglen,
2945 .pages = &page,
2946 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002947 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2950 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002951 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 };
2953
Bryan Schumaker7c513052011-03-24 17:12:24 +00002954 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 -07002955}
2956
2957static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2958 unsigned int pgbase, unsigned int pglen)
2959{
2960 struct nfs4_exception exception = { };
2961 int err;
2962 do {
2963 err = nfs4_handle_exception(NFS_SERVER(inode),
2964 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2965 &exception);
2966 } while (exception.retry);
2967 return err;
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002971 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static int
2974nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002975 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002977 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 int status = 0;
2980
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002981 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2982 if (IS_ERR(ctx))
2983 return PTR_ERR(ctx);
2984
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002985 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002986 state = nfs4_do_open(dir, dentry, ctx->mode,
2987 flags, sattr, ctx->cred,
2988 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002989 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (IS_ERR(state)) {
2991 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002994 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002995 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002996 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002998 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return status;
3000}
3001
3002static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3003{
Trond Myklebust16e42952005-10-27 22:12:44 -04003004 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003005 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003007 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003009 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003010 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3014 .rpc_argp = &args,
3015 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003017 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003018
Bryan Schumaker7c513052011-03-24 17:12:24 +00003019 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003020 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003021 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return status;
3023}
3024
3025static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dir),
3031 _nfs4_proc_remove(dir, name),
3032 &exception);
3033 } while (exception.retry);
3034 return err;
3035}
3036
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003037static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003039 struct nfs_server *server = NFS_SERVER(dir);
3040 struct nfs_removeargs *args = msg->rpc_argp;
3041 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003043 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003045 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003048static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3049{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003050 nfs4_setup_sequence(NFS_SERVER(data->dir),
3051 &data->args.seq_args,
3052 &data->res.seq_res,
3053 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003056static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3059
Trond Myklebust14516c32010-07-31 14:29:06 -04003060 if (!nfs4_sequence_done(task, &res->seq_res))
3061 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003062 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003063 return 0;
3064 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Jeff Laytond3d41522010-09-17 17:31:57 -04003068static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3069{
3070 struct nfs_server *server = NFS_SERVER(dir);
3071 struct nfs_renameargs *arg = msg->rpc_argp;
3072 struct nfs_renameres *res = msg->rpc_resp;
3073
3074 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003075 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003076 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003077}
3078
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003079static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3080{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003081 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3082 &data->args.seq_args,
3083 &data->res.seq_res,
3084 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003085}
3086
3087static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3088 struct inode *new_dir)
3089{
3090 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3091
3092 if (!nfs4_sequence_done(task, &res->seq_res))
3093 return 0;
3094 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3095 return 0;
3096
3097 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003098 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003099 return 1;
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3103 struct inode *new_dir, struct qstr *new_name)
3104{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003105 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003106 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 .old_dir = NFS_FH(old_dir),
3108 .new_dir = NFS_FH(new_dir),
3109 .old_name = old_name,
3110 .new_name = new_name,
3111 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003112 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003113 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003114 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct rpc_message msg = {
3116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3117 .rpc_argp = &arg,
3118 .rpc_resp = &res,
3119 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003120 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Bryan Schumaker7c513052011-03-24 17:12:24 +00003122 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (!status) {
3124 update_changeattr(old_dir, &res.old_cinfo);
3125 update_changeattr(new_dir, &res.new_cinfo);
3126 }
3127 return status;
3128}
3129
3130static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3131 struct inode *new_dir, struct qstr *new_name)
3132{
3133 struct nfs4_exception exception = { };
3134 int err;
3135 do {
3136 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3137 _nfs4_proc_rename(old_dir, old_name,
3138 new_dir, new_name),
3139 &exception);
3140 } while (exception.retry);
3141 return err;
3142}
3143
3144static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3145{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003146 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct nfs4_link_arg arg = {
3148 .fh = NFS_FH(inode),
3149 .dir_fh = NFS_FH(dir),
3150 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003153 struct nfs4_link_res res = {
3154 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3158 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003159 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003161 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Trond Myklebust136f2622010-04-16 16:22:49 -04003163 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003164 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003165 goto out;
3166
Bryan Schumaker7c513052011-03-24 17:12:24 +00003167 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003168 if (!status) {
3169 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003170 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003171 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003172out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003173 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return status;
3175}
3176
3177static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3178{
3179 struct nfs4_exception exception = { };
3180 int err;
3181 do {
3182 err = nfs4_handle_exception(NFS_SERVER(inode),
3183 _nfs4_proc_link(inode, dir, name),
3184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003189struct nfs4_createdata {
3190 struct rpc_message msg;
3191 struct nfs4_create_arg arg;
3192 struct nfs4_create_res res;
3193 struct nfs_fh fh;
3194 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195};
3196
3197static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3198 struct qstr *name, struct iattr *sattr, u32 ftype)
3199{
3200 struct nfs4_createdata *data;
3201
3202 data = kzalloc(sizeof(*data), GFP_KERNEL);
3203 if (data != NULL) {
3204 struct nfs_server *server = NFS_SERVER(dir);
3205
3206 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3207 data->msg.rpc_argp = &data->arg;
3208 data->msg.rpc_resp = &data->res;
3209 data->arg.dir_fh = NFS_FH(dir);
3210 data->arg.server = server;
3211 data->arg.name = name;
3212 data->arg.attrs = sattr;
3213 data->arg.ftype = ftype;
3214 data->arg.bitmask = server->attr_bitmask;
3215 data->res.server = server;
3216 data->res.fh = &data->fh;
3217 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003218 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 }
3220 return data;
3221}
3222
3223static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3224{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003225 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003226 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 if (status == 0) {
3228 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3230 }
3231 return status;
3232}
3233
3234static void nfs4_free_createdata(struct nfs4_createdata *data)
3235{
3236 kfree(data);
3237}
3238
Chuck Lever4f390c12006-08-22 20:06:22 -04003239static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003240 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 struct nfs4_createdata *data;
3243 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Chuck Lever94a6d752006-08-22 20:06:23 -04003245 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003246 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003247
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 status = -ENOMEM;
3249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3250 if (data == NULL)
3251 goto out;
3252
3253 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3254 data->arg.u.symlink.pages = &page;
3255 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257 status = nfs4_do_create(dir, dentry, data);
3258
3259 nfs4_free_createdata(data);
3260out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return status;
3262}
3263
Chuck Lever4f390c12006-08-22 20:06:22 -04003264static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003265 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 struct nfs4_exception exception = { };
3268 int err;
3269 do {
3270 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003271 _nfs4_proc_symlink(dir, dentry, page,
3272 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
3278static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr)
3280{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 struct nfs4_createdata *data;
3282 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3285 if (data == NULL)
3286 goto out;
3287
3288 status = nfs4_do_create(dir, dentry, data);
3289
3290 nfs4_free_createdata(data);
3291out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return status;
3293}
3294
3295static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3296 struct iattr *sattr)
3297{
3298 struct nfs4_exception exception = { };
3299 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003300
3301 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 do {
3303 err = nfs4_handle_exception(NFS_SERVER(dir),
3304 _nfs4_proc_mkdir(dir, dentry, sattr),
3305 &exception);
3306 } while (exception.retry);
3307 return err;
3308}
3309
3310static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003311 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct inode *dir = dentry->d_inode;
3314 struct nfs4_readdir_arg args = {
3315 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003316 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 .pgbase = 0,
3318 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003319 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003320 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 };
3322 struct nfs4_readdir_res res;
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3325 .rpc_argp = &args,
3326 .rpc_resp = &res,
3327 .rpc_cred = cred,
3328 };
3329 int status;
3330
Harvey Harrison3110ff82008-05-02 13:42:44 -07003331 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003332 dentry->d_parent->d_name.name,
3333 dentry->d_name.name,
3334 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003335 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003337 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 -05003338 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003339 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003340 status += args.pgbase;
3341 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003342
3343 nfs_invalidate_atime(dir);
3344
Harvey Harrison3110ff82008-05-02 13:42:44 -07003345 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return status;
3347}
3348
3349static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003350 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 struct nfs4_exception exception = { };
3353 int err;
3354 do {
3355 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3356 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003357 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 &exception);
3359 } while (exception.retry);
3360 return err;
3361}
3362
3363static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3364 struct iattr *sattr, dev_t rdev)
3365{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003366 struct nfs4_createdata *data;
3367 int mode = sattr->ia_mode;
3368 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003370 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3371 if (data == NULL)
3372 goto out;
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003375 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003377 data->arg.ftype = NF4BLK;
3378 data->arg.u.device.specdata1 = MAJOR(rdev);
3379 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
3381 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003382 data->arg.ftype = NF4CHR;
3383 data->arg.u.device.specdata1 = MAJOR(rdev);
3384 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003385 } else if (!S_ISSOCK(mode)) {
3386 status = -EINVAL;
3387 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003390 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003391out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003392 nfs4_free_createdata(data);
3393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return status;
3395}
3396
3397static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3398 struct iattr *sattr, dev_t rdev)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003402
3403 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 do {
3405 err = nfs4_handle_exception(NFS_SERVER(dir),
3406 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
3412static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fsstat *fsstat)
3414{
3415 struct nfs4_statfs_arg args = {
3416 .fh = fhandle,
3417 .bitmask = server->attr_bitmask,
3418 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003419 struct nfs4_statfs_res res = {
3420 .fsstat = fsstat,
3421 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 struct rpc_message msg = {
3423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3424 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003425 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 };
3427
Trond Myklebust0e574af2005-10-27 22:12:38 -04003428 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003429 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3433{
3434 struct nfs4_exception exception = { };
3435 int err;
3436 do {
3437 err = nfs4_handle_exception(server,
3438 _nfs4_proc_statfs(server, fhandle, fsstat),
3439 &exception);
3440 } while (exception.retry);
3441 return err;
3442}
3443
3444static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3445 struct nfs_fsinfo *fsinfo)
3446{
3447 struct nfs4_fsinfo_arg args = {
3448 .fh = fhandle,
3449 .bitmask = server->attr_bitmask,
3450 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003451 struct nfs4_fsinfo_res res = {
3452 .fsinfo = fsinfo,
3453 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3456 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003457 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 };
3459
Bryan Schumaker7c513052011-03-24 17:12:24 +00003460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3464{
3465 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003466 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 int err;
3468
3469 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003470 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3471 if (err == 0) {
3472 struct nfs_client *clp = server->nfs_client;
3473
3474 spin_lock(&clp->cl_lock);
3475 clp->cl_lease_time = fsinfo->lease_time * HZ;
3476 clp->cl_last_renewal = now;
3477 spin_unlock(&clp->cl_lock);
3478 break;
3479 }
3480 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 } while (exception.retry);
3482 return err;
3483}
3484
3485static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3486{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003487 int error;
3488
Trond Myklebust0e574af2005-10-27 22:12:38 -04003489 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003490 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003491 if (error == 0) {
3492 /* block layout checks this! */
3493 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003494 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003495 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003496
3497 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
3500static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3501 struct nfs_pathconf *pathconf)
3502{
3503 struct nfs4_pathconf_arg args = {
3504 .fh = fhandle,
3505 .bitmask = server->attr_bitmask,
3506 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003507 struct nfs4_pathconf_res res = {
3508 .pathconf = pathconf,
3509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 struct rpc_message msg = {
3511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3512 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003513 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 };
3515
3516 /* None of the pathconf attributes are mandatory to implement */
3517 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3518 memset(pathconf, 0, sizeof(*pathconf));
3519 return 0;
3520 }
3521
Trond Myklebust0e574af2005-10-27 22:12:38 -04003522 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003523 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
3526static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3527 struct nfs_pathconf *pathconf)
3528{
3529 struct nfs4_exception exception = { };
3530 int err;
3531
3532 do {
3533 err = nfs4_handle_exception(server,
3534 _nfs4_proc_pathconf(server, fhandle, pathconf),
3535 &exception);
3536 } while (exception.retry);
3537 return err;
3538}
3539
Trond Myklebust5521abf2013-03-16 20:54:34 -04003540int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003541 const struct nfs_open_context *ctx,
3542 const struct nfs_lock_context *l_ctx,
3543 fmode_t fmode)
3544{
3545 const struct nfs_lockowner *lockowner = NULL;
3546
3547 if (l_ctx != NULL)
3548 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003549 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003550}
3551EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3552
Trond Myklebust5521abf2013-03-16 20:54:34 -04003553static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3554 const struct nfs_open_context *ctx,
3555 const struct nfs_lock_context *l_ctx,
3556 fmode_t fmode)
3557{
3558 nfs4_stateid current_stateid;
3559
3560 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3561 return false;
3562 return nfs4_stateid_match(stateid, &current_stateid);
3563}
3564
3565static bool nfs4_error_stateid_expired(int err)
3566{
3567 switch (err) {
3568 case -NFS4ERR_DELEG_REVOKED:
3569 case -NFS4ERR_ADMIN_REVOKED:
3570 case -NFS4ERR_BAD_STATEID:
3571 case -NFS4ERR_STALE_STATEID:
3572 case -NFS4ERR_OLD_STATEID:
3573 case -NFS4ERR_OPENMODE:
3574 case -NFS4ERR_EXPIRED:
3575 return true;
3576 }
3577 return false;
3578}
3579
Benny Halevyd20581a2011-05-22 19:52:03 +03003580void __nfs4_read_done_cb(struct nfs_read_data *data)
3581{
Fred Isamancd841602012-04-20 14:47:44 -04003582 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003583}
3584
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003585static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Fred Isamancd841602012-04-20 14:47:44 -04003587 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003589 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003590 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003591 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003593
Benny Halevyd20581a2011-05-22 19:52:03 +03003594 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003596 renew_lease(server, data->timestamp);
3597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Trond Myklebust5521abf2013-03-16 20:54:34 -04003600static bool nfs4_read_stateid_changed(struct rpc_task *task,
3601 struct nfs_readargs *args)
3602{
3603
3604 if (!nfs4_error_stateid_expired(task->tk_status) ||
3605 nfs4_stateid_is_current(&args->stateid,
3606 args->context,
3607 args->lock_context,
3608 FMODE_READ))
3609 return false;
3610 rpc_restart_call_prepare(task);
3611 return true;
3612}
3613
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003614static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3615{
3616
3617 dprintk("--> %s\n", __func__);
3618
3619 if (!nfs4_sequence_done(task, &data->res.seq_res))
3620 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003621 if (nfs4_read_stateid_changed(task, &data->args))
3622 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003623 return data->read_done_cb ? data->read_done_cb(task, data) :
3624 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003625}
3626
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003627static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003630 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003631 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003632 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003635static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3636{
Trond Myklebust9b206142013-03-17 15:52:00 -04003637 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003638 &data->args.seq_args,
3639 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003640 task))
3641 return;
3642 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3643 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644}
3645
Fred Isamanb029bc92011-03-03 15:13:42 +00003646static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
Fred Isamancd841602012-04-20 14:47:44 -04003648 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003650 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003651 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003652 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003654 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003656 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003657 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Trond Myklebust5521abf2013-03-16 20:54:34 -04003661static bool nfs4_write_stateid_changed(struct rpc_task *task,
3662 struct nfs_writeargs *args)
3663{
3664
3665 if (!nfs4_error_stateid_expired(task->tk_status) ||
3666 nfs4_stateid_is_current(&args->stateid,
3667 args->context,
3668 args->lock_context,
3669 FMODE_WRITE))
3670 return false;
3671 rpc_restart_call_prepare(task);
3672 return true;
3673}
3674
Fred Isamanb029bc92011-03-03 15:13:42 +00003675static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3676{
3677 if (!nfs4_sequence_done(task, &data->res.seq_res))
3678 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003679 if (nfs4_write_stateid_changed(task, &data->args))
3680 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003681 return data->write_done_cb ? data->write_done_cb(task, data) :
3682 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003683}
3684
Trond Myklebust5a37f852012-04-28 14:55:16 -04003685static
3686bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003687{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003688 const struct nfs_pgio_header *hdr = data->header;
3689
3690 /* Don't request attributes for pNFS or O_DIRECT writes */
3691 if (data->ds_clp != NULL || hdr->dreq != NULL)
3692 return false;
3693 /* Otherwise, request attributes if and only if we don't hold
3694 * a delegation
3695 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003696 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003697}
Fred Isamana69aef12011-03-03 15:13:47 +00003698
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003699static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Fred Isamancd841602012-04-20 14:47:44 -04003701 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003702
Trond Myklebust5a37f852012-04-28 14:55:16 -04003703 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003704 data->args.bitmask = NULL;
3705 data->res.fattr = NULL;
3706 } else
3707 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003708
Fred Isamanb029bc92011-03-03 15:13:42 +00003709 if (!data->write_done_cb)
3710 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003711 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 data->timestamp = jiffies;
3713
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003714 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003715 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003718static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3719{
Trond Myklebust9b206142013-03-17 15:52:00 -04003720 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003721 &data->args.seq_args,
3722 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003723 task))
3724 return;
3725 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3726 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
Fred Isaman0b7c0152012-04-20 14:47:39 -04003729static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3730{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003731 nfs4_setup_sequence(NFS_SERVER(data->inode),
3732 &data->args.seq_args,
3733 &data->res.seq_res,
3734 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Fred Isaman0b7c0152012-04-20 14:47:39 -04003737static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003740
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003741 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003742 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003743 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
Fred Isaman0b7c0152012-04-20 14:47:39 -04003748static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003749{
3750 if (!nfs4_sequence_done(task, &data->res.seq_res))
3751 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003752 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003753}
3754
Fred Isaman0b7c0152012-04-20 14:47:39 -04003755static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003757 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003758
Fred Isaman0b7c0152012-04-20 14:47:39 -04003759 if (data->commit_done_cb == NULL)
3760 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003761 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003762 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003763 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003766struct nfs4_renewdata {
3767 struct nfs_client *client;
3768 unsigned long timestamp;
3769};
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771/*
3772 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3773 * standalone procedure for queueing an asynchronous RENEW.
3774 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003775static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003776{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003777 struct nfs4_renewdata *data = calldata;
3778 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003779
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003780 if (atomic_read(&clp->cl_count) > 1)
3781 nfs4_schedule_state_renewal(clp);
3782 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003783 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003784}
3785
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003786static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003788 struct nfs4_renewdata *data = calldata;
3789 struct nfs_client *clp = data->client;
3790 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003793 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003794 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3795 return;
3796 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003797 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003798 return;
3799 }
3800 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003802 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003805static const struct rpc_call_ops nfs4_renew_ops = {
3806 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003807 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003808};
3809
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003810static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3814 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003815 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003817 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003819 if (renew_flags == 0)
3820 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003821 if (!atomic_inc_not_zero(&clp->cl_count))
3822 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003823 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003824 if (data == NULL)
3825 return -ENOMEM;
3826 data->client = clp;
3827 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003829 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003832static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
3834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3836 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003837 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
3839 unsigned long now = jiffies;
3840 int status;
3841
3842 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3843 if (status < 0)
3844 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003845 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 return 0;
3847}
3848
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003849static inline int nfs4_server_supports_acls(struct nfs_server *server)
3850{
3851 return (server->caps & NFS_CAP_ACLS)
3852 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3853 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3854}
3855
Trond Myklebust21f498c2012-08-24 10:59:25 -04003856/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3857 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003858 * the stack.
3859 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003860#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003861
Neil Hormane9e3d722011-03-04 19:26:03 -05003862static int buf_to_pages_noslab(const void *buf, size_t buflen,
3863 struct page **pages, unsigned int *pgbase)
3864{
3865 struct page *newpage, **spages;
3866 int rc = 0;
3867 size_t len;
3868 spages = pages;
3869
3870 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003871 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003872 newpage = alloc_page(GFP_KERNEL);
3873
3874 if (newpage == NULL)
3875 goto unwind;
3876 memcpy(page_address(newpage), buf, len);
3877 buf += len;
3878 buflen -= len;
3879 *pages++ = newpage;
3880 rc++;
3881 } while (buflen != 0);
3882
3883 return rc;
3884
3885unwind:
3886 for(; rc > 0; rc--)
3887 __free_page(spages[rc-1]);
3888 return -ENOMEM;
3889}
3890
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003891struct nfs4_cached_acl {
3892 int cached;
3893 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003894 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003895};
3896
3897static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003898{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003899 struct nfs_inode *nfsi = NFS_I(inode);
3900
3901 spin_lock(&inode->i_lock);
3902 kfree(nfsi->nfs4_acl);
3903 nfsi->nfs4_acl = acl;
3904 spin_unlock(&inode->i_lock);
3905}
3906
3907static void nfs4_zap_acl_attr(struct inode *inode)
3908{
3909 nfs4_set_cached_acl(inode, NULL);
3910}
3911
3912static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3913{
3914 struct nfs_inode *nfsi = NFS_I(inode);
3915 struct nfs4_cached_acl *acl;
3916 int ret = -ENOENT;
3917
3918 spin_lock(&inode->i_lock);
3919 acl = nfsi->nfs4_acl;
3920 if (acl == NULL)
3921 goto out;
3922 if (buf == NULL) /* user is just asking for length */
3923 goto out_len;
3924 if (acl->cached == 0)
3925 goto out;
3926 ret = -ERANGE; /* see getxattr(2) man page */
3927 if (acl->len > buflen)
3928 goto out;
3929 memcpy(buf, acl->data, acl->len);
3930out_len:
3931 ret = acl->len;
3932out:
3933 spin_unlock(&inode->i_lock);
3934 return ret;
3935}
3936
Sachin Prabhu5794d212012-04-17 14:36:40 +01003937static 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 +00003938{
3939 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003940 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003941
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003942 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003943 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003944 if (acl == NULL)
3945 goto out;
3946 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003947 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003948 } else {
3949 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3950 if (acl == NULL)
3951 goto out;
3952 acl->cached = 0;
3953 }
3954 acl->len = acl_len;
3955out:
3956 nfs4_set_cached_acl(inode, acl);
3957}
3958
Andy Adamsonbf118a32011-12-07 11:55:27 -05003959/*
3960 * The getxattr API returns the required buffer length when called with a
3961 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3962 * the required buf. On a NULL buf, we send a page of data to the server
3963 * guessing that the ACL request can be serviced by a page. If so, we cache
3964 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3965 * the cache. If not so, we throw away the page, and cache the required
3966 * length. The next getxattr call will then produce another round trip to
3967 * the server, this time with the input buf of the required size.
3968 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003969static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003970{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003971 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003972 struct nfs_getaclargs args = {
3973 .fh = NFS_FH(inode),
3974 .acl_pages = pages,
3975 .acl_len = buflen,
3976 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003977 struct nfs_getaclres res = {
3978 .acl_len = buflen,
3979 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003980 struct rpc_message msg = {
3981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3982 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003983 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003984 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003985 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3986 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003987
Andy Adamsonbf118a32011-12-07 11:55:27 -05003988 /* As long as we're doing a round trip to the server anyway,
3989 * let's be prepared for a page of acl data. */
3990 if (npages == 0)
3991 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003992 if (npages > ARRAY_SIZE(pages))
3993 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003994
Andy Adamsonbf118a32011-12-07 11:55:27 -05003995 for (i = 0; i < npages; i++) {
3996 pages[i] = alloc_page(GFP_KERNEL);
3997 if (!pages[i])
3998 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003999 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004000
4001 /* for decoding across pages */
4002 res.acl_scratch = alloc_page(GFP_KERNEL);
4003 if (!res.acl_scratch)
4004 goto out_free;
4005
Andy Adamsonbf118a32011-12-07 11:55:27 -05004006 args.acl_len = npages * PAGE_SIZE;
4007 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004008
Peng Taode040be2012-01-10 22:42:47 +08004009 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004010 __func__, buf, buflen, npages, args.acl_len);
4011 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4012 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004013 if (ret)
4014 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004015
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004016 /* Handle the case where the passed-in buffer is too short */
4017 if (res.acl_flags & NFS4_ACL_TRUNC) {
4018 /* Did the user only issue a request for the acl length? */
4019 if (buf == NULL)
4020 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004021 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004022 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004023 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004024 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004025 if (buf) {
4026 if (res.acl_len > buflen) {
4027 ret = -ERANGE;
4028 goto out_free;
4029 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004030 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004031 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004032out_ok:
4033 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004034out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004035 for (i = 0; i < npages; i++)
4036 if (pages[i])
4037 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004038 if (res.acl_scratch)
4039 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004040 return ret;
4041}
4042
Trond Myklebust16b42892006-08-24 12:27:15 -04004043static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4044{
4045 struct nfs4_exception exception = { };
4046 ssize_t ret;
4047 do {
4048 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4049 if (ret >= 0)
4050 break;
4051 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4052 } while (exception.retry);
4053 return ret;
4054}
4055
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004056static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4057{
4058 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004059 int ret;
4060
4061 if (!nfs4_server_supports_acls(server))
4062 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004063 ret = nfs_revalidate_inode(server, inode);
4064 if (ret < 0)
4065 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004066 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4067 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004068 ret = nfs4_read_cached_acl(inode, buf, buflen);
4069 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004070 /* -ENOENT is returned if there is no ACL or if there is an ACL
4071 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004072 return ret;
4073 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004074}
4075
Trond Myklebust16b42892006-08-24 12:27:15 -04004076static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004077{
4078 struct nfs_server *server = NFS_SERVER(inode);
4079 struct page *pages[NFS4ACL_MAXPAGES];
4080 struct nfs_setaclargs arg = {
4081 .fh = NFS_FH(inode),
4082 .acl_pages = pages,
4083 .acl_len = buflen,
4084 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004085 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004086 struct rpc_message msg = {
4087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4088 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004089 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004090 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004091 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004092 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004093
4094 if (!nfs4_server_supports_acls(server))
4095 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004096 if (npages > ARRAY_SIZE(pages))
4097 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004098 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4099 if (i < 0)
4100 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004101 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004102 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004103
4104 /*
4105 * Free each page after tx, so the only ref left is
4106 * held by the network stack
4107 */
4108 for (; i > 0; i--)
4109 put_page(pages[i-1]);
4110
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004111 /*
4112 * Acl update can result in inode attribute update.
4113 * so mark the attribute cache invalid.
4114 */
4115 spin_lock(&inode->i_lock);
4116 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4117 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004118 nfs_access_zap_cache(inode);
4119 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004120 return ret;
4121}
4122
Trond Myklebust16b42892006-08-24 12:27:15 -04004123static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4124{
4125 struct nfs4_exception exception = { };
4126 int err;
4127 do {
4128 err = nfs4_handle_exception(NFS_SERVER(inode),
4129 __nfs4_proc_set_acl(inode, buf, buflen),
4130 &exception);
4131 } while (exception.retry);
4132 return err;
4133}
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004136nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004138 struct nfs_client *clp = server->nfs_client;
4139
4140 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return 0;
4142 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004143 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004144 case -NFS4ERR_ADMIN_REVOKED:
4145 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004146 if (state == NULL)
4147 break;
4148 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004149 case -NFS4ERR_OPENMODE:
4150 if (state == NULL)
4151 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004152 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4153 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004154 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004155 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004156 if (state != NULL) {
4157 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4158 goto stateid_invalid;
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004161 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004162 nfs4_schedule_lease_recovery(clp);
4163 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004164#if defined(CONFIG_NFS_V4_1)
4165 case -NFS4ERR_BADSESSION:
4166 case -NFS4ERR_BADSLOT:
4167 case -NFS4ERR_BAD_HIGH_SLOT:
4168 case -NFS4ERR_DEADSESSION:
4169 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4170 case -NFS4ERR_SEQ_FALSE_RETRY:
4171 case -NFS4ERR_SEQ_MISORDERED:
4172 dprintk("%s ERROR %d, Reset session\n", __func__,
4173 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004174 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004175 task->tk_status = 0;
4176 return -EAGAIN;
4177#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004179 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004180 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4182 task->tk_status = 0;
4183 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004184 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 case -NFS4ERR_OLD_STATEID:
4186 task->tk_status = 0;
4187 return -EAGAIN;
4188 }
4189 task->tk_status = nfs4_map_errors(task->tk_status);
4190 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004191stateid_invalid:
4192 task->tk_status = -EIO;
4193 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004194wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004195 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004196 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4197 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4198 task->tk_status = 0;
4199 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Chuck Leverf0920752012-05-21 22:45:41 -04004202static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4203 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004204{
4205 __be32 verf[2];
4206
Chuck Lever2c820d92012-05-21 22:45:33 -04004207 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4208 /* An impossible timestamp guarantees this value
4209 * will never match a generated boot time. */
4210 verf[0] = 0;
4211 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4212 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004213 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4214 verf[0] = (__be32)nn->boot_time.tv_sec;
4215 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004216 }
Chuck Levercd937102012-03-02 17:14:31 -05004217 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4218}
4219
Chuck Levere984a552012-09-14 17:24:21 -04004220static unsigned int
4221nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4222 char *buf, size_t len)
4223{
4224 unsigned int result;
4225
4226 rcu_read_lock();
4227 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4228 clp->cl_ipaddr,
4229 rpc_peeraddr2str(clp->cl_rpcclient,
4230 RPC_DISPLAY_ADDR),
4231 rpc_peeraddr2str(clp->cl_rpcclient,
4232 RPC_DISPLAY_PROTO));
4233 rcu_read_unlock();
4234 return result;
4235}
4236
4237static unsigned int
4238nfs4_init_uniform_client_string(const struct nfs_client *clp,
4239 char *buf, size_t len)
4240{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004241 char *nodename = clp->cl_rpcclient->cl_nodename;
4242
4243 if (nfs4_client_id_uniquifier[0] != '\0')
4244 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004245 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4246 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004247 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004248}
4249
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004250/**
4251 * nfs4_proc_setclientid - Negotiate client ID
4252 * @clp: state data structure
4253 * @program: RPC program for NFSv4 callback service
4254 * @port: IP port number for NFS4 callback service
4255 * @cred: RPC credential to use for this call
4256 * @res: where to place the result
4257 *
4258 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4259 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004260int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4261 unsigned short port, struct rpc_cred *cred,
4262 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 nfs4_verifier sc_verifier;
4265 struct nfs4_setclientid setclientid = {
4266 .sc_verifier = &sc_verifier,
4267 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004268 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4272 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004273 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004276 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Chuck Leverde734832012-07-11 16:30:50 -04004278 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004279 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004280 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4281 setclientid.sc_name_len =
4282 nfs4_init_uniform_client_string(clp,
4283 setclientid.sc_name,
4284 sizeof(setclientid.sc_name));
4285 else
4286 setclientid.sc_name_len =
4287 nfs4_init_nonuniform_client_string(clp,
4288 setclientid.sc_name,
4289 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004290 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004291 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004292 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004293 sizeof(setclientid.sc_netid),
4294 rpc_peeraddr2str(clp->cl_rpcclient,
4295 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004296 rcu_read_unlock();
4297 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004298 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 clp->cl_ipaddr, port >> 8, port & 255);
4300
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004301 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4302 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4303 setclientid.sc_name_len, setclientid.sc_name);
4304 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4305 dprintk("NFS reply setclientid: %d\n", status);
4306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004309/**
4310 * nfs4_proc_setclientid_confirm - Confirm client ID
4311 * @clp: state data structure
4312 * @res: result of a previous SETCLIENTID
4313 * @cred: RPC credential to use for this call
4314 *
4315 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4316 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004317int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004318 struct nfs4_setclientid_res *arg,
4319 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 struct rpc_message msg = {
4322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004323 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004324 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 int status;
4327
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004328 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4329 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4330 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004331 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004332 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return status;
4334}
4335
Trond Myklebustfe650402006-01-03 09:55:18 +01004336struct nfs4_delegreturndata {
4337 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004338 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004339 struct nfs_fh fh;
4340 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004341 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004342 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004343 int rpc_status;
4344};
4345
Trond Myklebustfe650402006-01-03 09:55:18 +01004346static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4347{
4348 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004349
Trond Myklebust14516c32010-07-31 14:29:06 -04004350 if (!nfs4_sequence_done(task, &data->res.seq_res))
4351 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004352
Ricardo Labiaga79708862009-12-07 09:23:21 -05004353 switch (task->tk_status) {
4354 case -NFS4ERR_STALE_STATEID:
4355 case -NFS4ERR_EXPIRED:
4356 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004357 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004358 break;
4359 default:
4360 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4361 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004362 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004363 return;
4364 }
4365 }
4366 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004367}
4368
4369static void nfs4_delegreturn_release(void *calldata)
4370{
Trond Myklebustfe650402006-01-03 09:55:18 +01004371 kfree(calldata);
4372}
4373
Andy Adamson938e1012009-04-01 09:22:28 -04004374#if defined(CONFIG_NFS_V4_1)
4375static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4376{
4377 struct nfs4_delegreturndata *d_data;
4378
4379 d_data = (struct nfs4_delegreturndata *)data;
4380
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004381 nfs4_setup_sequence(d_data->res.server,
4382 &d_data->args.seq_args,
4383 &d_data->res.seq_res,
4384 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004385}
4386#endif /* CONFIG_NFS_V4_1 */
4387
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004388static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004389#if defined(CONFIG_NFS_V4_1)
4390 .rpc_call_prepare = nfs4_delegreturn_prepare,
4391#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004392 .rpc_call_done = nfs4_delegreturn_done,
4393 .rpc_release = nfs4_delegreturn_release,
4394};
4395
Trond Myklebuste6f81072008-01-24 18:14:34 -05004396static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004397{
4398 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004399 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004400 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004401 struct rpc_message msg = {
4402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4403 .rpc_cred = cred,
4404 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004405 struct rpc_task_setup task_setup_data = {
4406 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004407 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004408 .callback_ops = &nfs4_delegreturn_ops,
4409 .flags = RPC_TASK_ASYNC,
4410 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004411 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004412
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004413 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004414 if (data == NULL)
4415 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004416 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004417 data->args.fhandle = &data->fh;
4418 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004419 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004420 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004421 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004422 data->res.fattr = &data->fattr;
4423 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004424 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004425 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004426 data->rpc_status = 0;
4427
Trond Myklebustc970aa82007-07-14 15:39:59 -04004428 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004429 msg.rpc_argp = &data->args;
4430 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004431 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004432 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004433 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004434 if (!issync)
4435 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004436 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004437 if (status != 0)
4438 goto out;
4439 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004440 if (status == 0)
4441 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4442 else
4443 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004444out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004445 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004446 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Trond Myklebuste6f81072008-01-24 18:14:34 -05004449int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 struct nfs_server *server = NFS_SERVER(inode);
4452 struct nfs4_exception exception = { };
4453 int err;
4454 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004455 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 switch (err) {
4457 case -NFS4ERR_STALE_STATEID:
4458 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 case 0:
4460 return 0;
4461 }
4462 err = nfs4_handle_exception(server, err, &exception);
4463 } while (exception.retry);
4464 return err;
4465}
4466
4467#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4468#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4469
4470/*
4471 * sleep, with exponential backoff, and retry the LOCK operation.
4472 */
4473static unsigned long
4474nfs4_set_lock_task_retry(unsigned long timeout)
4475{
Jeff Laytond3103102011-12-01 22:44:39 +01004476 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 timeout <<= 1;
4478 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4479 return NFS4_LOCK_MAXTIMEOUT;
4480 return timeout;
4481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4484{
4485 struct inode *inode = state->inode;
4486 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004487 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004488 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004492 struct nfs_lockt_res res = {
4493 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 };
4495 struct rpc_message msg = {
4496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4497 .rpc_argp = &arg,
4498 .rpc_resp = &res,
4499 .rpc_cred = state->owner->so_cred,
4500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 struct nfs4_lock_state *lsp;
4502 int status;
4503
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004504 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004505 status = nfs4_set_lock_state(state, request);
4506 if (status != 0)
4507 goto out;
4508 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004509 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004510 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004511 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004512 switch (status) {
4513 case 0:
4514 request->fl_type = F_UNLCK;
4515 break;
4516 case -NFS4ERR_DENIED:
4517 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004519 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004520out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return status;
4522}
4523
4524static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4525{
4526 struct nfs4_exception exception = { };
4527 int err;
4528
4529 do {
4530 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4531 _nfs4_proc_getlk(state, cmd, request),
4532 &exception);
4533 } while (exception.retry);
4534 return err;
4535}
4536
4537static int do_vfs_lock(struct file *file, struct file_lock *fl)
4538{
4539 int res = 0;
4540 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4541 case FL_POSIX:
4542 res = posix_lock_file_wait(file, fl);
4543 break;
4544 case FL_FLOCK:
4545 res = flock_lock_file_wait(file, fl);
4546 break;
4547 default:
4548 BUG();
4549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 return res;
4551}
4552
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004553struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004554 struct nfs_locku_args arg;
4555 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004556 struct nfs4_lock_state *lsp;
4557 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004558 struct file_lock fl;
4559 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004560 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004561};
4562
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004563static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4564 struct nfs_open_context *ctx,
4565 struct nfs4_lock_state *lsp,
4566 struct nfs_seqid *seqid)
4567{
4568 struct nfs4_unlockdata *p;
4569 struct inode *inode = lsp->ls_state->inode;
4570
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004571 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004572 if (p == NULL)
4573 return NULL;
4574 p->arg.fh = NFS_FH(inode);
4575 p->arg.fl = &p->fl;
4576 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004577 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004578 p->arg.stateid = &lsp->ls_stateid;
4579 p->lsp = lsp;
4580 atomic_inc(&lsp->ls_count);
4581 /* Ensure we don't close file until we're done freeing locks! */
4582 p->ctx = get_nfs_open_context(ctx);
4583 memcpy(&p->fl, fl, sizeof(p->fl));
4584 p->server = NFS_SERVER(inode);
4585 return p;
4586}
4587
Trond Myklebust06f814a2006-01-03 09:55:07 +01004588static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004589{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004590 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004591 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004592 nfs4_put_lock_state(calldata->lsp);
4593 put_nfs_open_context(calldata->ctx);
4594 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004595}
4596
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004597static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004598{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004599 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600
Trond Myklebust14516c32010-07-31 14:29:06 -04004601 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4602 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004603 switch (task->tk_status) {
4604 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004605 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4606 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004607 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004608 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004609 case -NFS4ERR_BAD_STATEID:
4610 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611 case -NFS4ERR_STALE_STATEID:
4612 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004613 break;
4614 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004615 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004616 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004617 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004618 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004619}
4620
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004621static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004622{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004623 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004625 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004626 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004627 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004628 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004629 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004630 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004631 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004632 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004633 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004634 &calldata->res.seq_res,
4635 task) != 0)
4636 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004637 return;
4638out_no_action:
4639 task->tk_action = NULL;
4640out_wait:
4641 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004644static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004645 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004646 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004647 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004648};
4649
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004650static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4651 struct nfs_open_context *ctx,
4652 struct nfs4_lock_state *lsp,
4653 struct nfs_seqid *seqid)
4654{
4655 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004656 struct rpc_message msg = {
4657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4658 .rpc_cred = ctx->cred,
4659 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004660 struct rpc_task_setup task_setup_data = {
4661 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004662 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004663 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004664 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004665 .flags = RPC_TASK_ASYNC,
4666 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004667
Frank Filz137d6ac2007-07-09 15:32:29 -07004668 /* Ensure this is an unlock - when canceling a lock, the
4669 * canceled lock is passed in, and it won't be an unlock.
4670 */
4671 fl->fl_type = F_UNLCK;
4672
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004673 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4674 if (data == NULL) {
4675 nfs_free_seqid(seqid);
4676 return ERR_PTR(-ENOMEM);
4677 }
4678
Trond Myklebust9d12b212012-01-17 22:04:25 -05004679 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004680 msg.rpc_argp = &data->arg;
4681 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004682 task_setup_data.callback_data = data;
4683 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684}
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4687{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004688 struct inode *inode = state->inode;
4689 struct nfs4_state_owner *sp = state->owner;
4690 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004691 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004692 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004693 struct rpc_task *task;
4694 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004695 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Trond Myklebust9b073572006-06-29 16:38:34 -04004697 status = nfs4_set_lock_state(state, request);
4698 /* Unlock _before_ we do the RPC call */
4699 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004700 /* Exclude nfs_delegation_claim_locks() */
4701 mutex_lock(&sp->so_delegreturn_mutex);
4702 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004703 down_read(&nfsi->rwsem);
4704 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4705 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004706 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004707 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004708 }
4709 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004710 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004711 if (status != 0)
4712 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004713 /* Is this a delegated lock? */
4714 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004715 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004716 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004717 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004718 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004719 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004720 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004721 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722 status = PTR_ERR(task);
4723 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004724 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004725 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004726 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004727out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004728 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
4731
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004732struct nfs4_lockdata {
4733 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004734 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 struct nfs4_lock_state *lsp;
4736 struct nfs_open_context *ctx;
4737 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004738 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004739 int rpc_status;
4740 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004741 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004742};
4743
4744static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004745 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4746 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747{
4748 struct nfs4_lockdata *p;
4749 struct inode *inode = lsp->ls_state->inode;
4750 struct nfs_server *server = NFS_SERVER(inode);
4751
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004752 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 if (p == NULL)
4754 return NULL;
4755
4756 p->arg.fh = NFS_FH(inode);
4757 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004758 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004759 if (p->arg.open_seqid == NULL)
4760 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004761 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004762 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004763 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004765 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004766 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004767 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004768 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004769 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004770 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004771 atomic_inc(&lsp->ls_count);
4772 p->ctx = get_nfs_open_context(ctx);
4773 memcpy(&p->fl, fl, sizeof(p->fl));
4774 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004775out_free_seqid:
4776 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004777out_free:
4778 kfree(p);
4779 return NULL;
4780}
4781
4782static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4783{
4784 struct nfs4_lockdata *data = calldata;
4785 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Harvey Harrison3110ff82008-05-02 13:42:44 -07004787 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004788 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004789 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790 /* Do we need to do an open_to_lock_owner? */
4791 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004792 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004793 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004794 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004795 data->arg.open_stateid = &state->stateid;
4796 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004797 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004798 } else
4799 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004800 if (!nfs4_valid_open_stateid(state)) {
4801 data->rpc_status = -EBADF;
4802 task->tk_action = NULL;
4803 goto out_release_open_seqid;
4804 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004805 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004806 if (nfs4_setup_sequence(data->server,
4807 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004808 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004809 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004810 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004811out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004812 nfs_release_seqid(data->arg.open_seqid);
4813out_release_lock_seqid:
4814 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004815out_wait:
4816 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004817 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004818}
4819
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004820static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4821{
4822 struct nfs4_lockdata *data = calldata;
4823
Harvey Harrison3110ff82008-05-02 13:42:44 -07004824 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825
Trond Myklebust14516c32010-07-31 14:29:06 -04004826 if (!nfs4_sequence_done(task, &data->res.seq_res))
4827 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004828
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004829 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004830 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831 if (data->rpc_status == 0)
4832 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4833 else
4834 goto out;
4835 }
4836 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004837 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004838 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004839 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004842 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004843}
4844
4845static void nfs4_lock_release(void *calldata)
4846{
4847 struct nfs4_lockdata *data = calldata;
4848
Harvey Harrison3110ff82008-05-02 13:42:44 -07004849 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004850 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851 if (data->cancelled != 0) {
4852 struct rpc_task *task;
4853 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4854 data->arg.lock_seqid);
4855 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004856 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004857 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004858 } else
4859 nfs_free_seqid(data->arg.lock_seqid);
4860 nfs4_put_lock_state(data->lsp);
4861 put_nfs_open_context(data->ctx);
4862 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004863 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004864}
4865
4866static const struct rpc_call_ops nfs4_lock_ops = {
4867 .rpc_call_prepare = nfs4_lock_prepare,
4868 .rpc_call_done = nfs4_lock_done,
4869 .rpc_release = nfs4_lock_release,
4870};
4871
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004872static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4873{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004874 switch (error) {
4875 case -NFS4ERR_ADMIN_REVOKED:
4876 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004877 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004878 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004879 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004880 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004881 break;
4882 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004883 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004884 case -NFS4ERR_EXPIRED:
4885 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004886 };
4887}
4888
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004889static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004890{
4891 struct nfs4_lockdata *data;
4892 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004893 struct rpc_message msg = {
4894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4895 .rpc_cred = state->owner->so_cred,
4896 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004897 struct rpc_task_setup task_setup_data = {
4898 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004899 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004900 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004901 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004902 .flags = RPC_TASK_ASYNC,
4903 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004904 int ret;
4905
Harvey Harrison3110ff82008-05-02 13:42:44 -07004906 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004907 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004908 fl->fl_u.nfs4_fl.owner,
4909 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004910 if (data == NULL)
4911 return -ENOMEM;
4912 if (IS_SETLKW(cmd))
4913 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004914 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004915 msg.rpc_argp = &data->arg;
4916 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004917 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004918 if (recovery_type > NFS_LOCK_NEW) {
4919 if (recovery_type == NFS_LOCK_RECLAIM)
4920 data->arg.reclaim = NFS_LOCK_RECLAIM;
4921 nfs4_set_sequence_privileged(&data->arg.seq_args);
4922 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004923 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004924 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004925 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926 ret = nfs4_wait_for_completion_rpc_task(task);
4927 if (ret == 0) {
4928 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004929 if (ret)
4930 nfs4_handle_setlk_error(data->server, data->lsp,
4931 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004932 } else
4933 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004934 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004935 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
4939static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4940{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004941 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004942 struct nfs4_exception exception = {
4943 .inode = state->inode,
4944 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004945 int err;
4946
4947 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004948 /* Cache the lock if possible... */
4949 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4950 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004951 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004952 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004953 break;
4954 nfs4_handle_exception(server, err, &exception);
4955 } while (exception.retry);
4956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
4958
4959static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4960{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004961 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004962 struct nfs4_exception exception = {
4963 .inode = state->inode,
4964 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004965 int err;
4966
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004967 err = nfs4_set_lock_state(state, request);
4968 if (err != 0)
4969 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004970 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004971 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4972 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004973 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004974 switch (err) {
4975 default:
4976 goto out;
4977 case -NFS4ERR_GRACE:
4978 case -NFS4ERR_DELAY:
4979 nfs4_handle_exception(server, err, &exception);
4980 err = 0;
4981 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004982 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004983out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004987#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004988/**
4989 * nfs41_check_expired_locks - possibly free a lock stateid
4990 *
4991 * @state: NFSv4 state for an inode
4992 *
4993 * Returns NFS_OK if recovery for this stateid is now finished.
4994 * Otherwise a negative NFS4ERR value is returned.
4995 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004996static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004997{
Chuck Levereb64cf92012-07-11 16:30:05 -04004998 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004999 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005000 struct nfs_server *server = NFS_SERVER(state->inode);
5001
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005002 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005003 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005004 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5005 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005006 /* Free the stateid unless the server
5007 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005008 if (status != -NFS4ERR_BAD_STATEID)
5009 nfs41_free_stateid(server,
5010 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005011 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005012 ret = status;
5013 }
5014 }
5015 };
5016
5017 return ret;
5018}
5019
5020static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5021{
5022 int status = NFS_OK;
5023
5024 if (test_bit(LK_STATE_IN_USE, &state->flags))
5025 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005026 if (status != NFS_OK)
5027 status = nfs4_lock_expired(state, request);
5028 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005029}
5030#endif
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5033{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005034 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005035 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005036 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005037 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005038 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005040 if ((fl_flags & FL_POSIX) &&
5041 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5042 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005043 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005044 status = nfs4_set_lock_state(state, request);
5045 if (status != 0)
5046 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005047 request->fl_flags |= FL_ACCESS;
5048 status = do_vfs_lock(request->fl_file, request);
5049 if (status < 0)
5050 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005051 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005052 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005053 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005054 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005055 request->fl_flags = fl_flags & ~FL_SLEEP;
5056 status = do_vfs_lock(request->fl_file, request);
5057 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005059 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5060 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005061 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005062 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005063 goto out;
5064 down_read(&nfsi->rwsem);
5065 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5066 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005067 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005068 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005069 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005070 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005071 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005072 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5073 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005074out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005075 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005076out:
5077 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return status;
5079}
5080
5081static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5082{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005083 struct nfs4_exception exception = {
5084 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005085 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 int err;
5088
5089 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005090 err = _nfs4_proc_setlk(state, cmd, request);
5091 if (err == -NFS4ERR_DENIED)
5092 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005094 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 } while (exception.retry);
5096 return err;
5097}
5098
5099static int
5100nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5101{
5102 struct nfs_open_context *ctx;
5103 struct nfs4_state *state;
5104 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5105 int status;
5106
5107 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005108 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 state = ctx->state;
5110
5111 if (request->fl_start < 0 || request->fl_end < 0)
5112 return -EINVAL;
5113
Trond Myklebustd9531262009-07-21 19:22:38 -04005114 if (IS_GETLK(cmd)) {
5115 if (state != NULL)
5116 return nfs4_proc_getlk(state, F_GETLK, request);
5117 return 0;
5118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
5120 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5121 return -EINVAL;
5122
Trond Myklebustd9531262009-07-21 19:22:38 -04005123 if (request->fl_type == F_UNLCK) {
5124 if (state != NULL)
5125 return nfs4_proc_unlck(state, cmd, request);
5126 return 0;
5127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Trond Myklebustd9531262009-07-21 19:22:38 -04005129 if (state == NULL)
5130 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005131 /*
5132 * Don't rely on the VFS having checked the file open mode,
5133 * since it won't do this for flock() locks.
5134 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005135 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005136 case F_RDLCK:
5137 if (!(filp->f_mode & FMODE_READ))
5138 return -EBADF;
5139 break;
5140 case F_WRLCK:
5141 if (!(filp->f_mode & FMODE_WRITE))
5142 return -EBADF;
5143 }
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 do {
5146 status = nfs4_proc_setlk(state, cmd, request);
5147 if ((status != -EAGAIN) || IS_SETLK(cmd))
5148 break;
5149 timeout = nfs4_set_lock_task_retry(timeout);
5150 status = -ERESTARTSYS;
5151 if (signalled())
5152 break;
5153 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return status;
5155}
5156
Trond Myklebust888e6942005-11-04 15:38:11 -05005157int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5158{
5159 struct nfs_server *server = NFS_SERVER(state->inode);
5160 struct nfs4_exception exception = { };
5161 int err;
5162
5163 err = nfs4_set_lock_state(state, fl);
5164 if (err != 0)
5165 goto out;
5166 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005167 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005168 switch (err) {
5169 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005170 printk(KERN_ERR "NFS: %s: unhandled error "
5171 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005172 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005173 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005174 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005175 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005176 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005177 set_bit(NFS_DELEGATED_STATE, &state->flags);
5178 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005179 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005180 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005181 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005182 case -NFS4ERR_BADSESSION:
5183 case -NFS4ERR_BADSLOT:
5184 case -NFS4ERR_BAD_HIGH_SLOT:
5185 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5186 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005187 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005188 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005189 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005190 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005191 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005192 case -NFS4ERR_ADMIN_REVOKED:
5193 case -NFS4ERR_BAD_STATEID:
5194 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005195 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005196 err = 0;
5197 goto out;
5198 case -ENOMEM:
5199 case -NFS4ERR_DENIED:
5200 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5201 err = 0;
5202 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005203 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005204 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005205 err = nfs4_handle_exception(server, err, &exception);
5206 } while (exception.retry);
5207out:
5208 return err;
5209}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005210
Trond Myklebustcf470c32012-03-07 13:49:12 -05005211struct nfs_release_lockowner_data {
5212 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005213 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005214 struct nfs_release_lockowner_args args;
5215};
5216
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005217static void nfs4_release_lockowner_release(void *calldata)
5218{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005219 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005220 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005221 kfree(calldata);
5222}
5223
Trond Myklebust17280172012-03-11 13:11:00 -04005224static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005225 .rpc_release = nfs4_release_lockowner_release,
5226};
5227
Trond Myklebustcf470c32012-03-07 13:49:12 -05005228int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005229{
5230 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005231 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005232 struct rpc_message msg = {
5233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5234 };
5235
5236 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005237 return -EINVAL;
5238 data = kmalloc(sizeof(*data), GFP_NOFS);
5239 if (!data)
5240 return -ENOMEM;
5241 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005242 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005243 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5244 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5245 data->args.lock_owner.s_dev = server->s_dev;
5246 msg.rpc_argp = &data->args;
5247 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5248 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005249}
5250
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005251#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5252
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005253static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5254 const void *buf, size_t buflen,
5255 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005256{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005257 if (strcmp(key, "") != 0)
5258 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005259
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005260 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005261}
5262
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005263static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5264 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005265{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005266 if (strcmp(key, "") != 0)
5267 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005268
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005269 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005270}
5271
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005272static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5273 size_t list_len, const char *name,
5274 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005275{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005276 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005277
J. Bruce Fields096455a2006-03-20 23:23:42 -05005278 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5279 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005280
5281 if (list && len <= list_len)
5282 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005283 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005284}
5285
Andy Adamson533eb462011-06-13 18:25:56 -04005286/*
5287 * nfs_fhget will use either the mounted_on_fileid or the fileid
5288 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005289static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5290{
Andy Adamson533eb462011-06-13 18:25:56 -04005291 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5292 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5293 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005294 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005295 return;
5296
5297 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005298 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005299 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5300 fattr->nlink = 2;
5301}
5302
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005303static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5304 const struct qstr *name,
5305 struct nfs4_fs_locations *fs_locations,
5306 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005307{
5308 struct nfs_server *server = NFS_SERVER(dir);
5309 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005310 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005311 };
5312 struct nfs4_fs_locations_arg args = {
5313 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005314 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005315 .page = page,
5316 .bitmask = bitmask,
5317 };
Benny Halevy22958462009-04-01 09:22:02 -04005318 struct nfs4_fs_locations_res res = {
5319 .fs_locations = fs_locations,
5320 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005321 struct rpc_message msg = {
5322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5323 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005324 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005325 };
5326 int status;
5327
Harvey Harrison3110ff82008-05-02 13:42:44 -07005328 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005329
5330 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5331 * is not supported */
5332 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5333 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5334 else
5335 bitmask[0] |= FATTR4_WORD0_FILEID;
5336
Trond Myklebustc228fd32007-01-13 02:28:11 -05005337 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005338 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005339 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005340 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005341 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005342 return status;
5343}
5344
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005345int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5346 const struct qstr *name,
5347 struct nfs4_fs_locations *fs_locations,
5348 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005349{
5350 struct nfs4_exception exception = { };
5351 int err;
5352 do {
5353 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005354 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005355 &exception);
5356 } while (exception.retry);
5357 return err;
5358}
5359
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005360static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5361{
5362 int status;
5363 struct nfs4_secinfo_arg args = {
5364 .dir_fh = NFS_FH(dir),
5365 .name = name,
5366 };
5367 struct nfs4_secinfo_res res = {
5368 .flavors = flavors,
5369 };
5370 struct rpc_message msg = {
5371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5372 .rpc_argp = &args,
5373 .rpc_resp = &res,
5374 };
5375
5376 dprintk("NFS call secinfo %s\n", name->name);
5377 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5378 dprintk("NFS reply secinfo: %d\n", status);
5379 return status;
5380}
5381
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005382int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5383 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005384{
5385 struct nfs4_exception exception = { };
5386 int err;
5387 do {
5388 err = nfs4_handle_exception(NFS_SERVER(dir),
5389 _nfs4_proc_secinfo(dir, name, flavors),
5390 &exception);
5391 } while (exception.retry);
5392 return err;
5393}
5394
Andy Adamson557134a2009-04-01 09:21:53 -04005395#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005396/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005397 * Check the exchange flags returned by the server for invalid flags, having
5398 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5399 * DS flags set.
5400 */
5401static int nfs4_check_cl_exchange_flags(u32 flags)
5402{
5403 if (flags & ~EXCHGID4_FLAG_MASK_R)
5404 goto out_inval;
5405 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5406 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5407 goto out_inval;
5408 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5409 goto out_inval;
5410 return NFS_OK;
5411out_inval:
5412 return -NFS4ERR_INVAL;
5413}
5414
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005415static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005416nfs41_same_server_scope(struct nfs41_server_scope *a,
5417 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005418{
5419 if (a->server_scope_sz == b->server_scope_sz &&
5420 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5421 return true;
5422
5423 return false;
5424}
5425
Andy Adamson357f54d2010-12-14 10:11:57 -05005426/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005427 * nfs4_proc_bind_conn_to_session()
5428 *
5429 * The 4.1 client currently uses the same TCP connection for the
5430 * fore and backchannel.
5431 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005432int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005433{
5434 int status;
5435 struct nfs41_bind_conn_to_session_res res;
5436 struct rpc_message msg = {
5437 .rpc_proc =
5438 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5439 .rpc_argp = clp,
5440 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005441 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005442 };
5443
5444 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005445
5446 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5447 if (unlikely(res.session == NULL)) {
5448 status = -ENOMEM;
5449 goto out;
5450 }
5451
5452 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5453 if (status == 0) {
5454 if (memcmp(res.session->sess_id.data,
5455 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5456 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5457 status = -EIO;
5458 goto out_session;
5459 }
5460 if (res.dir != NFS4_CDFS4_BOTH) {
5461 dprintk("NFS: %s: Unexpected direction from server\n",
5462 __func__);
5463 status = -EIO;
5464 goto out_session;
5465 }
5466 if (res.use_conn_in_rdma_mode) {
5467 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5468 __func__);
5469 status = -EIO;
5470 goto out_session;
5471 }
5472 }
5473out_session:
5474 kfree(res.session);
5475out:
5476 dprintk("<-- %s status= %d\n", __func__, status);
5477 return status;
5478}
5479
5480/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005481 * nfs4_proc_exchange_id()
5482 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005483 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5484 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005485 * Since the clientid has expired, all compounds using sessions
5486 * associated with the stale clientid will be returning
5487 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5488 * be in some phase of session reset.
5489 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005490int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491{
5492 nfs4_verifier verifier;
5493 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005494 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005495 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005496 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005497 };
5498 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005499 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005500 };
5501 int status;
5502 struct rpc_message msg = {
5503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5504 .rpc_argp = &args,
5505 .rpc_resp = &res,
5506 .rpc_cred = cred,
5507 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005508
Chuck Leverf0920752012-05-21 22:45:41 -04005509 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005510 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5511 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005512 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5513 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5514 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005515
Chuck Leveracdeb692012-05-21 22:46:16 -04005516 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005517 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005518 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005519 status = -ENOMEM;
5520 goto out;
5521 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005522
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005523 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005524 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005525 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005526 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005527 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005528 }
5529
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005530 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005531 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005532 status = -ENOMEM;
5533 goto out_server_scope;
5534 }
5535
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005536 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005537 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005538 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005539
Chuck Lever177313f2012-05-21 22:44:58 -04005540 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005541 clp->cl_clientid = res.clientid;
5542 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5543 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5544 clp->cl_seqid = res.seqid;
5545
Chuck Leveracdeb692012-05-21 22:46:16 -04005546 kfree(clp->cl_serverowner);
5547 clp->cl_serverowner = res.server_owner;
5548 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005549
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005550 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005551 kfree(clp->cl_implid);
5552 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005553
Chuck Lever177313f2012-05-21 22:44:58 -04005554 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005555 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005556 res.server_scope)) {
5557 dprintk("%s: server_scope mismatch detected\n",
5558 __func__);
5559 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005560 kfree(clp->cl_serverscope);
5561 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005562 }
5563
Chuck Lever177313f2012-05-21 22:44:58 -04005564 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005565 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005566 goto out;
5567 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005568 } else
5569 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005570
Chuck Leveracdeb692012-05-21 22:46:16 -04005571out_server_owner:
5572 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005573out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005574 kfree(res.server_scope);
5575out:
Chuck Lever177313f2012-05-21 22:44:58 -04005576 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005577 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005578 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005579 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005580 clp->cl_implid->date.seconds,
5581 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005582 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005583 return status;
5584}
5585
Trond Myklebust66245532012-05-25 17:18:09 -04005586static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5587 struct rpc_cred *cred)
5588{
5589 struct rpc_message msg = {
5590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5591 .rpc_argp = clp,
5592 .rpc_cred = cred,
5593 };
5594 int status;
5595
5596 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5597 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005598 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005599 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5600 return status;
5601}
5602
5603static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5604 struct rpc_cred *cred)
5605{
5606 unsigned int loop;
5607 int ret;
5608
5609 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5610 ret = _nfs4_proc_destroy_clientid(clp, cred);
5611 switch (ret) {
5612 case -NFS4ERR_DELAY:
5613 case -NFS4ERR_CLIENTID_BUSY:
5614 ssleep(1);
5615 break;
5616 default:
5617 return ret;
5618 }
5619 }
5620 return 0;
5621}
5622
5623int nfs4_destroy_clientid(struct nfs_client *clp)
5624{
5625 struct rpc_cred *cred;
5626 int ret = 0;
5627
5628 if (clp->cl_mvops->minor_version < 1)
5629 goto out;
5630 if (clp->cl_exchange_flags == 0)
5631 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005632 if (clp->cl_preserve_clid)
5633 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005634 cred = nfs4_get_exchange_id_cred(clp);
5635 ret = nfs4_proc_destroy_clientid(clp, cred);
5636 if (cred)
5637 put_rpccred(cred);
5638 switch (ret) {
5639 case 0:
5640 case -NFS4ERR_STALE_CLIENTID:
5641 clp->cl_exchange_flags = 0;
5642 }
5643out:
5644 return ret;
5645}
5646
Andy Adamson2050f0c2009-04-01 09:22:30 -04005647struct nfs4_get_lease_time_data {
5648 struct nfs4_get_lease_time_args *args;
5649 struct nfs4_get_lease_time_res *res;
5650 struct nfs_client *clp;
5651};
5652
5653static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5654 void *calldata)
5655{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005656 struct nfs4_get_lease_time_data *data =
5657 (struct nfs4_get_lease_time_data *)calldata;
5658
5659 dprintk("--> %s\n", __func__);
5660 /* just setup sequence, do not trigger session recovery
5661 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005662 nfs41_setup_sequence(data->clp->cl_session,
5663 &data->args->la_seq_args,
5664 &data->res->lr_seq_res,
5665 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005666 dprintk("<-- %s\n", __func__);
5667}
5668
5669/*
5670 * Called from nfs4_state_manager thread for session setup, so don't recover
5671 * from sequence operation or clientid errors.
5672 */
5673static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5674{
5675 struct nfs4_get_lease_time_data *data =
5676 (struct nfs4_get_lease_time_data *)calldata;
5677
5678 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005679 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5680 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005681 switch (task->tk_status) {
5682 case -NFS4ERR_DELAY:
5683 case -NFS4ERR_GRACE:
5684 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5685 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5686 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005687 /* fall through */
5688 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005689 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005690 return;
5691 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005692 dprintk("<-- %s\n", __func__);
5693}
5694
Trond Myklebust17280172012-03-11 13:11:00 -04005695static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005696 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5697 .rpc_call_done = nfs4_get_lease_time_done,
5698};
5699
5700int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5701{
5702 struct rpc_task *task;
5703 struct nfs4_get_lease_time_args args;
5704 struct nfs4_get_lease_time_res res = {
5705 .lr_fsinfo = fsinfo,
5706 };
5707 struct nfs4_get_lease_time_data data = {
5708 .args = &args,
5709 .res = &res,
5710 .clp = clp,
5711 };
5712 struct rpc_message msg = {
5713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5714 .rpc_argp = &args,
5715 .rpc_resp = &res,
5716 };
5717 struct rpc_task_setup task_setup = {
5718 .rpc_client = clp->cl_rpcclient,
5719 .rpc_message = &msg,
5720 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005721 .callback_data = &data,
5722 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005723 };
5724 int status;
5725
Trond Myklebust9d12b212012-01-17 22:04:25 -05005726 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005727 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005728 dprintk("--> %s\n", __func__);
5729 task = rpc_run_task(&task_setup);
5730
5731 if (IS_ERR(task))
5732 status = PTR_ERR(task);
5733 else {
5734 status = task->tk_status;
5735 rpc_put_task(task);
5736 }
5737 dprintk("<-- %s return %d\n", __func__, status);
5738
5739 return status;
5740}
5741
Andy Adamsonfc931582009-04-01 09:22:31 -04005742/*
5743 * Initialize the values to be used by the client in CREATE_SESSION
5744 * If nfs4_init_session set the fore channel request and response sizes,
5745 * use them.
5746 *
5747 * Set the back channel max_resp_sz_cached to zero to force the client to
5748 * always set csa_cachethis to FALSE because the current implementation
5749 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5750 */
5751static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5752{
5753 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005754 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5755 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005756
5757 if (mxrqst_sz == 0)
5758 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5759 if (mxresp_sz == 0)
5760 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5761 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005762 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5763 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005764 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005765 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005766
5767 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005768 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005769 __func__,
5770 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005771 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005772
5773 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005774 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5775 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5776 args->bc_attrs.max_resp_sz_cached = 0;
5777 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5778 args->bc_attrs.max_reqs = 1;
5779
5780 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5781 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5782 __func__,
5783 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5784 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5785 args->bc_attrs.max_reqs);
5786}
5787
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005788static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005789{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005790 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5791 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5792
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005793 if (rcvd->max_resp_sz > sent->max_resp_sz)
5794 return -EINVAL;
5795 /*
5796 * Our requested max_ops is the minimum we need; we're not
5797 * prepared to break up compounds into smaller pieces than that.
5798 * So, no point even trying to continue if the server won't
5799 * cooperate:
5800 */
5801 if (rcvd->max_ops < sent->max_ops)
5802 return -EINVAL;
5803 if (rcvd->max_reqs == 0)
5804 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005805 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5806 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005807 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005808}
5809
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005810static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5811{
5812 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5813 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005814
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005815 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5816 return -EINVAL;
5817 if (rcvd->max_resp_sz < sent->max_resp_sz)
5818 return -EINVAL;
5819 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5820 return -EINVAL;
5821 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005822 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005823 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005824 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005825 return -EINVAL;
5826 return 0;
5827}
Andy Adamson8d353012009-04-01 09:22:32 -04005828
Andy Adamson8d353012009-04-01 09:22:32 -04005829static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5830 struct nfs4_session *session)
5831{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005832 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005833
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005834 ret = nfs4_verify_fore_channel_attrs(args, session);
5835 if (ret)
5836 return ret;
5837 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005838}
5839
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005840static int _nfs4_proc_create_session(struct nfs_client *clp,
5841 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005842{
5843 struct nfs4_session *session = clp->cl_session;
5844 struct nfs41_create_session_args args = {
5845 .client = clp,
5846 .cb_program = NFS4_CALLBACK,
5847 };
5848 struct nfs41_create_session_res res = {
5849 .client = clp,
5850 };
5851 struct rpc_message msg = {
5852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5853 .rpc_argp = &args,
5854 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005855 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005856 };
5857 int status;
5858
5859 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005860 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005861
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005862 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005863
Trond Myklebust43095d32012-11-20 11:13:12 -05005864 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005865 /* Verify the session's negotiated channel_attrs values */
5866 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005867 /* Increment the clientid slot sequence id */
5868 clp->cl_seqid++;
5869 }
5870
5871 return status;
5872}
5873
5874/*
5875 * Issues a CREATE_SESSION operation to the server.
5876 * It is the responsibility of the caller to verify the session is
5877 * expired before calling this routine.
5878 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005879int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005880{
5881 int status;
5882 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005883 struct nfs4_session *session = clp->cl_session;
5884
5885 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5886
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005887 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005888 if (status)
5889 goto out;
5890
Andy Adamsonaacd5532011-11-09 13:58:21 -05005891 /* Init or reset the session slot tables */
5892 status = nfs4_setup_session_slot_tables(session);
5893 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005894 if (status)
5895 goto out;
5896
5897 ptr = (unsigned *)&session->sess_id.data[0];
5898 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5899 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005900out:
5901 dprintk("<-- %s\n", __func__);
5902 return status;
5903}
5904
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005905/*
5906 * Issue the over-the-wire RPC DESTROY_SESSION.
5907 * The caller must serialize access to this routine.
5908 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005909int nfs4_proc_destroy_session(struct nfs4_session *session,
5910 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005911{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005912 struct rpc_message msg = {
5913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5914 .rpc_argp = session,
5915 .rpc_cred = cred,
5916 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005917 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005918
5919 dprintk("--> nfs4_proc_destroy_session\n");
5920
5921 /* session is still being setup */
5922 if (session->clp->cl_cons_state != NFS_CS_READY)
5923 return status;
5924
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005925 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005926
5927 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005928 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005929 "Session has been destroyed regardless...\n", status);
5930
5931 dprintk("<-- nfs4_proc_destroy_session\n");
5932 return status;
5933}
5934
Trond Myklebust7b38c362012-05-23 13:23:31 -04005935/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005936 * Renew the cl_session lease.
5937 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005938struct nfs4_sequence_data {
5939 struct nfs_client *clp;
5940 struct nfs4_sequence_args args;
5941 struct nfs4_sequence_res res;
5942};
5943
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005944static void nfs41_sequence_release(void *data)
5945{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005946 struct nfs4_sequence_data *calldata = data;
5947 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005948
Alexandros Batsakis71358402010-02-05 03:45:05 -08005949 if (atomic_read(&clp->cl_count) > 1)
5950 nfs4_schedule_state_renewal(clp);
5951 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005952 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005953}
5954
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005955static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5956{
5957 switch(task->tk_status) {
5958 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005959 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5960 return -EAGAIN;
5961 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005962 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005963 }
5964 return 0;
5965}
5966
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005967static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005969 struct nfs4_sequence_data *calldata = data;
5970 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005971
Trond Myklebust14516c32010-07-31 14:29:06 -04005972 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5973 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974
5975 if (task->tk_status < 0) {
5976 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005977 if (atomic_read(&clp->cl_count) == 1)
5978 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005980 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5981 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982 return;
5983 }
5984 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005985 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005986out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005987 dprintk("<-- %s\n", __func__);
5988}
5989
5990static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5991{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005992 struct nfs4_sequence_data *calldata = data;
5993 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005994 struct nfs4_sequence_args *args;
5995 struct nfs4_sequence_res *res;
5996
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005997 args = task->tk_msg.rpc_argp;
5998 res = task->tk_msg.rpc_resp;
5999
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006000 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006001}
6002
6003static const struct rpc_call_ops nfs41_sequence_ops = {
6004 .rpc_call_done = nfs41_sequence_call_done,
6005 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006006 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006007};
6008
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006009static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6010 struct rpc_cred *cred,
6011 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006012{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006013 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006014 struct rpc_message msg = {
6015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6016 .rpc_cred = cred,
6017 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018 struct rpc_task_setup task_setup_data = {
6019 .rpc_client = clp->cl_rpcclient,
6020 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006021 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006022 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6023 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024
Alexandros Batsakis71358402010-02-05 03:45:05 -08006025 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006026 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006027 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006028 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006029 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006030 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006031 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006032 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006033 if (is_privileged)
6034 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006035 msg.rpc_argp = &calldata->args;
6036 msg.rpc_resp = &calldata->res;
6037 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006038 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006039
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006040 return rpc_run_task(&task_setup_data);
6041}
6042
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006043static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006044{
6045 struct rpc_task *task;
6046 int ret = 0;
6047
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006048 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6049 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006050 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006051 if (IS_ERR(task))
6052 ret = PTR_ERR(task);
6053 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006054 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006055 dprintk("<-- %s status=%d\n", __func__, ret);
6056 return ret;
6057}
6058
6059static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6060{
6061 struct rpc_task *task;
6062 int ret;
6063
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006064 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006065 if (IS_ERR(task)) {
6066 ret = PTR_ERR(task);
6067 goto out;
6068 }
6069 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006070 if (!ret) {
6071 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6072
6073 if (task->tk_status == 0)
6074 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006075 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006076 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006077 rpc_put_task(task);
6078out:
6079 dprintk("<-- %s status=%d\n", __func__, ret);
6080 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006081}
6082
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006083struct nfs4_reclaim_complete_data {
6084 struct nfs_client *clp;
6085 struct nfs41_reclaim_complete_args arg;
6086 struct nfs41_reclaim_complete_res res;
6087};
6088
6089static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6090{
6091 struct nfs4_reclaim_complete_data *calldata = data;
6092
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006093 nfs41_setup_sequence(calldata->clp->cl_session,
6094 &calldata->arg.seq_args,
6095 &calldata->res.seq_res,
6096 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006097}
6098
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006099static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6100{
6101 switch(task->tk_status) {
6102 case 0:
6103 case -NFS4ERR_COMPLETE_ALREADY:
6104 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6105 break;
6106 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006107 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006108 /* fall through */
6109 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006110 return -EAGAIN;
6111 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006112 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006113 }
6114 return 0;
6115}
6116
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006117static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6118{
6119 struct nfs4_reclaim_complete_data *calldata = data;
6120 struct nfs_client *clp = calldata->clp;
6121 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6122
6123 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006124 if (!nfs41_sequence_done(task, res))
6125 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006126
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006127 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6128 rpc_restart_call_prepare(task);
6129 return;
6130 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006131 dprintk("<-- %s\n", __func__);
6132}
6133
6134static void nfs4_free_reclaim_complete_data(void *data)
6135{
6136 struct nfs4_reclaim_complete_data *calldata = data;
6137
6138 kfree(calldata);
6139}
6140
6141static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6142 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6143 .rpc_call_done = nfs4_reclaim_complete_done,
6144 .rpc_release = nfs4_free_reclaim_complete_data,
6145};
6146
6147/*
6148 * Issue a global reclaim complete.
6149 */
6150static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6151{
6152 struct nfs4_reclaim_complete_data *calldata;
6153 struct rpc_task *task;
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6156 };
6157 struct rpc_task_setup task_setup_data = {
6158 .rpc_client = clp->cl_rpcclient,
6159 .rpc_message = &msg,
6160 .callback_ops = &nfs4_reclaim_complete_call_ops,
6161 .flags = RPC_TASK_ASYNC,
6162 };
6163 int status = -ENOMEM;
6164
6165 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006166 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006167 if (calldata == NULL)
6168 goto out;
6169 calldata->clp = clp;
6170 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006171
Trond Myklebust9d12b212012-01-17 22:04:25 -05006172 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006173 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174 msg.rpc_argp = &calldata->arg;
6175 msg.rpc_resp = &calldata->res;
6176 task_setup_data.callback_data = calldata;
6177 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006178 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006179 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006180 goto out;
6181 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006182 status = nfs4_wait_for_completion_rpc_task(task);
6183 if (status == 0)
6184 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006185 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006186 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006187out:
6188 dprintk("<-- %s status=%d\n", __func__, status);
6189 return status;
6190}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006191
6192static void
6193nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6194{
6195 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006196 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006197 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006198
6199 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006200 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6201 * right now covering the LAYOUTGET we are about to send.
6202 * However, that is not so catastrophic, and there seems
6203 * to be no way to prevent it completely.
6204 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006205 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006206 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006207 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006208 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6209 NFS_I(lgp->args.inode)->layout,
6210 lgp->args.ctx->state)) {
6211 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006212 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006213}
6214
6215static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6216{
6217 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006218 struct inode *inode = lgp->args.inode;
6219 struct nfs_server *server = NFS_SERVER(inode);
6220 struct pnfs_layout_hdr *lo;
6221 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006222 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006223
6224 dprintk("--> %s\n", __func__);
6225
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006226 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006227 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006228
6229 switch (task->tk_status) {
6230 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006231 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006232 case -NFS4ERR_LAYOUTTRYLATER:
6233 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006234 timeo = rpc_get_timeout(task->tk_client);
6235 giveup = lgp->args.timestamp + timeo;
6236 if (time_after(giveup, jiffies))
6237 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006238 break;
6239 case -NFS4ERR_EXPIRED:
6240 case -NFS4ERR_BAD_STATEID:
6241 spin_lock(&inode->i_lock);
6242 lo = NFS_I(inode)->layout;
6243 if (!lo || list_empty(&lo->plh_segs)) {
6244 spin_unlock(&inode->i_lock);
6245 /* If the open stateid was bad, then recover it. */
6246 state = lgp->args.ctx->state;
6247 } else {
6248 LIST_HEAD(head);
6249
6250 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6251 spin_unlock(&inode->i_lock);
6252 /* Mark the bad layout state as invalid, then
6253 * retry using the open stateid. */
6254 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006255 }
6256 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006257 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6258 rpc_restart_call_prepare(task);
6259out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006260 dprintk("<-- %s\n", __func__);
6261}
6262
Idan Kedar85541162012-08-02 11:47:10 +03006263static size_t max_response_pages(struct nfs_server *server)
6264{
6265 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6266 return nfs_page_array_len(0, max_resp_sz);
6267}
6268
6269static void nfs4_free_pages(struct page **pages, size_t size)
6270{
6271 int i;
6272
6273 if (!pages)
6274 return;
6275
6276 for (i = 0; i < size; i++) {
6277 if (!pages[i])
6278 break;
6279 __free_page(pages[i]);
6280 }
6281 kfree(pages);
6282}
6283
6284static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6285{
6286 struct page **pages;
6287 int i;
6288
6289 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6290 if (!pages) {
6291 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6292 return NULL;
6293 }
6294
6295 for (i = 0; i < size; i++) {
6296 pages[i] = alloc_page(gfp_flags);
6297 if (!pages[i]) {
6298 dprintk("%s: failed to allocate page\n", __func__);
6299 nfs4_free_pages(pages, size);
6300 return NULL;
6301 }
6302 }
6303
6304 return pages;
6305}
6306
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006307static void nfs4_layoutget_release(void *calldata)
6308{
6309 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006310 struct inode *inode = lgp->args.inode;
6311 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006312 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006313
6314 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006315 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006316 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006317 put_nfs_open_context(lgp->args.ctx);
6318 kfree(calldata);
6319 dprintk("<-- %s\n", __func__);
6320}
6321
6322static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6323 .rpc_call_prepare = nfs4_layoutget_prepare,
6324 .rpc_call_done = nfs4_layoutget_done,
6325 .rpc_release = nfs4_layoutget_release,
6326};
6327
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006328struct pnfs_layout_segment *
6329nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006330{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006331 struct inode *inode = lgp->args.inode;
6332 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006333 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006334 struct rpc_task *task;
6335 struct rpc_message msg = {
6336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6337 .rpc_argp = &lgp->args,
6338 .rpc_resp = &lgp->res,
6339 };
6340 struct rpc_task_setup task_setup_data = {
6341 .rpc_client = server->client,
6342 .rpc_message = &msg,
6343 .callback_ops = &nfs4_layoutget_call_ops,
6344 .callback_data = lgp,
6345 .flags = RPC_TASK_ASYNC,
6346 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006347 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006348 int status = 0;
6349
6350 dprintk("--> %s\n", __func__);
6351
Idan Kedar85541162012-08-02 11:47:10 +03006352 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6353 if (!lgp->args.layout.pages) {
6354 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006355 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006356 }
6357 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006358 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006359
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006360 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006361 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006362 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006363
6364 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6365 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6366
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006367 task = rpc_run_task(&task_setup_data);
6368 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006369 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006370 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006371 if (status == 0)
6372 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006373 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6374 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006375 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006376 rpc_put_task(task);
6377 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006378 if (status)
6379 return ERR_PTR(status);
6380 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006381}
6382
Benny Halevycbe82602011-05-22 19:52:37 +03006383static void
6384nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6385{
6386 struct nfs4_layoutreturn *lrp = calldata;
6387
6388 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006389 nfs41_setup_sequence(lrp->clp->cl_session,
6390 &lrp->args.seq_args,
6391 &lrp->res.seq_res,
6392 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006393}
6394
6395static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6396{
6397 struct nfs4_layoutreturn *lrp = calldata;
6398 struct nfs_server *server;
6399
6400 dprintk("--> %s\n", __func__);
6401
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006402 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006403 return;
6404
6405 server = NFS_SERVER(lrp->args.inode);
6406 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006407 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006408 return;
6409 }
Benny Halevycbe82602011-05-22 19:52:37 +03006410 dprintk("<-- %s\n", __func__);
6411}
6412
6413static void nfs4_layoutreturn_release(void *calldata)
6414{
6415 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006416 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006417
6418 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006419 spin_lock(&lo->plh_inode->i_lock);
6420 if (lrp->res.lrs_present)
6421 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6422 lo->plh_block_lgets--;
6423 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006424 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006425 kfree(calldata);
6426 dprintk("<-- %s\n", __func__);
6427}
6428
6429static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6430 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6431 .rpc_call_done = nfs4_layoutreturn_done,
6432 .rpc_release = nfs4_layoutreturn_release,
6433};
6434
6435int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6436{
6437 struct rpc_task *task;
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6440 .rpc_argp = &lrp->args,
6441 .rpc_resp = &lrp->res,
6442 };
6443 struct rpc_task_setup task_setup_data = {
6444 .rpc_client = lrp->clp->cl_rpcclient,
6445 .rpc_message = &msg,
6446 .callback_ops = &nfs4_layoutreturn_call_ops,
6447 .callback_data = lrp,
6448 };
6449 int status;
6450
6451 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006452 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006453 task = rpc_run_task(&task_setup_data);
6454 if (IS_ERR(task))
6455 return PTR_ERR(task);
6456 status = task->tk_status;
6457 dprintk("<-- %s status=%d\n", __func__, status);
6458 rpc_put_task(task);
6459 return status;
6460}
6461
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006462/*
6463 * Retrieve the list of Data Server devices from the MDS.
6464 */
6465static int _nfs4_getdevicelist(struct nfs_server *server,
6466 const struct nfs_fh *fh,
6467 struct pnfs_devicelist *devlist)
6468{
6469 struct nfs4_getdevicelist_args args = {
6470 .fh = fh,
6471 .layoutclass = server->pnfs_curr_ld->id,
6472 };
6473 struct nfs4_getdevicelist_res res = {
6474 .devlist = devlist,
6475 };
6476 struct rpc_message msg = {
6477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6478 .rpc_argp = &args,
6479 .rpc_resp = &res,
6480 };
6481 int status;
6482
6483 dprintk("--> %s\n", __func__);
6484 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6485 &res.seq_res, 0);
6486 dprintk("<-- %s status=%d\n", __func__, status);
6487 return status;
6488}
6489
6490int nfs4_proc_getdevicelist(struct nfs_server *server,
6491 const struct nfs_fh *fh,
6492 struct pnfs_devicelist *devlist)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496
6497 do {
6498 err = nfs4_handle_exception(server,
6499 _nfs4_getdevicelist(server, fh, devlist),
6500 &exception);
6501 } while (exception.retry);
6502
6503 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6504 err, devlist->num_devs);
6505
6506 return err;
6507}
6508EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6509
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006510static int
6511_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6512{
6513 struct nfs4_getdeviceinfo_args args = {
6514 .pdev = pdev,
6515 };
6516 struct nfs4_getdeviceinfo_res res = {
6517 .pdev = pdev,
6518 };
6519 struct rpc_message msg = {
6520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6521 .rpc_argp = &args,
6522 .rpc_resp = &res,
6523 };
6524 int status;
6525
6526 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006527 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006528 dprintk("<-- %s status=%d\n", __func__, status);
6529
6530 return status;
6531}
6532
6533int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6534{
6535 struct nfs4_exception exception = { };
6536 int err;
6537
6538 do {
6539 err = nfs4_handle_exception(server,
6540 _nfs4_proc_getdeviceinfo(server, pdev),
6541 &exception);
6542 } while (exception.retry);
6543 return err;
6544}
6545EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6546
Andy Adamson863a3c62011-03-23 13:27:54 +00006547static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6548{
6549 struct nfs4_layoutcommit_data *data = calldata;
6550 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006551 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006552
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006553 nfs41_setup_sequence(session,
6554 &data->args.seq_args,
6555 &data->res.seq_res,
6556 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006557}
6558
6559static void
6560nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6561{
6562 struct nfs4_layoutcommit_data *data = calldata;
6563 struct nfs_server *server = NFS_SERVER(data->args.inode);
6564
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006565 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006566 return;
6567
6568 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006569 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6570 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6571 case -NFS4ERR_BADLAYOUT: /* no layout */
6572 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006573 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006574 break;
6575 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006576 nfs_post_op_update_inode_force_wcc(data->args.inode,
6577 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006578 break;
6579 default:
6580 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6581 rpc_restart_call_prepare(task);
6582 return;
6583 }
6584 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006585}
6586
6587static void nfs4_layoutcommit_release(void *calldata)
6588{
6589 struct nfs4_layoutcommit_data *data = calldata;
6590
Andy Adamsondb29c082011-07-30 20:52:38 -04006591 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006592 put_rpccred(data->cred);
6593 kfree(data);
6594}
6595
6596static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6597 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6598 .rpc_call_done = nfs4_layoutcommit_done,
6599 .rpc_release = nfs4_layoutcommit_release,
6600};
6601
6602int
Andy Adamsonef311532011-03-12 02:58:10 -05006603nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006604{
6605 struct rpc_message msg = {
6606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6607 .rpc_argp = &data->args,
6608 .rpc_resp = &data->res,
6609 .rpc_cred = data->cred,
6610 };
6611 struct rpc_task_setup task_setup_data = {
6612 .task = &data->task,
6613 .rpc_client = NFS_CLIENT(data->args.inode),
6614 .rpc_message = &msg,
6615 .callback_ops = &nfs4_layoutcommit_ops,
6616 .callback_data = data,
6617 .flags = RPC_TASK_ASYNC,
6618 };
6619 struct rpc_task *task;
6620 int status = 0;
6621
6622 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6623 "lbw: %llu inode %lu\n",
6624 data->task.tk_pid, sync,
6625 data->args.lastbytewritten,
6626 data->args.inode->i_ino);
6627
Trond Myklebust9d12b212012-01-17 22:04:25 -05006628 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006629 task = rpc_run_task(&task_setup_data);
6630 if (IS_ERR(task))
6631 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006632 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006633 goto out;
6634 status = nfs4_wait_for_completion_rpc_task(task);
6635 if (status != 0)
6636 goto out;
6637 status = task->tk_status;
6638out:
6639 dprintk("%s: status %d\n", __func__, status);
6640 rpc_put_task(task);
6641 return status;
6642}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006643
6644static int
6645_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6646 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6647{
6648 struct nfs41_secinfo_no_name_args args = {
6649 .style = SECINFO_STYLE_CURRENT_FH,
6650 };
6651 struct nfs4_secinfo_res res = {
6652 .flavors = flavors,
6653 };
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6656 .rpc_argp = &args,
6657 .rpc_resp = &res,
6658 };
6659 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6660}
6661
6662static int
6663nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6664 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6665{
6666 struct nfs4_exception exception = { };
6667 int err;
6668 do {
6669 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6670 switch (err) {
6671 case 0:
6672 case -NFS4ERR_WRONGSEC:
6673 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006674 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006675 default:
6676 err = nfs4_handle_exception(server, err, &exception);
6677 }
6678 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006679out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006680 return err;
6681}
6682
6683static int
6684nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6685 struct nfs_fsinfo *info)
6686{
6687 int err;
6688 struct page *page;
6689 rpc_authflavor_t flavor;
6690 struct nfs4_secinfo_flavors *flavors;
6691
6692 page = alloc_page(GFP_KERNEL);
6693 if (!page) {
6694 err = -ENOMEM;
6695 goto out;
6696 }
6697
6698 flavors = page_address(page);
6699 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6700
6701 /*
6702 * Fall back on "guess and check" method if
6703 * the server doesn't support SECINFO_NO_NAME
6704 */
6705 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6706 err = nfs4_find_root_sec(server, fhandle, info);
6707 goto out_freepage;
6708 }
6709 if (err)
6710 goto out_freepage;
6711
6712 flavor = nfs_find_best_sec(flavors);
6713 if (err == 0)
6714 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6715
6716out_freepage:
6717 put_page(page);
6718 if (err == -EACCES)
6719 return -EPERM;
6720out:
6721 return err;
6722}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006723
6724static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006725{
6726 int status;
6727 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006728 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006729 };
6730 struct nfs41_test_stateid_res res;
6731 struct rpc_message msg = {
6732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6733 .rpc_argp = &args,
6734 .rpc_resp = &res,
6735 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006736
Chuck Lever38527b12012-07-11 16:30:23 -04006737 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006738 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006739 nfs4_set_sequence_privileged(&args.seq_args);
6740 status = nfs4_call_sync_sequence(server->client, server, &msg,
6741 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006742 if (status != NFS_OK) {
6743 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006744 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006745 }
6746 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006747 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006748}
6749
Chuck Lever38527b12012-07-11 16:30:23 -04006750/**
6751 * nfs41_test_stateid - perform a TEST_STATEID operation
6752 *
6753 * @server: server / transport on which to perform the operation
6754 * @stateid: state ID to test
6755 *
6756 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6757 * Otherwise a negative NFS4ERR value is returned if the operation
6758 * failed or the state ID is not currently valid.
6759 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006760static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006761{
6762 struct nfs4_exception exception = { };
6763 int err;
6764 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006765 err = _nfs41_test_stateid(server, stateid);
6766 if (err != -NFS4ERR_DELAY)
6767 break;
6768 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006769 } while (exception.retry);
6770 return err;
6771}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006772
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006773static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006774{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006775 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006776 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006777 };
6778 struct nfs41_free_stateid_res res;
6779 struct rpc_message msg = {
6780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6781 .rpc_argp = &args,
6782 .rpc_resp = &res,
6783 };
Chuck Lever38527b12012-07-11 16:30:23 -04006784 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006785
Chuck Lever38527b12012-07-11 16:30:23 -04006786 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006787 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006788 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006789 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006790 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006791 dprintk("NFS reply free_stateid: %d\n", status);
6792 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006793}
6794
Chuck Lever38527b12012-07-11 16:30:23 -04006795/**
6796 * nfs41_free_stateid - perform a FREE_STATEID operation
6797 *
6798 * @server: server / transport on which to perform the operation
6799 * @stateid: state ID to release
6800 *
6801 * Returns NFS_OK if the server freed "stateid". Otherwise a
6802 * negative NFS4ERR value is returned.
6803 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006804static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006805{
6806 struct nfs4_exception exception = { };
6807 int err;
6808 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006809 err = _nfs4_free_stateid(server, stateid);
6810 if (err != -NFS4ERR_DELAY)
6811 break;
6812 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006813 } while (exception.retry);
6814 return err;
6815}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006816
6817static bool nfs41_match_stateid(const nfs4_stateid *s1,
6818 const nfs4_stateid *s2)
6819{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006820 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006821 return false;
6822
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006823 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006824 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006825 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006826 return true;
6827
6828 return false;
6829}
6830
Andy Adamson557134a2009-04-01 09:21:53 -04006831#endif /* CONFIG_NFS_V4_1 */
6832
Trond Myklebust36281ca2012-03-04 18:13:56 -05006833static bool nfs4_match_stateid(const nfs4_stateid *s1,
6834 const nfs4_stateid *s2)
6835{
Trond Myklebustf597c532012-03-04 18:13:56 -05006836 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006837}
6838
6839
Trond Myklebust17280172012-03-11 13:11:00 -04006840static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006841 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006842 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 .recover_open = nfs4_open_reclaim,
6844 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006845 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006846 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006847 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848};
6849
Andy Adamson591d71c2009-04-01 09:22:47 -04006850#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006851static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006852 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6853 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6854 .recover_open = nfs4_open_reclaim,
6855 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006856 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006857 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006858 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006859 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006860};
6861#endif /* CONFIG_NFS_V4_1 */
6862
Trond Myklebust17280172012-03-11 13:11:00 -04006863static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006864 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006865 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 .recover_open = nfs4_open_expired,
6867 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006868 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006869 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870};
6871
Andy Adamson591d71c2009-04-01 09:22:47 -04006872#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006873static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006874 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6875 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006876 .recover_open = nfs41_open_expired,
6877 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006878 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006879 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006880};
6881#endif /* CONFIG_NFS_V4_1 */
6882
Trond Myklebust17280172012-03-11 13:11:00 -04006883static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006884 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006885 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006886 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006887};
6888
6889#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006890static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006891 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006892 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006893 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006894};
6895#endif
6896
Trond Myklebust97dc1352010-06-16 09:52:26 -04006897static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6898 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006899 .init_caps = NFS_CAP_READDIRPLUS
6900 | NFS_CAP_ATOMIC_OPEN
6901 | NFS_CAP_CHANGE_ATTR
6902 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006903 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006904 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006905 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006906 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6907 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6908 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006909};
6910
6911#if defined(CONFIG_NFS_V4_1)
6912static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6913 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006914 .init_caps = NFS_CAP_READDIRPLUS
6915 | NFS_CAP_ATOMIC_OPEN
6916 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006917 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006918 | NFS_CAP_STATEID_NFSV41
6919 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006920 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006921 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006922 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006923 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6924 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6925 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006926};
6927#endif
6928
Trond Myklebust97dc1352010-06-16 09:52:26 -04006929const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6930 [0] = &nfs_v4_0_minor_ops,
6931#if defined(CONFIG_NFS_V4_1)
6932 [1] = &nfs_v4_1_minor_ops,
6933#endif
6934};
6935
Bryan Schumaker73a79702012-07-16 16:39:12 -04006936const struct inode_operations nfs4_dir_inode_operations = {
6937 .create = nfs_create,
6938 .lookup = nfs_lookup,
6939 .atomic_open = nfs_atomic_open,
6940 .link = nfs_link,
6941 .unlink = nfs_unlink,
6942 .symlink = nfs_symlink,
6943 .mkdir = nfs_mkdir,
6944 .rmdir = nfs_rmdir,
6945 .mknod = nfs_mknod,
6946 .rename = nfs_rename,
6947 .permission = nfs_permission,
6948 .getattr = nfs_getattr,
6949 .setattr = nfs_setattr,
6950 .getxattr = generic_getxattr,
6951 .setxattr = generic_setxattr,
6952 .listxattr = generic_listxattr,
6953 .removexattr = generic_removexattr,
6954};
6955
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006956static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006957 .permission = nfs_permission,
6958 .getattr = nfs_getattr,
6959 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006960 .getxattr = generic_getxattr,
6961 .setxattr = generic_setxattr,
6962 .listxattr = generic_listxattr,
6963 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006964};
6965
David Howells509de812006-08-22 20:06:11 -04006966const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 .version = 4, /* protocol version */
6968 .dentry_ops = &nfs4_dentry_operations,
6969 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006970 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006971 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006973 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006974 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 .getattr = nfs4_proc_getattr,
6976 .setattr = nfs4_proc_setattr,
6977 .lookup = nfs4_proc_lookup,
6978 .access = nfs4_proc_access,
6979 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 .create = nfs4_proc_create,
6981 .remove = nfs4_proc_remove,
6982 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006983 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 .unlink_done = nfs4_proc_unlink_done,
6985 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006986 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006987 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006988 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 .link = nfs4_proc_link,
6990 .symlink = nfs4_proc_symlink,
6991 .mkdir = nfs4_proc_mkdir,
6992 .rmdir = nfs4_proc_remove,
6993 .readdir = nfs4_proc_readdir,
6994 .mknod = nfs4_proc_mknod,
6995 .statfs = nfs4_proc_statfs,
6996 .fsinfo = nfs4_proc_fsinfo,
6997 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006998 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 .decode_dirent = nfs4_decode_dirent,
7000 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007001 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007002 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007003 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007005 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007006 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007007 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007009 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007010 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007012 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007013 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007014 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007015 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007016 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007017 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007018 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04007019 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007020 .create_server = nfs4_create_server,
7021 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022};
7023
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007024static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7025 .prefix = XATTR_NAME_NFSV4_ACL,
7026 .list = nfs4_xattr_list_nfs4_acl,
7027 .get = nfs4_xattr_get_nfs4_acl,
7028 .set = nfs4_xattr_set_nfs4_acl,
7029};
7030
7031const struct xattr_handler *nfs4_xattr_handlers[] = {
7032 &nfs4_xattr_nfs4_acl_handler,
7033 NULL
7034};
7035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036/*
7037 * Local variables:
7038 * c-basic-offset: 8
7039 * End:
7040 */