blob: c13144911d2017be38a10276098b0f83e9b1a96a [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 Myklebustb570a972013-04-12 15:04:51 -0400307 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
308 nfs_remove_bad_delegation(inode);
309 exception->retry = 1;
310 break;
311 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500312 if (state == NULL)
313 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400314 ret = nfs4_schedule_stateid_recovery(server, state);
315 if (ret < 0)
316 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400319 if (state != NULL) {
320 ret = nfs4_schedule_stateid_recovery(server, state);
321 if (ret < 0)
322 break;
323 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500325 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500326 nfs4_schedule_lease_recovery(clp);
327 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500328#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400329 case -NFS4ERR_BADSESSION:
330 case -NFS4ERR_BADSLOT:
331 case -NFS4ERR_BAD_HIGH_SLOT:
332 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
333 case -NFS4ERR_DEADSESSION:
334 case -NFS4ERR_SEQ_FALSE_RETRY:
335 case -NFS4ERR_SEQ_MISORDERED:
336 dprintk("%s ERROR: %d Reset session\n", __func__,
337 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400338 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400339 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500340#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500342 if (exception->timeout > HZ) {
343 /* We have retried a decent amount, time to
344 * fail
345 */
346 ret = -EBUSY;
347 break;
348 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500349 case -NFS4ERR_GRACE:
350 case -NFS4ERR_DELAY:
351 ret = nfs4_delay(server->client, &exception->timeout);
352 if (ret != 0)
353 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400354 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 case -NFS4ERR_OLD_STATEID:
356 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800357 break;
358 case -NFS4ERR_BADOWNER:
359 /* The following works around a Linux server bug! */
360 case -NFS4ERR_BADNAME:
361 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
362 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
363 exception->retry = 1;
364 printk(KERN_WARNING "NFS: v4 server %s "
365 "does not accept raw "
366 "uid/gids. "
367 "Reenabling the idmapper.\n",
368 server->nfs_client->cl_hostname);
369 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500370 }
371 /* We failed to handle the error */
372 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500373wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500374 ret = nfs4_wait_clnt_recover(clp);
375 if (ret == 0)
376 exception->retry = 1;
377 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378}
379
380
Trond Myklebust452e9352010-07-31 14:29:06 -0400381static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_lock(&clp->cl_lock);
384 if (time_before(clp->cl_last_renewal,timestamp))
385 clp->cl_last_renewal = timestamp;
386 spin_unlock(&clp->cl_lock);
387}
388
Trond Myklebust452e9352010-07-31 14:29:06 -0400389static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
390{
391 do_renew_lease(server->nfs_client, timestamp);
392}
393
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400394#if defined(CONFIG_NFS_V4_1)
395
Trond Myklebustd185a332010-06-16 09:52:25 -0400396static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400397{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500398 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400399 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500400 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400401
Benny Halevydfb4f3092010-09-24 09:17:01 -0400402 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400403 /* just wake up the next guy waiting since
404 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500405 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500406 return;
Andy Adamson13615872009-04-01 09:22:17 -0400407 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500408 tbl = res->sr_slot->table;
409 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500410
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500411 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500412 /* Be nice to the server: try to ensure that the last transmitted
413 * value for highest_user_slotid <= target_highest_slotid
414 */
415 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
416 send_new_highest_used_slotid = true;
417
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500418 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
419 send_new_highest_used_slotid = false;
420 goto out_unlock;
421 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500422 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500423
424 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
425 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500426out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400428 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500429 if (send_new_highest_used_slotid)
430 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400431}
432
Trond Myklebust14516c32010-07-31 14:29:06 -0400433static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500435 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500436 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500438 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500439 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Trond Myklebustac20d162012-12-15 15:36:07 -0500448 if (slot->interrupted) {
449 slot->interrupted = 0;
450 interrupted = true;
451 }
452
Andy Adamson691daf32009-12-04 15:55:39 -0500453 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 switch (res->sr_status) {
455 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500457 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500458 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500459 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500460 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500461 if (res->sr_status_flags != 0)
462 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500463 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500465 case 1:
466 /*
467 * sr_status remains 1 if an RPC level error occurred.
468 * The server may or may not have processed the sequence
469 * operation..
470 * Mark the slot as having hosted an interrupted RPC call.
471 */
472 slot->interrupted = 1;
473 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400474 case -NFS4ERR_DELAY:
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
477 * of RFC5661.
478 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500479 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500481 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500482 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500484 case -NFS4ERR_BADSLOT:
485 /*
486 * The slot id we used was probably retired. Try again
487 * using a different slot id.
488 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500489 goto retry_nowait;
490 case -NFS4ERR_SEQ_MISORDERED:
491 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500492 * Was the last operation on this sequence interrupted?
493 * If so, retry after bumping the sequence number.
494 */
495 if (interrupted) {
496 ++slot->seq_nr;
497 goto retry_nowait;
498 }
499 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500500 * Could this slot have been previously retired?
501 * If so, then the server may be expecting seq_nr = 1!
502 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500503 if (slot->seq_nr != 1) {
504 slot->seq_nr = 1;
505 goto retry_nowait;
506 }
507 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500508 case -NFS4ERR_SEQ_FALSE_RETRY:
509 ++slot->seq_nr;
510 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 default:
512 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500513 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514 }
515out:
516 /* The session may be reset by one of the error handlers. */
517 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400518 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500519 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500520retry_nowait:
521 if (rpc_restart_call_prepare(task)) {
522 task->tk_status = 0;
523 ret = 0;
524 }
525 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400526out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400527 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 goto out;
529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
530 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400531}
532
Trond Myklebust14516c32010-07-31 14:29:06 -0400533static int nfs4_sequence_done(struct rpc_task *task,
534 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400535{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500536 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 return 1;
538 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400539}
540
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
543{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500544 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400546 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500547 if (cache_reply)
548 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500549 res->sr_slot = NULL;
550}
551
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400552static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
553{
554 args->sa_privileged = 1;
555}
556
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000557int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct rpc_task *task)
561{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400562 struct nfs4_slot *slot;
563 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564
565 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400567 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400568 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 tbl = &session->fc_slot_table;
571
Trond Myklebust69d206b2012-11-22 13:21:02 -0500572 task->tk_timeout = 0;
573
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400575 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400576 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500577 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400579 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400580 }
581
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500582 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500583 if (IS_ERR(slot)) {
584 /* If out of memory, try again in 1/4 second */
585 if (slot == ERR_PTR(-ENOMEM))
586 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400588 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500592 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500594 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
595 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
Benny Halevydfb4f3092010-09-24 09:17:01 -0400597 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500598 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400599 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 /*
601 * sr_status is only set in decode_sequence, and so will remain
602 * set to 1 if an rpc level failure occurs.
603 */
604 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400605out_success:
606 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400608out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400609 /* Privileged tasks are queued with top priority */
610 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400611 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
612 NULL, RPC_PRIORITY_PRIVILEGED);
613 else
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400615 spin_unlock(&tbl->slot_tbl_lock);
616 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000618EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619
Trond Myklebust035168a2010-06-16 09:52:26 -0400620int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 struct rpc_task *task)
624{
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 int ret = 0;
627
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400628 if (session == NULL) {
629 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400631 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500633 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400634 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638out:
639 dprintk("<-- %s status=%d\n", __func__, ret);
640 return ret;
641}
642
643struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647};
648
649static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400652 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
655
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400656 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Trond Myklebust17280172012-03-11 13:11:00 -0400666static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400675 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
Trond Myklebustdf896452010-06-16 09:52:26 -0400701#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400702static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500703void nfs41_init_sequence(struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res, int cache_reply)
705{
706}
707
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400708static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709{
710}
711
712
Trond Myklebust14516c32010-07-31 14:29:06 -0400713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400715{
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400717}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718#endif /* CONFIG_NFS_V4_1 */
719
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400720static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728}
729
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400730static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400740 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700749 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400750 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000752 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500762 struct nfs4_string owner_name;
763 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100772 int rpc_status;
773 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774};
775
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400776static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
777 int err, struct nfs4_exception *exception)
778{
779 if (err != -EINVAL)
780 return false;
781 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
782 return false;
783 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
784 exception->retry = 1;
785 return true;
786}
787
788static enum open_claim_type4
789nfs4_map_atomic_open_claim(struct nfs_server *server,
790 enum open_claim_type4 claim)
791{
792 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
793 return claim;
794 switch (claim) {
795 default:
796 return claim;
797 case NFS4_OPEN_CLAIM_FH:
798 return NFS4_OPEN_CLAIM_NULL;
799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
800 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
801 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
802 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
803 }
804}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400805
806static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807{
808 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400812 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400820 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400821 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct inode *dir = parent->d_inode;
825 struct nfs_server *server = NFS_SERVER(dir);
826 struct nfs4_opendata *p;
827
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p == NULL)
830 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400831 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 if (p->o_arg.seqid == NULL)
833 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400834 nfs_sb_active(dentry->d_sb);
835 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 p->dir = parent;
837 p->owner = sp;
838 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700841 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
842 * will return permission denied for all bits until close */
843 if (!(flags & O_EXCL)) {
844 /* ask server to check for all possible rights as results
845 * are cached */
846 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
847 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700848 }
David Howells7539bba2006-08-22 20:06:09 -0400849 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400850 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
851 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 p->o_arg.server = server;
854 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400855 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400856 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
857 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400858 case NFS4_OPEN_CLAIM_NULL:
859 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
860 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
861 p->o_arg.fh = NFS_FH(dir);
862 break;
863 case NFS4_OPEN_CLAIM_PREVIOUS:
864 case NFS4_OPEN_CLAIM_FH:
865 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
866 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
867 p->o_arg.fh = NFS_FH(dentry->d_inode);
868 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500869 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500870 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400871
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500874
875 verf[0] = jiffies;
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400883 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 return p;
886err_free:
887 kfree(p);
888err:
889 dput(parent);
890 return NULL;
891}
892
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898
899 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 nfs4_put_state_owner(p->owner);
903 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 dput(p->dentry);
905 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500906 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400907 kfree(p);
908}
909
910static void nfs4_opendata_put(struct nfs4_opendata *p)
911{
912 if (p != NULL)
913 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914}
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
917{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918 int ret;
919
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925{
926 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927
Trond Myklebust536e43d2012-01-17 22:04:26 -0500928 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 goto out;
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 break;
939 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 return ret;
945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500949 if (delegation == NULL)
950 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500955 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400981 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 case FMODE_READ:
984 set_bit(NFS_O_RDONLY_STATE, &state->flags);
985 break;
986 case FMODE_WRITE:
987 set_bit(NFS_O_WRONLY_STATE, &state->flags);
988 break;
989 case FMODE_READ|FMODE_WRITE:
990 set_bit(NFS_O_RDWR_STATE, &state->flags);
991 }
992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400995{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400996 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400999}
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001static 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 -07001002{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001003 /*
1004 * Protect the call to nfs4_state_set_mode_locked and
1005 * serialise the stateid update
1006 */
1007 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001008 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001009 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001010 set_bit(NFS_DELEGATED_STATE, &state->flags);
1011 }
1012 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_sequnlock(&state->seqlock);
1015 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001017 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static 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 -05001021{
1022 struct nfs_inode *nfsi = NFS_I(state->inode);
1023 struct nfs_delegation *deleg_cur;
1024 int ret = 0;
1025
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001027
1028 rcu_read_lock();
1029 deleg_cur = rcu_dereference(nfsi->delegation);
1030 if (deleg_cur == NULL)
1031 goto no_delegation;
1032
1033 spin_lock(&deleg_cur->lock);
1034 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001035 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
1039 if (delegation == NULL)
1040 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001041 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001042 goto no_delegation_unlock;
1043
Trond Myklebustb7391f42008-12-23 15:21:52 -05001044 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001046 ret = 1;
1047no_delegation_unlock:
1048 spin_unlock(&deleg_cur->lock);
1049no_delegation:
1050 rcu_read_unlock();
1051
1052 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001054 ret = 1;
1055 }
1056
1057 return ret;
1058}
1059
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062{
1063 struct nfs_delegation *delegation;
1064
1065 rcu_read_lock();
1066 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 rcu_read_unlock();
1069 return;
1070 }
1071 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001072 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073}
1074
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076{
1077 struct nfs4_state *state = opendata->state;
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001080 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 nfs4_stateid stateid;
1083 int ret = -EAGAIN;
1084
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 if (can_open_cached(state, fmode, open_mode)) {
1089 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 goto out_return_state;
1092 }
1093 spin_unlock(&state->owner->so_lock);
1094 }
Trond Myklebust34310432008-12-23 15:21:38 -05001095 rcu_read_lock();
1096 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001097 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001100 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001102 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001104 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 if (ret != 0)
1106 goto out;
1107 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001108
1109 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113out:
1114 return ERR_PTR(ret);
1115out_return_state:
1116 atomic_inc(&state->count);
1117 return state;
1118}
1119
Andy Adamsone23008e2012-10-02 21:07:32 -04001120static void
1121nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1122{
1123 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1124 struct nfs_delegation *delegation;
1125 int delegation_flags = 0;
1126
1127 rcu_read_lock();
1128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1129 if (delegation)
1130 delegation_flags = delegation->flags;
1131 rcu_read_unlock();
1132 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1133 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1134 "returning a delegation for "
1135 "OPEN(CLAIM_DELEGATE_CUR)\n",
1136 clp->cl_hostname);
1137 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1138 nfs_inode_set_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 else
1142 nfs_inode_reclaim_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145}
1146
1147/*
1148 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1149 * and update the nfs4_state.
1150 */
1151static struct nfs4_state *
1152_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1153{
1154 struct inode *inode = data->state->inode;
1155 struct nfs4_state *state = data->state;
1156 int ret;
1157
1158 if (!data->rpc_done) {
1159 ret = data->rpc_status;
1160 goto err;
1161 }
1162
1163 ret = -ESTALE;
1164 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1165 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1166 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1167 goto err;
1168
1169 ret = -ENOMEM;
1170 state = nfs4_get_open_state(inode, data->owner);
1171 if (state == NULL)
1172 goto err;
1173
1174 ret = nfs_refresh_inode(inode, &data->f_attr);
1175 if (ret)
1176 goto err;
1177
1178 if (data->o_res.delegation_type != 0)
1179 nfs4_opendata_check_deleg(data, state);
1180 update_open_stateid(state, &data->o_res.stateid, NULL,
1181 data->o_arg.fmode);
1182
1183 return state;
1184err:
1185 return ERR_PTR(ret);
1186
1187}
1188
1189static struct nfs4_state *
1190_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001191{
1192 struct inode *inode;
1193 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001194 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001195
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001197 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001198 goto out;
1199 }
1200
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001202 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001203 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001205 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001206 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001207 goto err;
1208 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209 state = nfs4_get_open_state(inode, data->owner);
1210 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001212 if (data->o_res.delegation_type != 0)
1213 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001214 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001217out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001218 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220err_put_inode:
1221 iput(inode);
1222err:
1223 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001224}
1225
Andy Adamsone23008e2012-10-02 21:07:32 -04001226static struct nfs4_state *
1227nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1228{
1229 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1230 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1231 return _nfs4_opendata_to_nfs4_state(data);
1232}
1233
Trond Myklebust864472e2006-01-03 09:55:15 +01001234static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1235{
1236 struct nfs_inode *nfsi = NFS_I(state->inode);
1237 struct nfs_open_context *ctx;
1238
1239 spin_lock(&state->inode->i_lock);
1240 list_for_each_entry(ctx, &nfsi->open_files, list) {
1241 if (ctx->state != state)
1242 continue;
1243 get_nfs_open_context(ctx);
1244 spin_unlock(&state->inode->i_lock);
1245 return ctx;
1246 }
1247 spin_unlock(&state->inode->i_lock);
1248 return ERR_PTR(-ENOENT);
1249}
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1252 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253{
1254 struct nfs4_opendata *opendata;
1255
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001256 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1257 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001258 if (opendata == NULL)
1259 return ERR_PTR(-ENOMEM);
1260 opendata->state = state;
1261 atomic_inc(&state->count);
1262 return opendata;
1263}
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001266{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 int ret;
1269
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 opendata->o_arg.open_flags = 0;
1271 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001272 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1273 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1274 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001275 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 if (ret != 0)
1277 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001278 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001279 if (IS_ERR(newstate))
1280 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001281 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001282 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 return 0;
1284}
1285
1286static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1287{
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 int ret;
1290
1291 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001293 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 smp_rmb();
1295 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001296 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001297 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 if (ret != 0)
1299 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001300 if (newstate != state)
1301 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 }
1303 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001304 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001305 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 if (ret != 0)
1307 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 if (newstate != state)
1309 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 }
1311 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001312 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001313 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 if (ret != 0)
1315 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 if (newstate != state)
1317 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001319 /*
1320 * We may have performed cached opens for all three recoveries.
1321 * Check if we need to update the current stateid.
1322 */
1323 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001324 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001325 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001326 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001327 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001328 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001329 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001330 return 0;
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * OPEN_RECLAIM:
1335 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001337static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001339 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int status;
1343
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001344 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1345 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001346 if (IS_ERR(opendata))
1347 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001348 rcu_read_lock();
1349 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001350 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001351 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001352 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 opendata->o_arg.u.delegation_type = delegation_type;
1354 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001355 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return status;
1357}
1358
Trond Myklebust539cd032007-06-05 11:46:42 -04001359static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct nfs_server *server = NFS_SERVER(state->inode);
1362 struct nfs4_exception exception = { };
1363 int err;
1364 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001365 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001366 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1367 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001368 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001369 break;
1370 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } while (exception.retry);
1372 return err;
1373}
1374
Trond Myklebust864472e2006-01-03 09:55:15 +01001375static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1376{
1377 struct nfs_open_context *ctx;
1378 int ret;
1379
1380 ctx = nfs4_state_find_open_context(state);
1381 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001382 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001383 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001384 put_nfs_open_context(ctx);
1385 return ret;
1386}
1387
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001388static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001390 switch (err) {
1391 default:
1392 printk(KERN_ERR "NFS: %s: unhandled error "
1393 "%d.\n", __func__, err);
1394 case 0:
1395 case -ENOENT:
1396 case -ESTALE:
1397 break;
1398 case -NFS4ERR_BADSESSION:
1399 case -NFS4ERR_BADSLOT:
1400 case -NFS4ERR_BAD_HIGH_SLOT:
1401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1402 case -NFS4ERR_DEADSESSION:
1403 set_bit(NFS_DELEGATED_STATE, &state->flags);
1404 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1405 return -EAGAIN;
1406 case -NFS4ERR_STALE_CLIENTID:
1407 case -NFS4ERR_STALE_STATEID:
1408 set_bit(NFS_DELEGATED_STATE, &state->flags);
1409 case -NFS4ERR_EXPIRED:
1410 /* Don't recall a delegation if it was lost */
1411 nfs4_schedule_lease_recovery(server->nfs_client);
1412 return -EAGAIN;
1413 case -NFS4ERR_DELEG_REVOKED:
1414 case -NFS4ERR_ADMIN_REVOKED:
1415 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001416 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001417 nfs_inode_find_state_and_recover(state->inode,
1418 stateid);
1419 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001420 return 0;
1421 case -NFS4ERR_DELAY:
1422 case -NFS4ERR_GRACE:
1423 set_bit(NFS_DELEGATED_STATE, &state->flags);
1424 ssleep(1);
1425 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001426 case -ENOMEM:
1427 case -NFS4ERR_DENIED:
1428 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1429 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return err;
1432}
1433
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001434int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1435{
1436 struct nfs_server *server = NFS_SERVER(state->inode);
1437 struct nfs4_opendata *opendata;
1438 int err;
1439
1440 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1441 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1442 if (IS_ERR(opendata))
1443 return PTR_ERR(opendata);
1444 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1445 err = nfs4_open_recover(opendata, state);
1446 nfs4_opendata_put(opendata);
1447 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1448}
1449
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001450static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1451{
1452 struct nfs4_opendata *data = calldata;
1453
1454 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001455 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001457 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001459 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001460 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001461}
1462
1463static void nfs4_open_confirm_release(void *calldata)
1464{
1465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state *state = NULL;
1467
1468 /* If this request hasn't been cancelled, do nothing */
1469 if (data->cancelled == 0)
1470 goto out_free;
1471 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001472 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001474 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001475 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001476 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001478 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001479}
1480
1481static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001482 .rpc_call_done = nfs4_open_confirm_done,
1483 .rpc_release = nfs4_open_confirm_release,
1484};
1485
1486/*
1487 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1488 */
1489static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1490{
1491 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1492 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001493 struct rpc_message msg = {
1494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1495 .rpc_argp = &data->c_arg,
1496 .rpc_resp = &data->c_res,
1497 .rpc_cred = data->owner->so_cred,
1498 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 struct rpc_task_setup task_setup_data = {
1500 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001502 .callback_ops = &nfs4_open_confirm_ops,
1503 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001504 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001505 .flags = RPC_TASK_ASYNC,
1506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int status;
1508
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001509 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001510 data->rpc_done = 0;
1511 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001512 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001513 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001514 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001515 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001516 status = nfs4_wait_for_completion_rpc_task(task);
1517 if (status != 0) {
1518 data->cancelled = 1;
1519 smp_wmb();
1520 } else
1521 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001522 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return status;
1524}
1525
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 struct nfs4_opendata *data = calldata;
1529 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001530 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001531
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001533 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001534 /*
1535 * Check if we still need to send an OPEN call, or if we can use
1536 * a delegation instead.
1537 */
1538 if (data->state != NULL) {
1539 struct nfs_delegation *delegation;
1540
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001542 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_lock();
1544 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001545 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1546 can_open_delegated(delegation, data->o_arg.fmode))
1547 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001550 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001551 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001552 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001553 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001554 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001555 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1556 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001557 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001558 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001559 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001560 &data->o_res.seq_res,
1561 task) != 0)
1562 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001563
1564 /* Set the create mode (note dependency on the session type) */
1565 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1566 if (data->o_arg.open_flags & O_EXCL) {
1567 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1568 if (nfs4_has_persistent_session(clp))
1569 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1570 else if (clp->cl_mvops->minor_version > 0)
1571 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1572 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001574unlock_no_action:
1575 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001576out_no_action:
1577 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001578out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001579 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001580}
1581
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582static void nfs4_open_done(struct rpc_task *task, void *calldata)
1583{
1584 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001587
Trond Myklebust14516c32010-07-31 14:29:06 -04001588 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1589 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001592 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1593 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001594 case S_IFREG:
1595 break;
1596 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001598 break;
1599 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001601 break;
1602 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001604 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001605 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001606 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001607 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1608 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001610 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001612
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613static void nfs4_open_release(void *calldata)
1614{
1615 struct nfs4_opendata *data = calldata;
1616 struct nfs4_state *state = NULL;
1617
1618 /* If this request hasn't been cancelled, do nothing */
1619 if (data->cancelled == 0)
1620 goto out_free;
1621 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001622 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 goto out_free;
1624 /* In case we need an open_confirm, no cleanup! */
1625 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1626 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001628 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001629 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001631 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632}
1633
1634static const struct rpc_call_ops nfs4_open_ops = {
1635 .rpc_call_prepare = nfs4_open_prepare,
1636 .rpc_call_done = nfs4_open_done,
1637 .rpc_release = nfs4_open_release,
1638};
1639
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001640static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641{
1642 struct inode *dir = data->dir->d_inode;
1643 struct nfs_server *server = NFS_SERVER(dir);
1644 struct nfs_openargs *o_arg = &data->o_arg;
1645 struct nfs_openres *o_res = &data->o_res;
1646 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001647 struct rpc_message msg = {
1648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1649 .rpc_argp = o_arg,
1650 .rpc_resp = o_res,
1651 .rpc_cred = data->owner->so_cred,
1652 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001653 struct rpc_task_setup task_setup_data = {
1654 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001655 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001656 .callback_ops = &nfs4_open_ops,
1657 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001658 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001659 .flags = RPC_TASK_ASYNC,
1660 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 int status;
1662
Trond Myklebust9d12b212012-01-17 22:04:25 -05001663 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001664 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001665 data->rpc_done = 0;
1666 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001667 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001668 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001669 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001670 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001671 if (IS_ERR(task))
1672 return PTR_ERR(task);
1673 status = nfs4_wait_for_completion_rpc_task(task);
1674 if (status != 0) {
1675 data->cancelled = 1;
1676 smp_wmb();
1677 } else
1678 status = data->rpc_status;
1679 rpc_put_task(task);
1680
1681 return status;
1682}
1683
1684static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1685{
1686 struct inode *dir = data->dir->d_inode;
1687 struct nfs_openres *o_res = &data->o_res;
1688 int status;
1689
1690 status = nfs4_run_open_task(data, 1);
1691 if (status != 0 || !data->rpc_done)
1692 return status;
1693
Trond Myklebust6926afd2012-01-07 13:22:46 -05001694 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1695
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001696 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1697 status = _nfs4_proc_open_confirm(data);
1698 if (status != 0)
1699 return status;
1700 }
1701
1702 return status;
1703}
1704
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001705static int nfs4_opendata_access(struct rpc_cred *cred,
1706 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001707 struct nfs4_state *state, fmode_t fmode,
1708 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001709{
1710 struct nfs_access_entry cache;
1711 u32 mask;
1712
1713 /* access call failed or for some reason the server doesn't
1714 * support any access modes -- defer access call until later */
1715 if (opendata->o_res.access_supported == 0)
1716 return 0;
1717
1718 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001719 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001720 * write mode bits, but POSIX allows the creating process to write.
1721 * use openflags to check for exec, because fmode won't
1722 * always have FMODE_EXEC set when file open for exec. */
1723 if (openflags & __FMODE_EXEC) {
1724 /* ONLY check for exec rights */
1725 mask = MAY_EXEC;
1726 } else if (fmode & FMODE_READ)
1727 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001728
1729 cache.cred = cred;
1730 cache.jiffies = jiffies;
1731 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1732 nfs_access_add_cache(state->inode, &cache);
1733
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001734 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001735 return 0;
1736
1737 /* even though OPEN succeeded, access is denied. Close the file */
1738 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001739 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001740}
1741
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001742/*
1743 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1744 */
1745static int _nfs4_proc_open(struct nfs4_opendata *data)
1746{
1747 struct inode *dir = data->dir->d_inode;
1748 struct nfs_server *server = NFS_SERVER(dir);
1749 struct nfs_openargs *o_arg = &data->o_arg;
1750 struct nfs_openres *o_res = &data->o_res;
1751 int status;
1752
1753 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001754 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001756 if (status != 0) {
1757 if (status == -NFS4ERR_BADNAME &&
1758 !(o_arg->open_flags & O_CREAT))
1759 return -ENOENT;
1760 return status;
1761 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762
Trond Myklebust6926afd2012-01-07 13:22:46 -05001763 nfs_fattr_map_and_free_names(server, &data->f_attr);
1764
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001765 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001766 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001767 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1768 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001770 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001775 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Andy Adamsond83217c2011-03-01 01:34:17 +00001779static int nfs4_recover_expired_lease(struct nfs_server *server)
1780{
1781 return nfs4_client_recover_expired_lease(server->nfs_client);
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784/*
1785 * OPEN_EXPIRED:
1786 * reclaim state on the server after a network partition.
1787 * Assumes caller holds the appropriate lock
1788 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001789static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001791 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001792 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001794 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001795 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001796 if (IS_ERR(opendata))
1797 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001798 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001799 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001800 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001801 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001802 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001805static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001806{
Trond Myklebust539cd032007-06-05 11:46:42 -04001807 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001808 struct nfs4_exception exception = { };
1809 int err;
1810
1811 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001812 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001813 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1814 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001815 switch (err) {
1816 default:
1817 goto out;
1818 case -NFS4ERR_GRACE:
1819 case -NFS4ERR_DELAY:
1820 nfs4_handle_exception(server, err, &exception);
1821 err = 0;
1822 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001823 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001824out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001825 return err;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001831 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Trond Myklebust864472e2006-01-03 09:55:15 +01001833 ctx = nfs4_state_find_open_context(state);
1834 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001835 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001836 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 put_nfs_open_context(ctx);
1838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001841#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001842static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001843{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001844 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001845 nfs4_stateid *stateid = &state->stateid;
1846 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001847
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001848 /* If a state reset has been done, test_stateid is unneeded */
1849 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1850 return;
1851
1852 status = nfs41_test_stateid(server, stateid);
1853 if (status != NFS_OK) {
1854 /* Free the stateid unless the server explicitly
1855 * informs us the stateid is unrecognized. */
1856 if (status != -NFS4ERR_BAD_STATEID)
1857 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001858 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001859
Bryan Schumaker69388672012-09-26 15:25:52 -04001860 write_seqlock(&state->seqlock);
1861 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1862 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001863 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1864 }
1865}
1866
1867/**
1868 * nfs41_check_open_stateid - possibly free an open stateid
1869 *
1870 * @state: NFSv4 state for an inode
1871 *
1872 * Returns NFS_OK if recovery for this stateid is now finished.
1873 * Otherwise a negative NFS4ERR value is returned.
1874 */
1875static int nfs41_check_open_stateid(struct nfs4_state *state)
1876{
1877 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001878 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001879 int status;
1880
1881 /* If a state reset has been done, test_stateid is unneeded */
1882 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1883 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1884 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1885 return -NFS4ERR_BAD_STATEID;
1886
1887 status = nfs41_test_stateid(server, stateid);
1888 if (status != NFS_OK) {
1889 /* Free the stateid unless the server explicitly
1890 * informs us the stateid is unrecognized. */
1891 if (status != -NFS4ERR_BAD_STATEID)
1892 nfs41_free_stateid(server, stateid);
1893
1894 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1895 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1896 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001897 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001898 }
1899 return status;
1900}
1901
1902static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1903{
Chuck Levereb64cf92012-07-11 16:30:05 -04001904 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001905
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001906 nfs41_clear_delegation_stateid(state);
1907 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001908 if (status != NFS_OK)
1909 status = nfs4_open_expired(sp, state);
1910 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001911}
1912#endif
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001915 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1916 * fields corresponding to attributes that were used to store the verifier.
1917 * Make sure we clobber those fields in the later setattr call
1918 */
1919static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1920{
1921 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1922 !(sattr->ia_valid & ATTR_ATIME_SET))
1923 sattr->ia_valid |= ATTR_ATIME;
1924
1925 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1926 !(sattr->ia_valid & ATTR_MTIME_SET))
1927 sattr->ia_valid |= ATTR_MTIME;
1928}
1929
Trond Myklebustc21443c2013-02-07 14:26:21 -05001930static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1931 fmode_t fmode,
1932 int flags,
1933 struct nfs4_state **res)
1934{
1935 struct nfs4_state_owner *sp = opendata->owner;
1936 struct nfs_server *server = sp->so_server;
1937 struct nfs4_state *state;
1938 unsigned int seq;
1939 int ret;
1940
1941 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1942
1943 ret = _nfs4_proc_open(opendata);
1944 if (ret != 0)
1945 goto out;
1946
1947 state = nfs4_opendata_to_nfs4_state(opendata);
1948 ret = PTR_ERR(state);
1949 if (IS_ERR(state))
1950 goto out;
1951 if (server->caps & NFS_CAP_POSIX_LOCK)
1952 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1953
1954 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1955 if (ret != 0)
1956 goto out;
1957
Trond Myklebust91876b12013-03-28 14:01:33 -04001958 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001959 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001960 *res = state;
1961out:
1962 return ret;
1963}
1964
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001965/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001966 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 */
Andy Adamson82be4172012-05-23 05:02:35 -04001968static int _nfs4_do_open(struct inode *dir,
1969 struct dentry *dentry,
1970 fmode_t fmode,
1971 int flags,
1972 struct iattr *sattr,
1973 struct rpc_cred *cred,
1974 struct nfs4_state **res,
1975 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct nfs4_state_owner *sp;
1978 struct nfs4_state *state = NULL;
1979 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001980 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001981 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001982 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001986 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1987 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1989 goto out_err;
1990 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001991 status = nfs4_recover_expired_lease(server);
1992 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001993 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001994 if (dentry->d_inode != NULL)
1995 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001996 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001997 if (dentry->d_inode)
1998 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001999 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002000 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002001 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002002 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Andy Adamson82be4172012-05-23 05:02:35 -04002004 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2005 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2006 if (!opendata->f_attr.mdsthreshold)
2007 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002008 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002009 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002010 if (dentry->d_inode != NULL)
2011 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002012
Trond Myklebustc21443c2013-02-07 14:26:21 -05002013 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002014 if (status != 0)
2015 goto err_opendata_put;
2016
Trond Myklebust549b19c2013-04-16 18:42:34 -04002017 if ((opendata->o_arg.open_flags & O_EXCL) &&
2018 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002019 nfs4_exclusive_attrset(opendata, sattr);
2020
2021 nfs_fattr_init(opendata->o_res.f_attr);
2022 status = nfs4_do_setattr(state->inode, cred,
2023 opendata->o_res.f_attr, sattr,
2024 state);
2025 if (status == 0)
2026 nfs_setattr_update_inode(state->inode, sattr);
2027 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2028 }
Andy Adamson82be4172012-05-23 05:02:35 -04002029
2030 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2031 *ctx_th = opendata->f_attr.mdsthreshold;
2032 else
2033 kfree(opendata->f_attr.mdsthreshold);
2034 opendata->f_attr.mdsthreshold = NULL;
2035
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002036 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 *res = state;
2039 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002040err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002041 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002042 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002043err_put_state_owner:
2044 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 *res = NULL;
2047 return status;
2048}
2049
2050
Andy Adamson82be4172012-05-23 05:02:35 -04002051static struct nfs4_state *nfs4_do_open(struct inode *dir,
2052 struct dentry *dentry,
2053 fmode_t fmode,
2054 int flags,
2055 struct iattr *sattr,
2056 struct rpc_cred *cred,
2057 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002059 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct nfs4_exception exception = { };
2061 struct nfs4_state *res;
2062 int status;
2063
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002064 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002066 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2067 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (status == 0)
2069 break;
2070 /* NOTE: BAD_SEQID means the server and client disagree about the
2071 * book-keeping w.r.t. state-changing operations
2072 * (OPEN/CLOSE/LOCK/LOCKU...)
2073 * It is actually a sign of a bug on the client or on the server.
2074 *
2075 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002076 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 * have unhashed the old state_owner for us, and that we can
2078 * therefore safely retry using a new one. We should still warn
2079 * the user though...
2080 */
2081 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002082 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002083 " returned a bad sequence-id error!\n",
2084 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 exception.retry = 1;
2086 continue;
2087 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002088 /*
2089 * BAD_STATEID on OPEN means that the server cancelled our
2090 * state before it received the OPEN_CONFIRM.
2091 * Recover by retrying the request as per the discussion
2092 * on Page 181 of RFC3530.
2093 */
2094 if (status == -NFS4ERR_BAD_STATEID) {
2095 exception.retry = 1;
2096 continue;
2097 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002098 if (status == -EAGAIN) {
2099 /* We must have found a delegation */
2100 exception.retry = 1;
2101 continue;
2102 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002103 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2104 continue;
2105 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 status, &exception));
2107 } while (exception.retry);
2108 return res;
2109}
2110
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002111static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2112 struct nfs_fattr *fattr, struct iattr *sattr,
2113 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002115 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002117 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 .iap = sattr,
2119 .server = server,
2120 .bitmask = server->attr_bitmask,
2121 };
2122 struct nfs_setattrres res = {
2123 .fattr = fattr,
2124 .server = server,
2125 };
2126 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2128 .rpc_argp = &arg,
2129 .rpc_resp = &res,
2130 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002132 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002133 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Trond Myklebust0e574af2005-10-27 22:12:38 -04002135 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Trond Myklebust5d422302013-03-14 16:57:48 -04002137 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002138 struct nfs_lockowner lockowner = {
2139 .l_owner = current->files,
2140 .l_pid = current->tgid,
2141 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002142 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002143 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002144 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2145 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002146 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002147 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002148 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Bryan Schumaker7c513052011-03-24 17:12:24 +00002150 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002151 if (status == 0 && state != NULL)
2152 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002153 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002156static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2157 struct nfs_fattr *fattr, struct iattr *sattr,
2158 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002160 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002161 struct nfs4_exception exception = {
2162 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002163 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int err;
2166 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002167 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2168 switch (err) {
2169 case -NFS4ERR_OPENMODE:
2170 if (state && !(state->state & FMODE_WRITE)) {
2171 err = -EBADF;
2172 if (sattr->ia_valid & ATTR_OPEN)
2173 err = -EACCES;
2174 goto out;
2175 }
2176 }
2177 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002179out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return err;
2181}
2182
2183struct nfs4_closedata {
2184 struct inode *inode;
2185 struct nfs4_state *state;
2186 struct nfs_closeargs arg;
2187 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002188 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002189 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002190 bool roc;
2191 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192};
2193
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002194static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002195{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002196 struct nfs4_closedata *calldata = data;
2197 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002198 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002199
Fred Isamanf7e89172011-01-06 11:36:32 +00002200 if (calldata->roc)
2201 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002202 nfs4_put_open_state(calldata->state);
2203 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002204 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002205 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002206 kfree(calldata);
2207}
2208
Trond Myklebust88069f72009-12-08 08:33:16 -05002209static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2210 fmode_t fmode)
2211{
2212 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002213 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002214 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2215 case FMODE_WRITE:
2216 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2217 break;
2218 case FMODE_READ:
2219 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2220 break;
2221 case 0:
2222 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2223 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2224 clear_bit(NFS_OPEN_STATE, &state->flags);
2225 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002226 spin_unlock(&state->owner->so_lock);
2227}
2228
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002229static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002231 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct nfs_server *server = NFS_SERVER(calldata->inode);
2234
Chuck Levera3ca5652012-03-01 17:00:40 -05002235 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002236 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2237 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* hmm. we are done with the inode, and in the process of freeing
2239 * the state_owner. we keep this around to process errors
2240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 switch (task->tk_status) {
2242 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002243 if (calldata->roc)
2244 pnfs_roc_set_barrier(state->inode,
2245 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002246 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002247 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002248 nfs4_close_clear_stateid_flags(state,
2249 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
2251 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002252 case -NFS4ERR_OLD_STATEID:
2253 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002255 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002258 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2259 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002261 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002262 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002263 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002266static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002268 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002269 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002270 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002271 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002272
Chuck Levera3ca5652012-03-01 17:00:40 -05002273 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002274 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002275 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002276
Trond Myklebust88069f72009-12-08 08:33:16 -05002277 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2278 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002279 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002280 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002281 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002282 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002283 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2284 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2285 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002286 }
2287 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2289 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2290 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002291 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002292 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002293 if (!nfs4_valid_open_stateid(state))
2294 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002295 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002296
2297 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002298 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002299 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002300 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002301
Fred Isamanf7e89172011-01-06 11:36:32 +00002302 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002303 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002304 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002305 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2306 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002307 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002308 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002309 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002310
Trond Myklebust516a6af2005-10-27 22:12:41 -04002311 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002312 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002313 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002314 &calldata->arg.seq_args,
2315 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002316 task) != 0)
2317 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002318 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002319 return;
2320out_no_action:
2321 task->tk_action = NULL;
2322out_wait:
2323 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002326static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002327 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002328 .rpc_call_done = nfs4_close_done,
2329 .rpc_release = nfs4_free_closedata,
2330};
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332/*
2333 * It is possible for data to be read/written from a mem-mapped file
2334 * after the sys_close call (which hits the vfs layer as a flush).
2335 * This means that we can't safely call nfsv4 close on a file until
2336 * the inode is cleared. This in turn means that we are not good
2337 * NFSv4 citizens - we do not indicate to the server to update the file's
2338 * share state even when we are done with one of the three share
2339 * stateid's in the inode.
2340 *
2341 * NOTE: Caller must be holding the sp->so_owner semaphore!
2342 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002343int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002345 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002347 struct nfs4_state_owner *sp = state->owner;
2348 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2351 .rpc_cred = state->owner->so_cred,
2352 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002353 struct rpc_task_setup task_setup_data = {
2354 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002355 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002356 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002357 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002358 .flags = RPC_TASK_ASYNC,
2359 };
Trond Myklebust95121352005-10-18 14:20:12 -07002360 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002362 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002364 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002365 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002366 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002368 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002369 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002371 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002372 if (calldata->arg.seqid == NULL)
2373 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002374 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002375 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002376 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002377 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002378 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002379 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002380 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002381
Trond Myklebust1174dd12010-12-21 10:52:24 -05002382 msg.rpc_argp = &calldata->arg;
2383 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002384 task_setup_data.callback_data = calldata;
2385 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002386 if (IS_ERR(task))
2387 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002388 status = 0;
2389 if (wait)
2390 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002391 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002392 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002393out_free_calldata:
2394 kfree(calldata);
2395out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002396 nfs4_put_open_state(state);
2397 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002398 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
Trond Myklebust2b484292010-09-17 10:56:51 -04002401static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002402nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct nfs4_state *state;
2405
Trond Myklebust565277f2007-10-15 18:17:53 -04002406 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002407 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2408 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002409 if (IS_ERR(state))
2410 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002411 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002412 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Trond Myklebust1185a552009-12-03 15:54:02 -05002415static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002416{
2417 if (ctx->state == NULL)
2418 return;
2419 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002420 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002421 else
Al Viro643168c2011-06-22 18:20:23 -04002422 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2426{
Benny Halevy43652ad2009-04-01 09:21:54 -04002427 struct nfs4_server_caps_arg args = {
2428 .fhandle = fhandle,
2429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct nfs4_server_caps_res res = {};
2431 struct rpc_message msg = {
2432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002433 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 .rpc_resp = &res,
2435 };
2436 int status;
2437
Bryan Schumaker7c513052011-03-24 17:12:24 +00002438 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (status == 0) {
2440 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002441 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2442 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2443 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2444 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2445 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2447 server->caps |= NFS_CAP_ACLS;
2448 if (res.has_links != 0)
2449 server->caps |= NFS_CAP_HARDLINKS;
2450 if (res.has_symlinks != 0)
2451 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002452 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2453 server->caps |= NFS_CAP_FILEID;
2454 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2455 server->caps |= NFS_CAP_MODE;
2456 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2457 server->caps |= NFS_CAP_NLINK;
2458 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2459 server->caps |= NFS_CAP_OWNER;
2460 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2461 server->caps |= NFS_CAP_OWNER_GROUP;
2462 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2463 server->caps |= NFS_CAP_ATIME;
2464 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2465 server->caps |= NFS_CAP_CTIME;
2466 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2467 server->caps |= NFS_CAP_MTIME;
2468
Trond Myklebusta65318b2009-03-11 14:10:28 -04002469 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2470 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2471 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002473 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return status;
2477}
2478
Trond Myklebust55a97592006-06-09 09:34:19 -04002479int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 struct nfs4_exception exception = { };
2482 int err;
2483 do {
2484 err = nfs4_handle_exception(server,
2485 _nfs4_server_capabilities(server, fhandle),
2486 &exception);
2487 } while (exception.retry);
2488 return err;
2489}
2490
2491static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2492 struct nfs_fsinfo *info)
2493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct nfs4_lookup_root_arg args = {
2495 .bitmask = nfs4_fattr_bitmap,
2496 };
2497 struct nfs4_lookup_res res = {
2498 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002499 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .fh = fhandle,
2501 };
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2504 .rpc_argp = &args,
2505 .rpc_resp = &res,
2506 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002507
Trond Myklebust0e574af2005-10-27 22:12:38 -04002508 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002509 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
2512static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2513 struct nfs_fsinfo *info)
2514{
2515 struct nfs4_exception exception = { };
2516 int err;
2517 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002518 err = _nfs4_lookup_root(server, fhandle, info);
2519 switch (err) {
2520 case 0:
2521 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002522 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002523 default:
2524 err = nfs4_handle_exception(server, err, &exception);
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002527out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return err;
2529}
2530
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002531static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2532 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2533{
2534 struct rpc_auth *auth;
2535 int ret;
2536
2537 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002538 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002539 ret = -EIO;
2540 goto out;
2541 }
2542 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002543out:
2544 return ret;
2545}
2546
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002547static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002548 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002550 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002551 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002553 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002554 if (len < 0)
2555 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002556
2557 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002558 /* AUTH_UNIX is the default flavor if none was specified,
2559 * thus has already been tried. */
2560 if (flav_array[i] == RPC_AUTH_UNIX)
2561 continue;
2562
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002563 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002564 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002565 continue;
2566 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002567 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002568 /*
2569 * -EACCESS could mean that the user doesn't have correct permissions
2570 * to access the mount. It could also mean that we tried to mount
2571 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2572 * existing mount programs don't handle -EACCES very well so it should
2573 * be mapped to -EPERM instead.
2574 */
2575 if (status == -EACCES)
2576 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002577 return status;
2578}
2579
2580/*
2581 * get the file handle for the "/" directory on the server
2582 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002583int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2584 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002585{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002586 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002587 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002588 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2589 /*
2590 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2591 * by nfs4_map_errors() as this function exits.
2592 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002593 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (status == 0)
2595 status = nfs4_server_capabilities(server, fhandle);
2596 if (status == 0)
2597 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002598 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Bryan Schumakerbae36242012-05-10 15:07:31 -04002601static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2602 struct nfs_fsinfo *info)
2603{
2604 int error;
2605 struct nfs_fattr *fattr = info->fattr;
2606
2607 error = nfs4_server_capabilities(server, mntfh);
2608 if (error < 0) {
2609 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2610 return error;
2611 }
2612
2613 error = nfs4_proc_getattr(server, mntfh, fattr);
2614 if (error < 0) {
2615 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2616 return error;
2617 }
2618
2619 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2620 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2621 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2622
2623 return error;
2624}
2625
Manoj Naik6b97fd32006-06-09 09:34:29 -04002626/*
2627 * Get locations and (maybe) other attributes of a referral.
2628 * Note that we'll actually follow the referral later when
2629 * we detect fsid mismatch in inode revalidation
2630 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002631static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2632 const struct qstr *name, struct nfs_fattr *fattr,
2633 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002634{
2635 int status = -ENOMEM;
2636 struct page *page = NULL;
2637 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002638
2639 page = alloc_page(GFP_KERNEL);
2640 if (page == NULL)
2641 goto out;
2642 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2643 if (locations == NULL)
2644 goto out;
2645
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002646 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002647 if (status != 0)
2648 goto out;
2649 /* Make sure server returned a different fsid for the referral */
2650 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002651 dprintk("%s: server did not return a different fsid for"
2652 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002653 status = -EIO;
2654 goto out;
2655 }
Andy Adamson533eb462011-06-13 18:25:56 -04002656 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2657 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002658
Andy Adamson533eb462011-06-13 18:25:56 -04002659 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002660 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002661 memset(fhandle, 0, sizeof(struct nfs_fh));
2662out:
2663 if (page)
2664 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002665 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002666 return status;
2667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2670{
2671 struct nfs4_getattr_arg args = {
2672 .fh = fhandle,
2673 .bitmask = server->attr_bitmask,
2674 };
2675 struct nfs4_getattr_res res = {
2676 .fattr = fattr,
2677 .server = server,
2678 };
2679 struct rpc_message msg = {
2680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2681 .rpc_argp = &args,
2682 .rpc_resp = &res,
2683 };
2684
Trond Myklebust0e574af2005-10-27 22:12:38 -04002685 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002686 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
2689static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2690{
2691 struct nfs4_exception exception = { };
2692 int err;
2693 do {
2694 err = nfs4_handle_exception(server,
2695 _nfs4_proc_getattr(server, fhandle, fattr),
2696 &exception);
2697 } while (exception.retry);
2698 return err;
2699}
2700
2701/*
2702 * The file is not closed if it is opened due to the a request to change
2703 * the size of the file. The open call will not be needed once the
2704 * VFS layer lookup-intents are implemented.
2705 *
2706 * Close is called when the inode is destroyed.
2707 * If we haven't opened the file for O_WRONLY, we
2708 * need to in the size_change case to obtain a stateid.
2709 *
2710 * Got race?
2711 * Because OPEN is always done by name in nfsv4, it is
2712 * possible that we opened a different file by the same
2713 * name. We can recognize this race condition, but we
2714 * can't do anything about it besides returning an error.
2715 *
2716 * This will be fixed with VFS changes (lookup-intent).
2717 */
2718static int
2719nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2720 struct iattr *sattr)
2721{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002722 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002723 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002724 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 int status;
2726
Benny Halevy8a1636c2010-07-14 15:43:57 -04002727 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002728 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002729
Trond Myklebust0e574af2005-10-27 22:12:38 -04002730 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Andy Adamson26699402012-05-30 16:12:24 -04002732 /* Deal with open(O_TRUNC) */
2733 if (sattr->ia_valid & ATTR_OPEN)
2734 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2735
2736 /* Optimization: if the end result is no change, don't RPC */
2737 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2738 return 0;
2739
Trond Myklebustd5308382005-11-04 15:33:38 -05002740 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002741 if (sattr->ia_valid & ATTR_FILE) {
2742 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002743
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002744 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002745 if (ctx) {
2746 cred = ctx->cred;
2747 state = ctx->state;
2748 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002749 }
2750
2751 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002752 if (status == 0)
2753 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return status;
2755}
2756
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002757static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2758 const struct qstr *name, struct nfs_fh *fhandle,
2759 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002760{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002761 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002762 int status;
2763 struct nfs4_lookup_arg args = {
2764 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002765 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002766 .name = name,
2767 };
2768 struct nfs4_lookup_res res = {
2769 .server = server,
2770 .fattr = fattr,
2771 .fh = fhandle,
2772 };
2773 struct rpc_message msg = {
2774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2775 .rpc_argp = &args,
2776 .rpc_resp = &res,
2777 };
2778
2779 nfs_fattr_init(fattr);
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002782 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 dprintk("NFS reply lookup: %d\n", status);
2784 return status;
2785}
2786
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002787static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002788{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002789 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002790 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002791 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2792 fattr->nlink = 2;
2793}
2794
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002795static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2796 struct qstr *name, struct nfs_fh *fhandle,
2797 struct nfs_fattr *fattr)
2798{
2799 struct nfs4_exception exception = { };
2800 struct rpc_clnt *client = *clnt;
2801 int err;
2802 do {
2803 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2804 switch (err) {
2805 case -NFS4ERR_BADNAME:
2806 err = -ENOENT;
2807 goto out;
2808 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002809 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002810 goto out;
2811 case -NFS4ERR_WRONGSEC:
2812 err = -EPERM;
2813 if (client != *clnt)
2814 goto out;
2815
2816 client = nfs4_create_sec_client(client, dir, name);
2817 if (IS_ERR(client))
2818 return PTR_ERR(client);
2819
2820 exception.retry = 1;
2821 break;
2822 default:
2823 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2824 }
2825 } while (exception.retry);
2826
2827out:
2828 if (err == 0)
2829 *clnt = client;
2830 else if (client != *clnt)
2831 rpc_shutdown_client(client);
2832
2833 return err;
2834}
2835
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002836static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002837 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002839 int status;
2840 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002841
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002842 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2843 if (client != NFS_CLIENT(dir)) {
2844 rpc_shutdown_client(client);
2845 nfs_fixup_secinfo_attributes(fattr);
2846 }
2847 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002850struct rpc_clnt *
2851nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2852 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2853{
2854 int status;
2855 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2856
2857 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2858 if (status < 0) {
2859 rpc_shutdown_client(client);
2860 return ERR_PTR(status);
2861 }
2862 return client;
2863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2866{
Trond Myklebust76b32992007-08-10 17:45:11 -04002867 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 struct nfs4_accessargs args = {
2869 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002870 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002872 struct nfs4_accessres res = {
2873 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2877 .rpc_argp = &args,
2878 .rpc_resp = &res,
2879 .rpc_cred = entry->cred,
2880 };
2881 int mode = entry->mask;
2882 int status;
2883
2884 /*
2885 * Determine which access bits we want to ask for...
2886 */
2887 if (mode & MAY_READ)
2888 args.access |= NFS4_ACCESS_READ;
2889 if (S_ISDIR(inode->i_mode)) {
2890 if (mode & MAY_WRITE)
2891 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2892 if (mode & MAY_EXEC)
2893 args.access |= NFS4_ACCESS_LOOKUP;
2894 } else {
2895 if (mode & MAY_WRITE)
2896 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2897 if (mode & MAY_EXEC)
2898 args.access |= NFS4_ACCESS_EXECUTE;
2899 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002900
2901 res.fattr = nfs_alloc_fattr();
2902 if (res.fattr == NULL)
2903 return -ENOMEM;
2904
Bryan Schumaker7c513052011-03-24 17:12:24 +00002905 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002907 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002908 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002910 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return status;
2912}
2913
2914static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2915{
2916 struct nfs4_exception exception = { };
2917 int err;
2918 do {
2919 err = nfs4_handle_exception(NFS_SERVER(inode),
2920 _nfs4_proc_access(inode, entry),
2921 &exception);
2922 } while (exception.retry);
2923 return err;
2924}
2925
2926/*
2927 * TODO: For the time being, we don't try to get any attributes
2928 * along with any of the zero-copy operations READ, READDIR,
2929 * READLINK, WRITE.
2930 *
2931 * In the case of the first three, we want to put the GETATTR
2932 * after the read-type operation -- this is because it is hard
2933 * to predict the length of a GETATTR response in v4, and thus
2934 * align the READ data correctly. This means that the GETATTR
2935 * may end up partially falling into the page cache, and we should
2936 * shift it into the 'tail' of the xdr_buf before processing.
2937 * To do this efficiently, we need to know the total length
2938 * of data received, which doesn't seem to be available outside
2939 * of the RPC layer.
2940 *
2941 * In the case of WRITE, we also want to put the GETATTR after
2942 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002943 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 *
2945 * Both of these changes to the XDR layer would in fact be quite
2946 * minor, but I decided to leave them for a subsequent patch.
2947 */
2948static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2949 unsigned int pgbase, unsigned int pglen)
2950{
2951 struct nfs4_readlink args = {
2952 .fh = NFS_FH(inode),
2953 .pgbase = pgbase,
2954 .pglen = pglen,
2955 .pages = &page,
2956 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002957 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct rpc_message msg = {
2959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2960 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002961 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 };
2963
Bryan Schumaker7c513052011-03-24 17:12:24 +00002964 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 -07002965}
2966
2967static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2968 unsigned int pgbase, unsigned int pglen)
2969{
2970 struct nfs4_exception exception = { };
2971 int err;
2972 do {
2973 err = nfs4_handle_exception(NFS_SERVER(inode),
2974 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2975 &exception);
2976 } while (exception.retry);
2977 return err;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002981 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983static int
2984nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002985 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002987 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 int status = 0;
2990
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002991 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2992 if (IS_ERR(ctx))
2993 return PTR_ERR(ctx);
2994
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002995 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002996 state = nfs4_do_open(dir, dentry, ctx->mode,
2997 flags, sattr, ctx->cred,
2998 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002999 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (IS_ERR(state)) {
3001 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003004 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003005 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003006 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003008 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return status;
3010}
3011
3012static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3013{
Trond Myklebust16e42952005-10-27 22:12:44 -04003014 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003015 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003017 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003019 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003020 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003021 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3024 .rpc_argp = &args,
3025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003027 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003028
Bryan Schumaker7c513052011-03-24 17:12:24 +00003029 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003030 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003031 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return status;
3033}
3034
3035static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3036{
3037 struct nfs4_exception exception = { };
3038 int err;
3039 do {
3040 err = nfs4_handle_exception(NFS_SERVER(dir),
3041 _nfs4_proc_remove(dir, name),
3042 &exception);
3043 } while (exception.retry);
3044 return err;
3045}
3046
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003047static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003049 struct nfs_server *server = NFS_SERVER(dir);
3050 struct nfs_removeargs *args = msg->rpc_argp;
3051 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003053 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003055 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003058static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3059{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003060 nfs4_setup_sequence(NFS_SERVER(data->dir),
3061 &data->args.seq_args,
3062 &data->res.seq_res,
3063 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003066static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003068 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3069
Trond Myklebust14516c32010-07-31 14:29:06 -04003070 if (!nfs4_sequence_done(task, &res->seq_res))
3071 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003072 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003073 return 0;
3074 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003075 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Jeff Laytond3d41522010-09-17 17:31:57 -04003078static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3079{
3080 struct nfs_server *server = NFS_SERVER(dir);
3081 struct nfs_renameargs *arg = msg->rpc_argp;
3082 struct nfs_renameres *res = msg->rpc_resp;
3083
3084 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003085 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003086 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003087}
3088
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003089static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3090{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003091 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3092 &data->args.seq_args,
3093 &data->res.seq_res,
3094 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003095}
3096
3097static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3098 struct inode *new_dir)
3099{
3100 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3101
3102 if (!nfs4_sequence_done(task, &res->seq_res))
3103 return 0;
3104 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3105 return 0;
3106
3107 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003108 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003109 return 1;
3110}
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3113 struct inode *new_dir, struct qstr *new_name)
3114{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003115 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003116 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 .old_dir = NFS_FH(old_dir),
3118 .new_dir = NFS_FH(new_dir),
3119 .old_name = old_name,
3120 .new_name = new_name,
3121 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003122 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003123 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 struct rpc_message msg = {
3126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3127 .rpc_argp = &arg,
3128 .rpc_resp = &res,
3129 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003130 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Bryan Schumaker7c513052011-03-24 17:12:24 +00003132 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (!status) {
3134 update_changeattr(old_dir, &res.old_cinfo);
3135 update_changeattr(new_dir, &res.new_cinfo);
3136 }
3137 return status;
3138}
3139
3140static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3141 struct inode *new_dir, struct qstr *new_name)
3142{
3143 struct nfs4_exception exception = { };
3144 int err;
3145 do {
3146 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3147 _nfs4_proc_rename(old_dir, old_name,
3148 new_dir, new_name),
3149 &exception);
3150 } while (exception.retry);
3151 return err;
3152}
3153
3154static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3155{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003156 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct nfs4_link_arg arg = {
3158 .fh = NFS_FH(inode),
3159 .dir_fh = NFS_FH(dir),
3160 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003161 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003163 struct nfs4_link_res res = {
3164 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 struct rpc_message msg = {
3167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3168 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003169 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003171 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Trond Myklebust136f2622010-04-16 16:22:49 -04003173 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003174 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003175 goto out;
3176
Bryan Schumaker7c513052011-03-24 17:12:24 +00003177 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003178 if (!status) {
3179 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003180 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003181 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003182out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003183 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return status;
3185}
3186
3187static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3188{
3189 struct nfs4_exception exception = { };
3190 int err;
3191 do {
3192 err = nfs4_handle_exception(NFS_SERVER(inode),
3193 _nfs4_proc_link(inode, dir, name),
3194 &exception);
3195 } while (exception.retry);
3196 return err;
3197}
3198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199struct nfs4_createdata {
3200 struct rpc_message msg;
3201 struct nfs4_create_arg arg;
3202 struct nfs4_create_res res;
3203 struct nfs_fh fh;
3204 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003205};
3206
3207static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3208 struct qstr *name, struct iattr *sattr, u32 ftype)
3209{
3210 struct nfs4_createdata *data;
3211
3212 data = kzalloc(sizeof(*data), GFP_KERNEL);
3213 if (data != NULL) {
3214 struct nfs_server *server = NFS_SERVER(dir);
3215
3216 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3217 data->msg.rpc_argp = &data->arg;
3218 data->msg.rpc_resp = &data->res;
3219 data->arg.dir_fh = NFS_FH(dir);
3220 data->arg.server = server;
3221 data->arg.name = name;
3222 data->arg.attrs = sattr;
3223 data->arg.ftype = ftype;
3224 data->arg.bitmask = server->attr_bitmask;
3225 data->res.server = server;
3226 data->res.fh = &data->fh;
3227 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003228 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 }
3230 return data;
3231}
3232
3233static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3234{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003235 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003236 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237 if (status == 0) {
3238 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3240 }
3241 return status;
3242}
3243
3244static void nfs4_free_createdata(struct nfs4_createdata *data)
3245{
3246 kfree(data);
3247}
3248
Chuck Lever4f390c12006-08-22 20:06:22 -04003249static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003250 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003252 struct nfs4_createdata *data;
3253 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Chuck Lever94a6d752006-08-22 20:06:23 -04003255 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003256 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003257
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003258 status = -ENOMEM;
3259 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3260 if (data == NULL)
3261 goto out;
3262
3263 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3264 data->arg.u.symlink.pages = &page;
3265 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 status = nfs4_do_create(dir, dentry, data);
3268
3269 nfs4_free_createdata(data);
3270out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return status;
3272}
3273
Chuck Lever4f390c12006-08-22 20:06:22 -04003274static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003275 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 struct nfs4_exception exception = { };
3278 int err;
3279 do {
3280 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003281 _nfs4_proc_symlink(dir, dentry, page,
3282 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 &exception);
3284 } while (exception.retry);
3285 return err;
3286}
3287
3288static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3289 struct iattr *sattr)
3290{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003291 struct nfs4_createdata *data;
3292 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003294 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3295 if (data == NULL)
3296 goto out;
3297
3298 status = nfs4_do_create(dir, dentry, data);
3299
3300 nfs4_free_createdata(data);
3301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return status;
3303}
3304
3305static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3306 struct iattr *sattr)
3307{
3308 struct nfs4_exception exception = { };
3309 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003310
3311 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 do {
3313 err = nfs4_handle_exception(NFS_SERVER(dir),
3314 _nfs4_proc_mkdir(dir, dentry, sattr),
3315 &exception);
3316 } while (exception.retry);
3317 return err;
3318}
3319
3320static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003321 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 struct inode *dir = dentry->d_inode;
3324 struct nfs4_readdir_arg args = {
3325 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003326 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 .pgbase = 0,
3328 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003329 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003330 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 };
3332 struct nfs4_readdir_res res;
3333 struct rpc_message msg = {
3334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3335 .rpc_argp = &args,
3336 .rpc_resp = &res,
3337 .rpc_cred = cred,
3338 };
3339 int status;
3340
Harvey Harrison3110ff82008-05-02 13:42:44 -07003341 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003342 dentry->d_parent->d_name.name,
3343 dentry->d_name.name,
3344 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003345 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003347 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 -05003348 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003349 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003350 status += args.pgbase;
3351 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003352
3353 nfs_invalidate_atime(dir);
3354
Harvey Harrison3110ff82008-05-02 13:42:44 -07003355 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return status;
3357}
3358
3359static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003360 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 struct nfs4_exception exception = { };
3363 int err;
3364 do {
3365 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3366 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003367 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 &exception);
3369 } while (exception.retry);
3370 return err;
3371}
3372
3373static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3374 struct iattr *sattr, dev_t rdev)
3375{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003376 struct nfs4_createdata *data;
3377 int mode = sattr->ia_mode;
3378 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003380 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3381 if (data == NULL)
3382 goto out;
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003385 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003387 data->arg.ftype = NF4BLK;
3388 data->arg.u.device.specdata1 = MAJOR(rdev);
3389 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003392 data->arg.ftype = NF4CHR;
3393 data->arg.u.device.specdata1 = MAJOR(rdev);
3394 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003395 } else if (!S_ISSOCK(mode)) {
3396 status = -EINVAL;
3397 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003400 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003401out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003402 nfs4_free_createdata(data);
3403out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return status;
3405}
3406
3407static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3408 struct iattr *sattr, dev_t rdev)
3409{
3410 struct nfs4_exception exception = { };
3411 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003412
3413 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 do {
3415 err = nfs4_handle_exception(NFS_SERVER(dir),
3416 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3417 &exception);
3418 } while (exception.retry);
3419 return err;
3420}
3421
3422static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3423 struct nfs_fsstat *fsstat)
3424{
3425 struct nfs4_statfs_arg args = {
3426 .fh = fhandle,
3427 .bitmask = server->attr_bitmask,
3428 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003429 struct nfs4_statfs_res res = {
3430 .fsstat = fsstat,
3431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3434 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003435 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 };
3437
Trond Myklebust0e574af2005-10-27 22:12:38 -04003438 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003439 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
3442static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3443{
3444 struct nfs4_exception exception = { };
3445 int err;
3446 do {
3447 err = nfs4_handle_exception(server,
3448 _nfs4_proc_statfs(server, fhandle, fsstat),
3449 &exception);
3450 } while (exception.retry);
3451 return err;
3452}
3453
3454static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3455 struct nfs_fsinfo *fsinfo)
3456{
3457 struct nfs4_fsinfo_arg args = {
3458 .fh = fhandle,
3459 .bitmask = server->attr_bitmask,
3460 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003461 struct nfs4_fsinfo_res res = {
3462 .fsinfo = fsinfo,
3463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 struct rpc_message msg = {
3465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3466 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003467 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 };
3469
Bryan Schumaker7c513052011-03-24 17:12:24 +00003470 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
3473static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3474{
3475 struct nfs4_exception exception = { };
3476 int err;
3477
3478 do {
3479 err = nfs4_handle_exception(server,
3480 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3481 &exception);
3482 } while (exception.retry);
3483 return err;
3484}
3485
3486static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3487{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003488 int error;
3489
Trond Myklebust0e574af2005-10-27 22:12:38 -04003490 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003491 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003492 if (error == 0) {
3493 /* block layout checks this! */
3494 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003495 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003496 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003497
3498 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
3501static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3502 struct nfs_pathconf *pathconf)
3503{
3504 struct nfs4_pathconf_arg args = {
3505 .fh = fhandle,
3506 .bitmask = server->attr_bitmask,
3507 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003508 struct nfs4_pathconf_res res = {
3509 .pathconf = pathconf,
3510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 struct rpc_message msg = {
3512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3513 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003514 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 };
3516
3517 /* None of the pathconf attributes are mandatory to implement */
3518 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3519 memset(pathconf, 0, sizeof(*pathconf));
3520 return 0;
3521 }
3522
Trond Myklebust0e574af2005-10-27 22:12:38 -04003523 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003524 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
3527static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3528 struct nfs_pathconf *pathconf)
3529{
3530 struct nfs4_exception exception = { };
3531 int err;
3532
3533 do {
3534 err = nfs4_handle_exception(server,
3535 _nfs4_proc_pathconf(server, fhandle, pathconf),
3536 &exception);
3537 } while (exception.retry);
3538 return err;
3539}
3540
Trond Myklebust5521abf2013-03-16 20:54:34 -04003541int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003542 const struct nfs_open_context *ctx,
3543 const struct nfs_lock_context *l_ctx,
3544 fmode_t fmode)
3545{
3546 const struct nfs_lockowner *lockowner = NULL;
3547
3548 if (l_ctx != NULL)
3549 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003550 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003551}
3552EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3553
Trond Myklebust5521abf2013-03-16 20:54:34 -04003554static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3555 const struct nfs_open_context *ctx,
3556 const struct nfs_lock_context *l_ctx,
3557 fmode_t fmode)
3558{
3559 nfs4_stateid current_stateid;
3560
3561 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3562 return false;
3563 return nfs4_stateid_match(stateid, &current_stateid);
3564}
3565
3566static bool nfs4_error_stateid_expired(int err)
3567{
3568 switch (err) {
3569 case -NFS4ERR_DELEG_REVOKED:
3570 case -NFS4ERR_ADMIN_REVOKED:
3571 case -NFS4ERR_BAD_STATEID:
3572 case -NFS4ERR_STALE_STATEID:
3573 case -NFS4ERR_OLD_STATEID:
3574 case -NFS4ERR_OPENMODE:
3575 case -NFS4ERR_EXPIRED:
3576 return true;
3577 }
3578 return false;
3579}
3580
Benny Halevyd20581a2011-05-22 19:52:03 +03003581void __nfs4_read_done_cb(struct nfs_read_data *data)
3582{
Fred Isamancd841602012-04-20 14:47:44 -04003583 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003584}
3585
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003586static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Fred Isamancd841602012-04-20 14:47:44 -04003588 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003590 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003591 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003592 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003594
Benny Halevyd20581a2011-05-22 19:52:03 +03003595 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003597 renew_lease(server, data->timestamp);
3598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
Trond Myklebust5521abf2013-03-16 20:54:34 -04003601static bool nfs4_read_stateid_changed(struct rpc_task *task,
3602 struct nfs_readargs *args)
3603{
3604
3605 if (!nfs4_error_stateid_expired(task->tk_status) ||
3606 nfs4_stateid_is_current(&args->stateid,
3607 args->context,
3608 args->lock_context,
3609 FMODE_READ))
3610 return false;
3611 rpc_restart_call_prepare(task);
3612 return true;
3613}
3614
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003615static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3616{
3617
3618 dprintk("--> %s\n", __func__);
3619
3620 if (!nfs4_sequence_done(task, &data->res.seq_res))
3621 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003622 if (nfs4_read_stateid_changed(task, &data->args))
3623 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003624 return data->read_done_cb ? data->read_done_cb(task, data) :
3625 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003626}
3627
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003628static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003631 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003632 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003633 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003636static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3637{
Trond Myklebust9b206142013-03-17 15:52:00 -04003638 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003639 &data->args.seq_args,
3640 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003641 task))
3642 return;
3643 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3644 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
Fred Isamanb029bc92011-03-03 15:13:42 +00003647static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Fred Isamancd841602012-04-20 14:47:44 -04003649 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003651 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003652 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003653 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003655 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003657 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003658 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Trond Myklebust5521abf2013-03-16 20:54:34 -04003662static bool nfs4_write_stateid_changed(struct rpc_task *task,
3663 struct nfs_writeargs *args)
3664{
3665
3666 if (!nfs4_error_stateid_expired(task->tk_status) ||
3667 nfs4_stateid_is_current(&args->stateid,
3668 args->context,
3669 args->lock_context,
3670 FMODE_WRITE))
3671 return false;
3672 rpc_restart_call_prepare(task);
3673 return true;
3674}
3675
Fred Isamanb029bc92011-03-03 15:13:42 +00003676static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3677{
3678 if (!nfs4_sequence_done(task, &data->res.seq_res))
3679 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003680 if (nfs4_write_stateid_changed(task, &data->args))
3681 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003682 return data->write_done_cb ? data->write_done_cb(task, data) :
3683 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003684}
3685
Trond Myklebust5a37f852012-04-28 14:55:16 -04003686static
3687bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003688{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003689 const struct nfs_pgio_header *hdr = data->header;
3690
3691 /* Don't request attributes for pNFS or O_DIRECT writes */
3692 if (data->ds_clp != NULL || hdr->dreq != NULL)
3693 return false;
3694 /* Otherwise, request attributes if and only if we don't hold
3695 * a delegation
3696 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003697 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003698}
Fred Isamana69aef12011-03-03 15:13:47 +00003699
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003700static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
Fred Isamancd841602012-04-20 14:47:44 -04003702 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003703
Trond Myklebust5a37f852012-04-28 14:55:16 -04003704 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003705 data->args.bitmask = NULL;
3706 data->res.fattr = NULL;
3707 } else
3708 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003709
Fred Isamanb029bc92011-03-03 15:13:42 +00003710 if (!data->write_done_cb)
3711 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003712 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 data->timestamp = jiffies;
3714
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003715 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003716 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003719static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3720{
Trond Myklebust9b206142013-03-17 15:52:00 -04003721 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003722 &data->args.seq_args,
3723 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003724 task))
3725 return;
3726 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3727 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
Fred Isaman0b7c0152012-04-20 14:47:39 -04003730static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3731{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003732 nfs4_setup_sequence(NFS_SERVER(data->inode),
3733 &data->args.seq_args,
3734 &data->res.seq_res,
3735 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Fred Isaman0b7c0152012-04-20 14:47:39 -04003738static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003741
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003742 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003743 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003744 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Fred Isaman0b7c0152012-04-20 14:47:39 -04003749static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003750{
3751 if (!nfs4_sequence_done(task, &data->res.seq_res))
3752 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003753 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003754}
3755
Fred Isaman0b7c0152012-04-20 14:47:39 -04003756static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003758 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003759
Fred Isaman0b7c0152012-04-20 14:47:39 -04003760 if (data->commit_done_cb == NULL)
3761 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003762 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003763 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003764 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003767struct nfs4_renewdata {
3768 struct nfs_client *client;
3769 unsigned long timestamp;
3770};
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772/*
3773 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3774 * standalone procedure for queueing an asynchronous RENEW.
3775 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003776static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003777{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003778 struct nfs4_renewdata *data = calldata;
3779 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003780
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003781 if (atomic_read(&clp->cl_count) > 1)
3782 nfs4_schedule_state_renewal(clp);
3783 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003784 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003785}
3786
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003787static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003789 struct nfs4_renewdata *data = calldata;
3790 struct nfs_client *clp = data->client;
3791 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003794 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003795 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3796 return;
3797 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003798 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003799 return;
3800 }
3801 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003803 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003806static const struct rpc_call_ops nfs4_renew_ops = {
3807 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003808 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003809};
3810
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003811static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct rpc_message msg = {
3814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3815 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003816 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003818 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003820 if (renew_flags == 0)
3821 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003822 if (!atomic_inc_not_zero(&clp->cl_count))
3823 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003824 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003825 if (data == NULL)
3826 return -ENOMEM;
3827 data->client = clp;
3828 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003829 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003830 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003833static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3837 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 };
3840 unsigned long now = jiffies;
3841 int status;
3842
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003843 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 if (status < 0)
3845 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003846 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return 0;
3848}
3849
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003850static inline int nfs4_server_supports_acls(struct nfs_server *server)
3851{
3852 return (server->caps & NFS_CAP_ACLS)
3853 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3854 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3855}
3856
Trond Myklebust21f498c2012-08-24 10:59:25 -04003857/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3858 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003859 * the stack.
3860 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003861#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003862
Neil Hormane9e3d722011-03-04 19:26:03 -05003863static int buf_to_pages_noslab(const void *buf, size_t buflen,
3864 struct page **pages, unsigned int *pgbase)
3865{
3866 struct page *newpage, **spages;
3867 int rc = 0;
3868 size_t len;
3869 spages = pages;
3870
3871 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003872 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003873 newpage = alloc_page(GFP_KERNEL);
3874
3875 if (newpage == NULL)
3876 goto unwind;
3877 memcpy(page_address(newpage), buf, len);
3878 buf += len;
3879 buflen -= len;
3880 *pages++ = newpage;
3881 rc++;
3882 } while (buflen != 0);
3883
3884 return rc;
3885
3886unwind:
3887 for(; rc > 0; rc--)
3888 __free_page(spages[rc-1]);
3889 return -ENOMEM;
3890}
3891
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003892struct nfs4_cached_acl {
3893 int cached;
3894 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003895 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003896};
3897
3898static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003899{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003900 struct nfs_inode *nfsi = NFS_I(inode);
3901
3902 spin_lock(&inode->i_lock);
3903 kfree(nfsi->nfs4_acl);
3904 nfsi->nfs4_acl = acl;
3905 spin_unlock(&inode->i_lock);
3906}
3907
3908static void nfs4_zap_acl_attr(struct inode *inode)
3909{
3910 nfs4_set_cached_acl(inode, NULL);
3911}
3912
3913static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3914{
3915 struct nfs_inode *nfsi = NFS_I(inode);
3916 struct nfs4_cached_acl *acl;
3917 int ret = -ENOENT;
3918
3919 spin_lock(&inode->i_lock);
3920 acl = nfsi->nfs4_acl;
3921 if (acl == NULL)
3922 goto out;
3923 if (buf == NULL) /* user is just asking for length */
3924 goto out_len;
3925 if (acl->cached == 0)
3926 goto out;
3927 ret = -ERANGE; /* see getxattr(2) man page */
3928 if (acl->len > buflen)
3929 goto out;
3930 memcpy(buf, acl->data, acl->len);
3931out_len:
3932 ret = acl->len;
3933out:
3934 spin_unlock(&inode->i_lock);
3935 return ret;
3936}
3937
Sachin Prabhu5794d212012-04-17 14:36:40 +01003938static 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 +00003939{
3940 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003941 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003942
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003943 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003944 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003945 if (acl == NULL)
3946 goto out;
3947 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003948 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003949 } else {
3950 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3951 if (acl == NULL)
3952 goto out;
3953 acl->cached = 0;
3954 }
3955 acl->len = acl_len;
3956out:
3957 nfs4_set_cached_acl(inode, acl);
3958}
3959
Andy Adamsonbf118a32011-12-07 11:55:27 -05003960/*
3961 * The getxattr API returns the required buffer length when called with a
3962 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3963 * the required buf. On a NULL buf, we send a page of data to the server
3964 * guessing that the ACL request can be serviced by a page. If so, we cache
3965 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3966 * the cache. If not so, we throw away the page, and cache the required
3967 * length. The next getxattr call will then produce another round trip to
3968 * the server, this time with the input buf of the required size.
3969 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003970static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003971{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003972 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003973 struct nfs_getaclargs args = {
3974 .fh = NFS_FH(inode),
3975 .acl_pages = pages,
3976 .acl_len = buflen,
3977 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003978 struct nfs_getaclres res = {
3979 .acl_len = buflen,
3980 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3983 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003984 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003985 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003986 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3987 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003988
Andy Adamsonbf118a32011-12-07 11:55:27 -05003989 /* As long as we're doing a round trip to the server anyway,
3990 * let's be prepared for a page of acl data. */
3991 if (npages == 0)
3992 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003993 if (npages > ARRAY_SIZE(pages))
3994 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003995
Andy Adamsonbf118a32011-12-07 11:55:27 -05003996 for (i = 0; i < npages; i++) {
3997 pages[i] = alloc_page(GFP_KERNEL);
3998 if (!pages[i])
3999 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004000 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004001
4002 /* for decoding across pages */
4003 res.acl_scratch = alloc_page(GFP_KERNEL);
4004 if (!res.acl_scratch)
4005 goto out_free;
4006
Andy Adamsonbf118a32011-12-07 11:55:27 -05004007 args.acl_len = npages * PAGE_SIZE;
4008 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004009
Peng Taode040be2012-01-10 22:42:47 +08004010 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004011 __func__, buf, buflen, npages, args.acl_len);
4012 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4013 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004014 if (ret)
4015 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004016
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004017 /* Handle the case where the passed-in buffer is too short */
4018 if (res.acl_flags & NFS4_ACL_TRUNC) {
4019 /* Did the user only issue a request for the acl length? */
4020 if (buf == NULL)
4021 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004022 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004023 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004024 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004025 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004026 if (buf) {
4027 if (res.acl_len > buflen) {
4028 ret = -ERANGE;
4029 goto out_free;
4030 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004031 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004032 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004033out_ok:
4034 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004035out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004036 for (i = 0; i < npages; i++)
4037 if (pages[i])
4038 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004039 if (res.acl_scratch)
4040 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004041 return ret;
4042}
4043
Trond Myklebust16b42892006-08-24 12:27:15 -04004044static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4045{
4046 struct nfs4_exception exception = { };
4047 ssize_t ret;
4048 do {
4049 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4050 if (ret >= 0)
4051 break;
4052 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4053 } while (exception.retry);
4054 return ret;
4055}
4056
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004057static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4058{
4059 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004060 int ret;
4061
4062 if (!nfs4_server_supports_acls(server))
4063 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004064 ret = nfs_revalidate_inode(server, inode);
4065 if (ret < 0)
4066 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004067 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4068 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004069 ret = nfs4_read_cached_acl(inode, buf, buflen);
4070 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004071 /* -ENOENT is returned if there is no ACL or if there is an ACL
4072 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004073 return ret;
4074 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004075}
4076
Trond Myklebust16b42892006-08-24 12:27:15 -04004077static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004078{
4079 struct nfs_server *server = NFS_SERVER(inode);
4080 struct page *pages[NFS4ACL_MAXPAGES];
4081 struct nfs_setaclargs arg = {
4082 .fh = NFS_FH(inode),
4083 .acl_pages = pages,
4084 .acl_len = buflen,
4085 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004086 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004087 struct rpc_message msg = {
4088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4089 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004090 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004091 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004092 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004093 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004094
4095 if (!nfs4_server_supports_acls(server))
4096 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004097 if (npages > ARRAY_SIZE(pages))
4098 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004099 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4100 if (i < 0)
4101 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004102 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004103 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004104
4105 /*
4106 * Free each page after tx, so the only ref left is
4107 * held by the network stack
4108 */
4109 for (; i > 0; i--)
4110 put_page(pages[i-1]);
4111
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004112 /*
4113 * Acl update can result in inode attribute update.
4114 * so mark the attribute cache invalid.
4115 */
4116 spin_lock(&inode->i_lock);
4117 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4118 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004119 nfs_access_zap_cache(inode);
4120 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004121 return ret;
4122}
4123
Trond Myklebust16b42892006-08-24 12:27:15 -04004124static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4125{
4126 struct nfs4_exception exception = { };
4127 int err;
4128 do {
4129 err = nfs4_handle_exception(NFS_SERVER(inode),
4130 __nfs4_proc_set_acl(inode, buf, buflen),
4131 &exception);
4132 } while (exception.retry);
4133 return err;
4134}
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004137nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004139 struct nfs_client *clp = server->nfs_client;
4140
4141 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return 0;
4143 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004144 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004145 case -NFS4ERR_ADMIN_REVOKED:
4146 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004147 if (state == NULL)
4148 break;
4149 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004150 case -NFS4ERR_OPENMODE:
4151 if (state == NULL)
4152 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004153 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4154 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004155 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004156 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004157 if (state != NULL) {
4158 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4159 goto stateid_invalid;
4160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004162 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004163 nfs4_schedule_lease_recovery(clp);
4164 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004165#if defined(CONFIG_NFS_V4_1)
4166 case -NFS4ERR_BADSESSION:
4167 case -NFS4ERR_BADSLOT:
4168 case -NFS4ERR_BAD_HIGH_SLOT:
4169 case -NFS4ERR_DEADSESSION:
4170 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4171 case -NFS4ERR_SEQ_FALSE_RETRY:
4172 case -NFS4ERR_SEQ_MISORDERED:
4173 dprintk("%s ERROR %d, Reset session\n", __func__,
4174 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004175 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004176 task->tk_status = 0;
4177 return -EAGAIN;
4178#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004180 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004181 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4183 task->tk_status = 0;
4184 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004185 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 case -NFS4ERR_OLD_STATEID:
4187 task->tk_status = 0;
4188 return -EAGAIN;
4189 }
4190 task->tk_status = nfs4_map_errors(task->tk_status);
4191 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004192stateid_invalid:
4193 task->tk_status = -EIO;
4194 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004195wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004196 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004197 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4198 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4199 task->tk_status = 0;
4200 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201}
4202
Chuck Leverf0920752012-05-21 22:45:41 -04004203static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4204 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004205{
4206 __be32 verf[2];
4207
Chuck Lever2c820d92012-05-21 22:45:33 -04004208 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4209 /* An impossible timestamp guarantees this value
4210 * will never match a generated boot time. */
4211 verf[0] = 0;
4212 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4213 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004214 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4215 verf[0] = (__be32)nn->boot_time.tv_sec;
4216 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004217 }
Chuck Levercd937102012-03-02 17:14:31 -05004218 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4219}
4220
Chuck Levere984a552012-09-14 17:24:21 -04004221static unsigned int
4222nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4223 char *buf, size_t len)
4224{
4225 unsigned int result;
4226
4227 rcu_read_lock();
4228 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4229 clp->cl_ipaddr,
4230 rpc_peeraddr2str(clp->cl_rpcclient,
4231 RPC_DISPLAY_ADDR),
4232 rpc_peeraddr2str(clp->cl_rpcclient,
4233 RPC_DISPLAY_PROTO));
4234 rcu_read_unlock();
4235 return result;
4236}
4237
4238static unsigned int
4239nfs4_init_uniform_client_string(const struct nfs_client *clp,
4240 char *buf, size_t len)
4241{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004242 char *nodename = clp->cl_rpcclient->cl_nodename;
4243
4244 if (nfs4_client_id_uniquifier[0] != '\0')
4245 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004246 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4247 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004248 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004249}
4250
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004251/**
4252 * nfs4_proc_setclientid - Negotiate client ID
4253 * @clp: state data structure
4254 * @program: RPC program for NFSv4 callback service
4255 * @port: IP port number for NFS4 callback service
4256 * @cred: RPC credential to use for this call
4257 * @res: where to place the result
4258 *
4259 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4260 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004261int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4262 unsigned short port, struct rpc_cred *cred,
4263 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
4265 nfs4_verifier sc_verifier;
4266 struct nfs4_setclientid setclientid = {
4267 .sc_verifier = &sc_verifier,
4268 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004269 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 };
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4273 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004274 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004275 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004277 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Chuck Leverde734832012-07-11 16:30:50 -04004279 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004280 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004281 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4282 setclientid.sc_name_len =
4283 nfs4_init_uniform_client_string(clp,
4284 setclientid.sc_name,
4285 sizeof(setclientid.sc_name));
4286 else
4287 setclientid.sc_name_len =
4288 nfs4_init_nonuniform_client_string(clp,
4289 setclientid.sc_name,
4290 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004291 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004292 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004293 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004294 sizeof(setclientid.sc_netid),
4295 rpc_peeraddr2str(clp->cl_rpcclient,
4296 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004297 rcu_read_unlock();
4298 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004299 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 clp->cl_ipaddr, port >> 8, port & 255);
4301
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004302 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4304 setclientid.sc_name_len, setclientid.sc_name);
4305 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4306 dprintk("NFS reply setclientid: %d\n", status);
4307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004310/**
4311 * nfs4_proc_setclientid_confirm - Confirm client ID
4312 * @clp: state data structure
4313 * @res: result of a previous SETCLIENTID
4314 * @cred: RPC credential to use for this call
4315 *
4316 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4317 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004318int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004319 struct nfs4_setclientid_res *arg,
4320 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
4322 struct nfs_fsinfo fsinfo;
4323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004325 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004327 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
4329 unsigned long now;
4330 int status;
4331
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004332 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4333 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4334 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004336 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (status == 0) {
4338 spin_lock(&clp->cl_lock);
4339 clp->cl_lease_time = fsinfo.lease_time * HZ;
4340 clp->cl_last_renewal = now;
4341 spin_unlock(&clp->cl_lock);
4342 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004343 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return status;
4345}
4346
Trond Myklebustfe650402006-01-03 09:55:18 +01004347struct nfs4_delegreturndata {
4348 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004349 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004350 struct nfs_fh fh;
4351 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004352 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004353 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004354 int rpc_status;
4355};
4356
Trond Myklebustfe650402006-01-03 09:55:18 +01004357static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4358{
4359 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004360
Trond Myklebust14516c32010-07-31 14:29:06 -04004361 if (!nfs4_sequence_done(task, &data->res.seq_res))
4362 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004363
Ricardo Labiaga79708862009-12-07 09:23:21 -05004364 switch (task->tk_status) {
4365 case -NFS4ERR_STALE_STATEID:
4366 case -NFS4ERR_EXPIRED:
4367 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004368 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004369 break;
4370 default:
4371 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4372 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004373 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004374 return;
4375 }
4376 }
4377 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004378}
4379
4380static void nfs4_delegreturn_release(void *calldata)
4381{
Trond Myklebustfe650402006-01-03 09:55:18 +01004382 kfree(calldata);
4383}
4384
Andy Adamson938e1012009-04-01 09:22:28 -04004385#if defined(CONFIG_NFS_V4_1)
4386static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4387{
4388 struct nfs4_delegreturndata *d_data;
4389
4390 d_data = (struct nfs4_delegreturndata *)data;
4391
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004392 nfs4_setup_sequence(d_data->res.server,
4393 &d_data->args.seq_args,
4394 &d_data->res.seq_res,
4395 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004396}
4397#endif /* CONFIG_NFS_V4_1 */
4398
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004399static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004400#if defined(CONFIG_NFS_V4_1)
4401 .rpc_call_prepare = nfs4_delegreturn_prepare,
4402#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004403 .rpc_call_done = nfs4_delegreturn_done,
4404 .rpc_release = nfs4_delegreturn_release,
4405};
4406
Trond Myklebuste6f81072008-01-24 18:14:34 -05004407static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004408{
4409 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004410 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004411 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004412 struct rpc_message msg = {
4413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4414 .rpc_cred = cred,
4415 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004416 struct rpc_task_setup task_setup_data = {
4417 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004418 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004419 .callback_ops = &nfs4_delegreturn_ops,
4420 .flags = RPC_TASK_ASYNC,
4421 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004422 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004423
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004424 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004425 if (data == NULL)
4426 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004427 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004428 data->args.fhandle = &data->fh;
4429 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004430 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004431 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004432 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004433 data->res.fattr = &data->fattr;
4434 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004435 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004436 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004437 data->rpc_status = 0;
4438
Trond Myklebustc970aa82007-07-14 15:39:59 -04004439 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004440 msg.rpc_argp = &data->args;
4441 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004442 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004443 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004444 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004445 if (!issync)
4446 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004447 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004448 if (status != 0)
4449 goto out;
4450 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004451 if (status == 0)
4452 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4453 else
4454 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004455out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004456 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
Trond Myklebuste6f81072008-01-24 18:14:34 -05004460int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
4462 struct nfs_server *server = NFS_SERVER(inode);
4463 struct nfs4_exception exception = { };
4464 int err;
4465 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004466 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 switch (err) {
4468 case -NFS4ERR_STALE_STATEID:
4469 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 case 0:
4471 return 0;
4472 }
4473 err = nfs4_handle_exception(server, err, &exception);
4474 } while (exception.retry);
4475 return err;
4476}
4477
4478#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4479#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4480
4481/*
4482 * sleep, with exponential backoff, and retry the LOCK operation.
4483 */
4484static unsigned long
4485nfs4_set_lock_task_retry(unsigned long timeout)
4486{
Jeff Laytond3103102011-12-01 22:44:39 +01004487 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 timeout <<= 1;
4489 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4490 return NFS4_LOCK_MAXTIMEOUT;
4491 return timeout;
4492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4495{
4496 struct inode *inode = state->inode;
4497 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004498 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004499 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004501 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004503 struct nfs_lockt_res res = {
4504 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 };
4506 struct rpc_message msg = {
4507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4508 .rpc_argp = &arg,
4509 .rpc_resp = &res,
4510 .rpc_cred = state->owner->so_cred,
4511 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 struct nfs4_lock_state *lsp;
4513 int status;
4514
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004515 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004516 status = nfs4_set_lock_state(state, request);
4517 if (status != 0)
4518 goto out;
4519 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004520 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004521 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004522 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004523 switch (status) {
4524 case 0:
4525 request->fl_type = F_UNLCK;
4526 break;
4527 case -NFS4ERR_DENIED:
4528 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004530 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return status;
4533}
4534
4535static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4536{
4537 struct nfs4_exception exception = { };
4538 int err;
4539
4540 do {
4541 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4542 _nfs4_proc_getlk(state, cmd, request),
4543 &exception);
4544 } while (exception.retry);
4545 return err;
4546}
4547
4548static int do_vfs_lock(struct file *file, struct file_lock *fl)
4549{
4550 int res = 0;
4551 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4552 case FL_POSIX:
4553 res = posix_lock_file_wait(file, fl);
4554 break;
4555 case FL_FLOCK:
4556 res = flock_lock_file_wait(file, fl);
4557 break;
4558 default:
4559 BUG();
4560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return res;
4562}
4563
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004564struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004565 struct nfs_locku_args arg;
4566 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004569 struct file_lock fl;
4570 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004571 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004572};
4573
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004574static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4575 struct nfs_open_context *ctx,
4576 struct nfs4_lock_state *lsp,
4577 struct nfs_seqid *seqid)
4578{
4579 struct nfs4_unlockdata *p;
4580 struct inode *inode = lsp->ls_state->inode;
4581
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004582 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004583 if (p == NULL)
4584 return NULL;
4585 p->arg.fh = NFS_FH(inode);
4586 p->arg.fl = &p->fl;
4587 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004588 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004589 p->arg.stateid = &lsp->ls_stateid;
4590 p->lsp = lsp;
4591 atomic_inc(&lsp->ls_count);
4592 /* Ensure we don't close file until we're done freeing locks! */
4593 p->ctx = get_nfs_open_context(ctx);
4594 memcpy(&p->fl, fl, sizeof(p->fl));
4595 p->server = NFS_SERVER(inode);
4596 return p;
4597}
4598
Trond Myklebust06f814a2006-01-03 09:55:07 +01004599static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004600{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004601 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004602 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004603 nfs4_put_lock_state(calldata->lsp);
4604 put_nfs_open_context(calldata->ctx);
4605 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004606}
4607
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004608static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004609{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004610 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611
Trond Myklebust14516c32010-07-31 14:29:06 -04004612 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4613 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004614 switch (task->tk_status) {
4615 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004616 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4617 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004618 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004619 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004620 case -NFS4ERR_BAD_STATEID:
4621 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004622 case -NFS4ERR_STALE_STATEID:
4623 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004624 break;
4625 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004626 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004627 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004628 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004629 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004630}
4631
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004632static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004633{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004634 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004636 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004637 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004639 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004640 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004641 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004642 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004643 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004644 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004645 &calldata->res.seq_res,
4646 task) != 0)
4647 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004648 return;
4649out_no_action:
4650 task->tk_action = NULL;
4651out_wait:
4652 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004655static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004656 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004657 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004658 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004659};
4660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4662 struct nfs_open_context *ctx,
4663 struct nfs4_lock_state *lsp,
4664 struct nfs_seqid *seqid)
4665{
4666 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004667 struct rpc_message msg = {
4668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4669 .rpc_cred = ctx->cred,
4670 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004671 struct rpc_task_setup task_setup_data = {
4672 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004673 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004674 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004675 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004676 .flags = RPC_TASK_ASYNC,
4677 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678
Frank Filz137d6ac2007-07-09 15:32:29 -07004679 /* Ensure this is an unlock - when canceling a lock, the
4680 * canceled lock is passed in, and it won't be an unlock.
4681 */
4682 fl->fl_type = F_UNLCK;
4683
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004684 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4685 if (data == NULL) {
4686 nfs_free_seqid(seqid);
4687 return ERR_PTR(-ENOMEM);
4688 }
4689
Trond Myklebust9d12b212012-01-17 22:04:25 -05004690 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004691 msg.rpc_argp = &data->arg;
4692 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004693 task_setup_data.callback_data = data;
4694 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695}
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4698{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004699 struct inode *inode = state->inode;
4700 struct nfs4_state_owner *sp = state->owner;
4701 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004702 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004703 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004704 struct rpc_task *task;
4705 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004706 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Trond Myklebust9b073572006-06-29 16:38:34 -04004708 status = nfs4_set_lock_state(state, request);
4709 /* Unlock _before_ we do the RPC call */
4710 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004711 /* Exclude nfs_delegation_claim_locks() */
4712 mutex_lock(&sp->so_delegreturn_mutex);
4713 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004714 down_read(&nfsi->rwsem);
4715 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4716 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004717 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004718 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004719 }
4720 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004721 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004722 if (status != 0)
4723 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004724 /* Is this a delegated lock? */
4725 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004726 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004727 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004728 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004729 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004730 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004731 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004732 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733 status = PTR_ERR(task);
4734 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004735 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004737 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004738out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004739 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743struct nfs4_lockdata {
4744 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004745 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746 struct nfs4_lock_state *lsp;
4747 struct nfs_open_context *ctx;
4748 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004749 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 int rpc_status;
4751 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004752 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753};
4754
4755static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004756 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4757 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758{
4759 struct nfs4_lockdata *p;
4760 struct inode *inode = lsp->ls_state->inode;
4761 struct nfs_server *server = NFS_SERVER(inode);
4762
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004763 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764 if (p == NULL)
4765 return NULL;
4766
4767 p->arg.fh = NFS_FH(inode);
4768 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004769 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004770 if (p->arg.open_seqid == NULL)
4771 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004772 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004774 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004775 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004776 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004777 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004778 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004779 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004780 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004781 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004782 atomic_inc(&lsp->ls_count);
4783 p->ctx = get_nfs_open_context(ctx);
4784 memcpy(&p->fl, fl, sizeof(p->fl));
4785 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004786out_free_seqid:
4787 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004788out_free:
4789 kfree(p);
4790 return NULL;
4791}
4792
4793static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4794{
4795 struct nfs4_lockdata *data = calldata;
4796 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Harvey Harrison3110ff82008-05-02 13:42:44 -07004798 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004799 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004800 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801 /* Do we need to do an open_to_lock_owner? */
4802 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004803 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004804 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004805 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004806 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004807 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004808 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004809 } else
4810 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004811 if (!nfs4_valid_open_stateid(state)) {
4812 data->rpc_status = -EBADF;
4813 task->tk_action = NULL;
4814 goto out_release_open_seqid;
4815 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004816 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004817 if (nfs4_setup_sequence(data->server,
4818 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004819 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004820 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004821 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004822out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004823 nfs_release_seqid(data->arg.open_seqid);
4824out_release_lock_seqid:
4825 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004826out_wait:
4827 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004828 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004829}
4830
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4832{
4833 struct nfs4_lockdata *data = calldata;
4834
Harvey Harrison3110ff82008-05-02 13:42:44 -07004835 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004836
Trond Myklebust14516c32010-07-31 14:29:06 -04004837 if (!nfs4_sequence_done(task, &data->res.seq_res))
4838 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004839
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842 if (data->rpc_status == 0)
4843 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4844 else
4845 goto out;
4846 }
4847 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004848 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004849 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004850 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004853 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004854}
4855
4856static void nfs4_lock_release(void *calldata)
4857{
4858 struct nfs4_lockdata *data = calldata;
4859
Harvey Harrison3110ff82008-05-02 13:42:44 -07004860 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004861 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004862 if (data->cancelled != 0) {
4863 struct rpc_task *task;
4864 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4865 data->arg.lock_seqid);
4866 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004867 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004868 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004869 } else
4870 nfs_free_seqid(data->arg.lock_seqid);
4871 nfs4_put_lock_state(data->lsp);
4872 put_nfs_open_context(data->ctx);
4873 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004874 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004875}
4876
4877static const struct rpc_call_ops nfs4_lock_ops = {
4878 .rpc_call_prepare = nfs4_lock_prepare,
4879 .rpc_call_done = nfs4_lock_done,
4880 .rpc_release = nfs4_lock_release,
4881};
4882
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004883static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4884{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004885 switch (error) {
4886 case -NFS4ERR_ADMIN_REVOKED:
4887 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004888 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004889 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004890 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004891 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004892 break;
4893 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004894 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004895 case -NFS4ERR_EXPIRED:
4896 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004897 };
4898}
4899
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004900static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004901{
4902 struct nfs4_lockdata *data;
4903 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004904 struct rpc_message msg = {
4905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4906 .rpc_cred = state->owner->so_cred,
4907 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004908 struct rpc_task_setup task_setup_data = {
4909 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004910 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004911 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004912 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004913 .flags = RPC_TASK_ASYNC,
4914 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004915 int ret;
4916
Harvey Harrison3110ff82008-05-02 13:42:44 -07004917 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004918 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004919 fl->fl_u.nfs4_fl.owner,
4920 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004921 if (data == NULL)
4922 return -ENOMEM;
4923 if (IS_SETLKW(cmd))
4924 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004925 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004926 msg.rpc_argp = &data->arg;
4927 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004928 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004929 if (recovery_type > NFS_LOCK_NEW) {
4930 if (recovery_type == NFS_LOCK_RECLAIM)
4931 data->arg.reclaim = NFS_LOCK_RECLAIM;
4932 nfs4_set_sequence_privileged(&data->arg.seq_args);
4933 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004934 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004935 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004936 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004937 ret = nfs4_wait_for_completion_rpc_task(task);
4938 if (ret == 0) {
4939 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004940 if (ret)
4941 nfs4_handle_setlk_error(data->server, data->lsp,
4942 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004943 } else
4944 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004945 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004946 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004947 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
4950static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4951{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004952 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004953 struct nfs4_exception exception = {
4954 .inode = state->inode,
4955 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004956 int err;
4957
4958 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004959 /* Cache the lock if possible... */
4960 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4961 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004962 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004963 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004964 break;
4965 nfs4_handle_exception(server, err, &exception);
4966 } while (exception.retry);
4967 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
4970static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4971{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004972 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004973 struct nfs4_exception exception = {
4974 .inode = state->inode,
4975 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004976 int err;
4977
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004978 err = nfs4_set_lock_state(state, request);
4979 if (err != 0)
4980 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004981 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4983 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004985 switch (err) {
4986 default:
4987 goto out;
4988 case -NFS4ERR_GRACE:
4989 case -NFS4ERR_DELAY:
4990 nfs4_handle_exception(server, err, &exception);
4991 err = 0;
4992 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004993 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004994out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004998#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004999/**
5000 * nfs41_check_expired_locks - possibly free a lock stateid
5001 *
5002 * @state: NFSv4 state for an inode
5003 *
5004 * Returns NFS_OK if recovery for this stateid is now finished.
5005 * Otherwise a negative NFS4ERR value is returned.
5006 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005007static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005008{
Chuck Levereb64cf92012-07-11 16:30:05 -04005009 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005010 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005011 struct nfs_server *server = NFS_SERVER(state->inode);
5012
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005013 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005014 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005015 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5016 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005017 /* Free the stateid unless the server
5018 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005019 if (status != -NFS4ERR_BAD_STATEID)
5020 nfs41_free_stateid(server,
5021 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005022 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005023 ret = status;
5024 }
5025 }
5026 };
5027
5028 return ret;
5029}
5030
5031static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5032{
5033 int status = NFS_OK;
5034
5035 if (test_bit(LK_STATE_IN_USE, &state->flags))
5036 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005037 if (status != NFS_OK)
5038 status = nfs4_lock_expired(state, request);
5039 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005040}
5041#endif
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5044{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005045 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005046 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005047 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005048 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005049 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005051 if ((fl_flags & FL_POSIX) &&
5052 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5053 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005054 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005055 status = nfs4_set_lock_state(state, request);
5056 if (status != 0)
5057 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058 request->fl_flags |= FL_ACCESS;
5059 status = do_vfs_lock(request->fl_file, request);
5060 if (status < 0)
5061 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005062 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005063 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005064 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005065 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005066 request->fl_flags = fl_flags & ~FL_SLEEP;
5067 status = do_vfs_lock(request->fl_file, request);
5068 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005069 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005070 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5071 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005072 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005073 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005074 goto out;
5075 down_read(&nfsi->rwsem);
5076 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5077 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005078 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005079 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005080 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005081 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005082 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005083 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5084 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005085out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005086 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005087out:
5088 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 return status;
5090}
5091
5092static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5093{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005094 struct nfs4_exception exception = {
5095 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005096 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 int err;
5099
5100 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005101 err = _nfs4_proc_setlk(state, cmd, request);
5102 if (err == -NFS4ERR_DENIED)
5103 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005105 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 } while (exception.retry);
5107 return err;
5108}
5109
5110static int
5111nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5112{
5113 struct nfs_open_context *ctx;
5114 struct nfs4_state *state;
5115 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5116 int status;
5117
5118 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005119 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 state = ctx->state;
5121
5122 if (request->fl_start < 0 || request->fl_end < 0)
5123 return -EINVAL;
5124
Trond Myklebustd9531262009-07-21 19:22:38 -04005125 if (IS_GETLK(cmd)) {
5126 if (state != NULL)
5127 return nfs4_proc_getlk(state, F_GETLK, request);
5128 return 0;
5129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
5131 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5132 return -EINVAL;
5133
Trond Myklebustd9531262009-07-21 19:22:38 -04005134 if (request->fl_type == F_UNLCK) {
5135 if (state != NULL)
5136 return nfs4_proc_unlck(state, cmd, request);
5137 return 0;
5138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
Trond Myklebustd9531262009-07-21 19:22:38 -04005140 if (state == NULL)
5141 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005142 /*
5143 * Don't rely on the VFS having checked the file open mode,
5144 * since it won't do this for flock() locks.
5145 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005146 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005147 case F_RDLCK:
5148 if (!(filp->f_mode & FMODE_READ))
5149 return -EBADF;
5150 break;
5151 case F_WRLCK:
5152 if (!(filp->f_mode & FMODE_WRITE))
5153 return -EBADF;
5154 }
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 do {
5157 status = nfs4_proc_setlk(state, cmd, request);
5158 if ((status != -EAGAIN) || IS_SETLK(cmd))
5159 break;
5160 timeout = nfs4_set_lock_task_retry(timeout);
5161 status = -ERESTARTSYS;
5162 if (signalled())
5163 break;
5164 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return status;
5166}
5167
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005168int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005169{
5170 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005171 int err;
5172
5173 err = nfs4_set_lock_state(state, fl);
5174 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005175 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005176 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005177 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005178}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005179
Trond Myklebustcf470c32012-03-07 13:49:12 -05005180struct nfs_release_lockowner_data {
5181 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005182 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005183 struct nfs_release_lockowner_args args;
5184};
5185
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005186static void nfs4_release_lockowner_release(void *calldata)
5187{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005188 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005189 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005190 kfree(calldata);
5191}
5192
Trond Myklebust17280172012-03-11 13:11:00 -04005193static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005194 .rpc_release = nfs4_release_lockowner_release,
5195};
5196
Trond Myklebustcf470c32012-03-07 13:49:12 -05005197int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005198{
5199 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005200 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5203 };
5204
5205 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005206 return -EINVAL;
5207 data = kmalloc(sizeof(*data), GFP_NOFS);
5208 if (!data)
5209 return -ENOMEM;
5210 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005211 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005212 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5213 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5214 data->args.lock_owner.s_dev = server->s_dev;
5215 msg.rpc_argp = &data->args;
5216 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5217 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005218}
5219
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005220#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5221
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005222static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5223 const void *buf, size_t buflen,
5224 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005225{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005226 if (strcmp(key, "") != 0)
5227 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005228
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005229 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005230}
5231
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005232static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5233 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005234{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005235 if (strcmp(key, "") != 0)
5236 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005237
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005238 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005239}
5240
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005241static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5242 size_t list_len, const char *name,
5243 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005244{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005245 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005246
J. Bruce Fields096455a2006-03-20 23:23:42 -05005247 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5248 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005249
5250 if (list && len <= list_len)
5251 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005252 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005253}
5254
Andy Adamson533eb462011-06-13 18:25:56 -04005255/*
5256 * nfs_fhget will use either the mounted_on_fileid or the fileid
5257 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005258static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5259{
Andy Adamson533eb462011-06-13 18:25:56 -04005260 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5261 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5262 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005263 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005264 return;
5265
5266 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005267 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005268 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5269 fattr->nlink = 2;
5270}
5271
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005272static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5273 const struct qstr *name,
5274 struct nfs4_fs_locations *fs_locations,
5275 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005276{
5277 struct nfs_server *server = NFS_SERVER(dir);
5278 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005279 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005280 };
5281 struct nfs4_fs_locations_arg args = {
5282 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005283 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005284 .page = page,
5285 .bitmask = bitmask,
5286 };
Benny Halevy22958462009-04-01 09:22:02 -04005287 struct nfs4_fs_locations_res res = {
5288 .fs_locations = fs_locations,
5289 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5292 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005293 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005294 };
5295 int status;
5296
Harvey Harrison3110ff82008-05-02 13:42:44 -07005297 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005298
5299 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5300 * is not supported */
5301 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5302 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5303 else
5304 bitmask[0] |= FATTR4_WORD0_FILEID;
5305
Trond Myklebustc228fd32007-01-13 02:28:11 -05005306 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005307 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005308 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005309 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005310 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005311 return status;
5312}
5313
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005314int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5315 const struct qstr *name,
5316 struct nfs4_fs_locations *fs_locations,
5317 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005318{
5319 struct nfs4_exception exception = { };
5320 int err;
5321 do {
5322 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005323 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005324 &exception);
5325 } while (exception.retry);
5326 return err;
5327}
5328
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005329static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5330{
5331 int status;
5332 struct nfs4_secinfo_arg args = {
5333 .dir_fh = NFS_FH(dir),
5334 .name = name,
5335 };
5336 struct nfs4_secinfo_res res = {
5337 .flavors = flavors,
5338 };
5339 struct rpc_message msg = {
5340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5341 .rpc_argp = &args,
5342 .rpc_resp = &res,
5343 };
5344
5345 dprintk("NFS call secinfo %s\n", name->name);
5346 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5347 dprintk("NFS reply secinfo: %d\n", status);
5348 return status;
5349}
5350
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005351int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5352 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005353{
5354 struct nfs4_exception exception = { };
5355 int err;
5356 do {
5357 err = nfs4_handle_exception(NFS_SERVER(dir),
5358 _nfs4_proc_secinfo(dir, name, flavors),
5359 &exception);
5360 } while (exception.retry);
5361 return err;
5362}
5363
Andy Adamson557134a2009-04-01 09:21:53 -04005364#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005365/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005366 * Check the exchange flags returned by the server for invalid flags, having
5367 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5368 * DS flags set.
5369 */
5370static int nfs4_check_cl_exchange_flags(u32 flags)
5371{
5372 if (flags & ~EXCHGID4_FLAG_MASK_R)
5373 goto out_inval;
5374 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5375 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5376 goto out_inval;
5377 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5378 goto out_inval;
5379 return NFS_OK;
5380out_inval:
5381 return -NFS4ERR_INVAL;
5382}
5383
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005384static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005385nfs41_same_server_scope(struct nfs41_server_scope *a,
5386 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005387{
5388 if (a->server_scope_sz == b->server_scope_sz &&
5389 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5390 return true;
5391
5392 return false;
5393}
5394
Andy Adamson357f54d2010-12-14 10:11:57 -05005395/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005396 * nfs4_proc_bind_conn_to_session()
5397 *
5398 * The 4.1 client currently uses the same TCP connection for the
5399 * fore and backchannel.
5400 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005401int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005402{
5403 int status;
5404 struct nfs41_bind_conn_to_session_res res;
5405 struct rpc_message msg = {
5406 .rpc_proc =
5407 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5408 .rpc_argp = clp,
5409 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005410 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005411 };
5412
5413 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005414
5415 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5416 if (unlikely(res.session == NULL)) {
5417 status = -ENOMEM;
5418 goto out;
5419 }
5420
5421 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5422 if (status == 0) {
5423 if (memcmp(res.session->sess_id.data,
5424 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5425 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5426 status = -EIO;
5427 goto out_session;
5428 }
5429 if (res.dir != NFS4_CDFS4_BOTH) {
5430 dprintk("NFS: %s: Unexpected direction from server\n",
5431 __func__);
5432 status = -EIO;
5433 goto out_session;
5434 }
5435 if (res.use_conn_in_rdma_mode) {
5436 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5437 __func__);
5438 status = -EIO;
5439 goto out_session;
5440 }
5441 }
5442out_session:
5443 kfree(res.session);
5444out:
5445 dprintk("<-- %s status= %d\n", __func__, status);
5446 return status;
5447}
5448
5449/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005450 * nfs4_proc_exchange_id()
5451 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5453 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005454 * Since the clientid has expired, all compounds using sessions
5455 * associated with the stale clientid will be returning
5456 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5457 * be in some phase of session reset.
5458 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005459int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005460{
5461 nfs4_verifier verifier;
5462 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005463 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005464 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005465 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005466 };
5467 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005468 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469 };
5470 int status;
5471 struct rpc_message msg = {
5472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5473 .rpc_argp = &args,
5474 .rpc_resp = &res,
5475 .rpc_cred = cred,
5476 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005477
Chuck Leverf0920752012-05-21 22:45:41 -04005478 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005479 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5480 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005481 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5482 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5483 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005484
Chuck Leveracdeb692012-05-21 22:46:16 -04005485 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005486 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005487 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005488 status = -ENOMEM;
5489 goto out;
5490 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005491
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005492 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005493 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005494 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005495 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005496 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005497 }
5498
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005499 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005500 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005501 status = -ENOMEM;
5502 goto out_server_scope;
5503 }
5504
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005505 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005506 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005507 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005508
Chuck Lever177313f2012-05-21 22:44:58 -04005509 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005510 clp->cl_clientid = res.clientid;
5511 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5512 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5513 clp->cl_seqid = res.seqid;
5514
Chuck Leveracdeb692012-05-21 22:46:16 -04005515 kfree(clp->cl_serverowner);
5516 clp->cl_serverowner = res.server_owner;
5517 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005518
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005519 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005520 kfree(clp->cl_implid);
5521 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005522
Chuck Lever177313f2012-05-21 22:44:58 -04005523 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005524 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005525 res.server_scope)) {
5526 dprintk("%s: server_scope mismatch detected\n",
5527 __func__);
5528 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005529 kfree(clp->cl_serverscope);
5530 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005531 }
5532
Chuck Lever177313f2012-05-21 22:44:58 -04005533 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005534 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005535 goto out;
5536 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005537 } else
5538 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005539
Chuck Leveracdeb692012-05-21 22:46:16 -04005540out_server_owner:
5541 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005542out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005543 kfree(res.server_scope);
5544out:
Chuck Lever177313f2012-05-21 22:44:58 -04005545 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005546 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005547 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005548 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005549 clp->cl_implid->date.seconds,
5550 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005552 return status;
5553}
5554
Trond Myklebust66245532012-05-25 17:18:09 -04005555static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5556 struct rpc_cred *cred)
5557{
5558 struct rpc_message msg = {
5559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5560 .rpc_argp = clp,
5561 .rpc_cred = cred,
5562 };
5563 int status;
5564
5565 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5566 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005567 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005568 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5569 return status;
5570}
5571
5572static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5573 struct rpc_cred *cred)
5574{
5575 unsigned int loop;
5576 int ret;
5577
5578 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5579 ret = _nfs4_proc_destroy_clientid(clp, cred);
5580 switch (ret) {
5581 case -NFS4ERR_DELAY:
5582 case -NFS4ERR_CLIENTID_BUSY:
5583 ssleep(1);
5584 break;
5585 default:
5586 return ret;
5587 }
5588 }
5589 return 0;
5590}
5591
5592int nfs4_destroy_clientid(struct nfs_client *clp)
5593{
5594 struct rpc_cred *cred;
5595 int ret = 0;
5596
5597 if (clp->cl_mvops->minor_version < 1)
5598 goto out;
5599 if (clp->cl_exchange_flags == 0)
5600 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005601 if (clp->cl_preserve_clid)
5602 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005603 cred = nfs4_get_exchange_id_cred(clp);
5604 ret = nfs4_proc_destroy_clientid(clp, cred);
5605 if (cred)
5606 put_rpccred(cred);
5607 switch (ret) {
5608 case 0:
5609 case -NFS4ERR_STALE_CLIENTID:
5610 clp->cl_exchange_flags = 0;
5611 }
5612out:
5613 return ret;
5614}
5615
Andy Adamson2050f0c2009-04-01 09:22:30 -04005616struct nfs4_get_lease_time_data {
5617 struct nfs4_get_lease_time_args *args;
5618 struct nfs4_get_lease_time_res *res;
5619 struct nfs_client *clp;
5620};
5621
5622static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5623 void *calldata)
5624{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005625 struct nfs4_get_lease_time_data *data =
5626 (struct nfs4_get_lease_time_data *)calldata;
5627
5628 dprintk("--> %s\n", __func__);
5629 /* just setup sequence, do not trigger session recovery
5630 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005631 nfs41_setup_sequence(data->clp->cl_session,
5632 &data->args->la_seq_args,
5633 &data->res->lr_seq_res,
5634 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005635 dprintk("<-- %s\n", __func__);
5636}
5637
5638/*
5639 * Called from nfs4_state_manager thread for session setup, so don't recover
5640 * from sequence operation or clientid errors.
5641 */
5642static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5643{
5644 struct nfs4_get_lease_time_data *data =
5645 (struct nfs4_get_lease_time_data *)calldata;
5646
5647 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005648 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5649 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005650 switch (task->tk_status) {
5651 case -NFS4ERR_DELAY:
5652 case -NFS4ERR_GRACE:
5653 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5654 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5655 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005656 /* fall through */
5657 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005658 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005659 return;
5660 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005661 dprintk("<-- %s\n", __func__);
5662}
5663
Trond Myklebust17280172012-03-11 13:11:00 -04005664static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005665 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5666 .rpc_call_done = nfs4_get_lease_time_done,
5667};
5668
5669int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5670{
5671 struct rpc_task *task;
5672 struct nfs4_get_lease_time_args args;
5673 struct nfs4_get_lease_time_res res = {
5674 .lr_fsinfo = fsinfo,
5675 };
5676 struct nfs4_get_lease_time_data data = {
5677 .args = &args,
5678 .res = &res,
5679 .clp = clp,
5680 };
5681 struct rpc_message msg = {
5682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5683 .rpc_argp = &args,
5684 .rpc_resp = &res,
5685 };
5686 struct rpc_task_setup task_setup = {
5687 .rpc_client = clp->cl_rpcclient,
5688 .rpc_message = &msg,
5689 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005690 .callback_data = &data,
5691 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005692 };
5693 int status;
5694
Trond Myklebust9d12b212012-01-17 22:04:25 -05005695 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005696 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005697 dprintk("--> %s\n", __func__);
5698 task = rpc_run_task(&task_setup);
5699
5700 if (IS_ERR(task))
5701 status = PTR_ERR(task);
5702 else {
5703 status = task->tk_status;
5704 rpc_put_task(task);
5705 }
5706 dprintk("<-- %s return %d\n", __func__, status);
5707
5708 return status;
5709}
5710
Andy Adamsonfc931582009-04-01 09:22:31 -04005711/*
5712 * Initialize the values to be used by the client in CREATE_SESSION
5713 * If nfs4_init_session set the fore channel request and response sizes,
5714 * use them.
5715 *
5716 * Set the back channel max_resp_sz_cached to zero to force the client to
5717 * always set csa_cachethis to FALSE because the current implementation
5718 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5719 */
5720static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5721{
5722 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005723 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5724 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005725
5726 if (mxrqst_sz == 0)
5727 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5728 if (mxresp_sz == 0)
5729 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5730 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005731 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5732 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005733 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005734 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005735
5736 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005737 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005738 __func__,
5739 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005740 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005741
5742 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005743 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5744 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5745 args->bc_attrs.max_resp_sz_cached = 0;
5746 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5747 args->bc_attrs.max_reqs = 1;
5748
5749 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5750 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5751 __func__,
5752 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5753 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5754 args->bc_attrs.max_reqs);
5755}
5756
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005757static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005758{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005759 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5760 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5761
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005762 if (rcvd->max_resp_sz > sent->max_resp_sz)
5763 return -EINVAL;
5764 /*
5765 * Our requested max_ops is the minimum we need; we're not
5766 * prepared to break up compounds into smaller pieces than that.
5767 * So, no point even trying to continue if the server won't
5768 * cooperate:
5769 */
5770 if (rcvd->max_ops < sent->max_ops)
5771 return -EINVAL;
5772 if (rcvd->max_reqs == 0)
5773 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005774 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5775 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005776 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005777}
5778
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005779static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5780{
5781 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5782 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005783
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005784 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5785 return -EINVAL;
5786 if (rcvd->max_resp_sz < sent->max_resp_sz)
5787 return -EINVAL;
5788 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5789 return -EINVAL;
5790 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005791 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005792 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005793 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005794 return -EINVAL;
5795 return 0;
5796}
Andy Adamson8d353012009-04-01 09:22:32 -04005797
Andy Adamson8d353012009-04-01 09:22:32 -04005798static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5799 struct nfs4_session *session)
5800{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005801 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005802
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005803 ret = nfs4_verify_fore_channel_attrs(args, session);
5804 if (ret)
5805 return ret;
5806 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005807}
5808
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005809static int _nfs4_proc_create_session(struct nfs_client *clp,
5810 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005811{
5812 struct nfs4_session *session = clp->cl_session;
5813 struct nfs41_create_session_args args = {
5814 .client = clp,
5815 .cb_program = NFS4_CALLBACK,
5816 };
5817 struct nfs41_create_session_res res = {
5818 .client = clp,
5819 };
5820 struct rpc_message msg = {
5821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5822 .rpc_argp = &args,
5823 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005824 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005825 };
5826 int status;
5827
5828 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005829 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005830
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005831 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005832
Trond Myklebust43095d32012-11-20 11:13:12 -05005833 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005834 /* Verify the session's negotiated channel_attrs values */
5835 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005836 /* Increment the clientid slot sequence id */
5837 clp->cl_seqid++;
5838 }
5839
5840 return status;
5841}
5842
5843/*
5844 * Issues a CREATE_SESSION operation to the server.
5845 * It is the responsibility of the caller to verify the session is
5846 * expired before calling this routine.
5847 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005848int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005849{
5850 int status;
5851 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005852 struct nfs4_session *session = clp->cl_session;
5853
5854 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5855
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005856 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005857 if (status)
5858 goto out;
5859
Andy Adamsonaacd5532011-11-09 13:58:21 -05005860 /* Init or reset the session slot tables */
5861 status = nfs4_setup_session_slot_tables(session);
5862 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005863 if (status)
5864 goto out;
5865
5866 ptr = (unsigned *)&session->sess_id.data[0];
5867 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5868 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005869out:
5870 dprintk("<-- %s\n", __func__);
5871 return status;
5872}
5873
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005874/*
5875 * Issue the over-the-wire RPC DESTROY_SESSION.
5876 * The caller must serialize access to this routine.
5877 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005878int nfs4_proc_destroy_session(struct nfs4_session *session,
5879 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005880{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005881 struct rpc_message msg = {
5882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5883 .rpc_argp = session,
5884 .rpc_cred = cred,
5885 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005886 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005887
5888 dprintk("--> nfs4_proc_destroy_session\n");
5889
5890 /* session is still being setup */
5891 if (session->clp->cl_cons_state != NFS_CS_READY)
5892 return status;
5893
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005894 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005895
5896 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005897 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005898 "Session has been destroyed regardless...\n", status);
5899
5900 dprintk("<-- nfs4_proc_destroy_session\n");
5901 return status;
5902}
5903
Trond Myklebust7b38c362012-05-23 13:23:31 -04005904/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005905 * Renew the cl_session lease.
5906 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005907struct nfs4_sequence_data {
5908 struct nfs_client *clp;
5909 struct nfs4_sequence_args args;
5910 struct nfs4_sequence_res res;
5911};
5912
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005913static void nfs41_sequence_release(void *data)
5914{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005915 struct nfs4_sequence_data *calldata = data;
5916 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005917
Alexandros Batsakis71358402010-02-05 03:45:05 -08005918 if (atomic_read(&clp->cl_count) > 1)
5919 nfs4_schedule_state_renewal(clp);
5920 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005921 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005922}
5923
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005924static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5925{
5926 switch(task->tk_status) {
5927 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5929 return -EAGAIN;
5930 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005931 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005932 }
5933 return 0;
5934}
5935
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005936static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005937{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005938 struct nfs4_sequence_data *calldata = data;
5939 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005940
Trond Myklebust14516c32010-07-31 14:29:06 -04005941 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5942 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005943
5944 if (task->tk_status < 0) {
5945 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005946 if (atomic_read(&clp->cl_count) == 1)
5947 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005948
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005949 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5950 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951 return;
5952 }
5953 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005955out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956 dprintk("<-- %s\n", __func__);
5957}
5958
5959static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5960{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005961 struct nfs4_sequence_data *calldata = data;
5962 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005963 struct nfs4_sequence_args *args;
5964 struct nfs4_sequence_res *res;
5965
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966 args = task->tk_msg.rpc_argp;
5967 res = task->tk_msg.rpc_resp;
5968
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005969 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005970}
5971
5972static const struct rpc_call_ops nfs41_sequence_ops = {
5973 .rpc_call_done = nfs41_sequence_call_done,
5974 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005975 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005976};
5977
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005978static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5979 struct rpc_cred *cred,
5980 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005982 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005983 struct rpc_message msg = {
5984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5985 .rpc_cred = cred,
5986 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005987 struct rpc_task_setup task_setup_data = {
5988 .rpc_client = clp->cl_rpcclient,
5989 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005990 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005991 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005992 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005993
Alexandros Batsakis71358402010-02-05 03:45:05 -08005994 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005995 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005996 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005997 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005998 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005999 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006000 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006001 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006002 if (is_privileged)
6003 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006004 msg.rpc_argp = &calldata->args;
6005 msg.rpc_resp = &calldata->res;
6006 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006007 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006008
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006009 return rpc_run_task(&task_setup_data);
6010}
6011
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006012static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006013{
6014 struct rpc_task *task;
6015 int ret = 0;
6016
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006017 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6018 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006019 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006020 if (IS_ERR(task))
6021 ret = PTR_ERR(task);
6022 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006023 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024 dprintk("<-- %s status=%d\n", __func__, ret);
6025 return ret;
6026}
6027
6028static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6029{
6030 struct rpc_task *task;
6031 int ret;
6032
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006033 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006034 if (IS_ERR(task)) {
6035 ret = PTR_ERR(task);
6036 goto out;
6037 }
6038 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006039 if (!ret) {
6040 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6041
6042 if (task->tk_status == 0)
6043 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006044 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006045 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006046 rpc_put_task(task);
6047out:
6048 dprintk("<-- %s status=%d\n", __func__, ret);
6049 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006050}
6051
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006052struct nfs4_reclaim_complete_data {
6053 struct nfs_client *clp;
6054 struct nfs41_reclaim_complete_args arg;
6055 struct nfs41_reclaim_complete_res res;
6056};
6057
6058static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6059{
6060 struct nfs4_reclaim_complete_data *calldata = data;
6061
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006062 nfs41_setup_sequence(calldata->clp->cl_session,
6063 &calldata->arg.seq_args,
6064 &calldata->res.seq_res,
6065 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006066}
6067
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006068static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6069{
6070 switch(task->tk_status) {
6071 case 0:
6072 case -NFS4ERR_COMPLETE_ALREADY:
6073 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6074 break;
6075 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006076 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006077 /* fall through */
6078 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006079 return -EAGAIN;
6080 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006081 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006082 }
6083 return 0;
6084}
6085
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006086static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6087{
6088 struct nfs4_reclaim_complete_data *calldata = data;
6089 struct nfs_client *clp = calldata->clp;
6090 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6091
6092 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006093 if (!nfs41_sequence_done(task, res))
6094 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006095
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006096 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6097 rpc_restart_call_prepare(task);
6098 return;
6099 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006100 dprintk("<-- %s\n", __func__);
6101}
6102
6103static void nfs4_free_reclaim_complete_data(void *data)
6104{
6105 struct nfs4_reclaim_complete_data *calldata = data;
6106
6107 kfree(calldata);
6108}
6109
6110static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6111 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6112 .rpc_call_done = nfs4_reclaim_complete_done,
6113 .rpc_release = nfs4_free_reclaim_complete_data,
6114};
6115
6116/*
6117 * Issue a global reclaim complete.
6118 */
6119static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6120{
6121 struct nfs4_reclaim_complete_data *calldata;
6122 struct rpc_task *task;
6123 struct rpc_message msg = {
6124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6125 };
6126 struct rpc_task_setup task_setup_data = {
6127 .rpc_client = clp->cl_rpcclient,
6128 .rpc_message = &msg,
6129 .callback_ops = &nfs4_reclaim_complete_call_ops,
6130 .flags = RPC_TASK_ASYNC,
6131 };
6132 int status = -ENOMEM;
6133
6134 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006135 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006136 if (calldata == NULL)
6137 goto out;
6138 calldata->clp = clp;
6139 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006140
Trond Myklebust9d12b212012-01-17 22:04:25 -05006141 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006142 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006143 msg.rpc_argp = &calldata->arg;
6144 msg.rpc_resp = &calldata->res;
6145 task_setup_data.callback_data = calldata;
6146 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006147 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006148 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006149 goto out;
6150 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006151 status = nfs4_wait_for_completion_rpc_task(task);
6152 if (status == 0)
6153 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006155 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006156out:
6157 dprintk("<-- %s status=%d\n", __func__, status);
6158 return status;
6159}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006160
6161static void
6162nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6163{
6164 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006165 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006166 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006167
6168 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006169 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6170 * right now covering the LAYOUTGET we are about to send.
6171 * However, that is not so catastrophic, and there seems
6172 * to be no way to prevent it completely.
6173 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006174 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006175 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006176 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006177 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6178 NFS_I(lgp->args.inode)->layout,
6179 lgp->args.ctx->state)) {
6180 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006181 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006182}
6183
6184static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6185{
6186 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006187 struct inode *inode = lgp->args.inode;
6188 struct nfs_server *server = NFS_SERVER(inode);
6189 struct pnfs_layout_hdr *lo;
6190 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006191 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006192
6193 dprintk("--> %s\n", __func__);
6194
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006195 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006196 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197
6198 switch (task->tk_status) {
6199 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006200 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006201 case -NFS4ERR_LAYOUTTRYLATER:
6202 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006203 timeo = rpc_get_timeout(task->tk_client);
6204 giveup = lgp->args.timestamp + timeo;
6205 if (time_after(giveup, jiffies))
6206 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006207 break;
6208 case -NFS4ERR_EXPIRED:
6209 case -NFS4ERR_BAD_STATEID:
6210 spin_lock(&inode->i_lock);
6211 lo = NFS_I(inode)->layout;
6212 if (!lo || list_empty(&lo->plh_segs)) {
6213 spin_unlock(&inode->i_lock);
6214 /* If the open stateid was bad, then recover it. */
6215 state = lgp->args.ctx->state;
6216 } else {
6217 LIST_HEAD(head);
6218
6219 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6220 spin_unlock(&inode->i_lock);
6221 /* Mark the bad layout state as invalid, then
6222 * retry using the open stateid. */
6223 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006224 }
6225 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006226 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6227 rpc_restart_call_prepare(task);
6228out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006229 dprintk("<-- %s\n", __func__);
6230}
6231
Idan Kedar85541162012-08-02 11:47:10 +03006232static size_t max_response_pages(struct nfs_server *server)
6233{
6234 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6235 return nfs_page_array_len(0, max_resp_sz);
6236}
6237
6238static void nfs4_free_pages(struct page **pages, size_t size)
6239{
6240 int i;
6241
6242 if (!pages)
6243 return;
6244
6245 for (i = 0; i < size; i++) {
6246 if (!pages[i])
6247 break;
6248 __free_page(pages[i]);
6249 }
6250 kfree(pages);
6251}
6252
6253static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6254{
6255 struct page **pages;
6256 int i;
6257
6258 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6259 if (!pages) {
6260 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6261 return NULL;
6262 }
6263
6264 for (i = 0; i < size; i++) {
6265 pages[i] = alloc_page(gfp_flags);
6266 if (!pages[i]) {
6267 dprintk("%s: failed to allocate page\n", __func__);
6268 nfs4_free_pages(pages, size);
6269 return NULL;
6270 }
6271 }
6272
6273 return pages;
6274}
6275
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276static void nfs4_layoutget_release(void *calldata)
6277{
6278 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006279 struct inode *inode = lgp->args.inode;
6280 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006281 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006282
6283 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006284 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006285 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006286 put_nfs_open_context(lgp->args.ctx);
6287 kfree(calldata);
6288 dprintk("<-- %s\n", __func__);
6289}
6290
6291static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6292 .rpc_call_prepare = nfs4_layoutget_prepare,
6293 .rpc_call_done = nfs4_layoutget_done,
6294 .rpc_release = nfs4_layoutget_release,
6295};
6296
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006297struct pnfs_layout_segment *
6298nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006299{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006300 struct inode *inode = lgp->args.inode;
6301 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006302 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006303 struct rpc_task *task;
6304 struct rpc_message msg = {
6305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6306 .rpc_argp = &lgp->args,
6307 .rpc_resp = &lgp->res,
6308 };
6309 struct rpc_task_setup task_setup_data = {
6310 .rpc_client = server->client,
6311 .rpc_message = &msg,
6312 .callback_ops = &nfs4_layoutget_call_ops,
6313 .callback_data = lgp,
6314 .flags = RPC_TASK_ASYNC,
6315 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006316 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006317 int status = 0;
6318
6319 dprintk("--> %s\n", __func__);
6320
Idan Kedar85541162012-08-02 11:47:10 +03006321 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6322 if (!lgp->args.layout.pages) {
6323 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006324 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006325 }
6326 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006327 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006328
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006329 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006330 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006331 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006332
6333 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6334 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6335
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006336 task = rpc_run_task(&task_setup_data);
6337 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006338 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006339 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006340 if (status == 0)
6341 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006342 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6343 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006344 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006345 rpc_put_task(task);
6346 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006347 if (status)
6348 return ERR_PTR(status);
6349 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350}
6351
Benny Halevycbe82602011-05-22 19:52:37 +03006352static void
6353nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6354{
6355 struct nfs4_layoutreturn *lrp = calldata;
6356
6357 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006358 nfs41_setup_sequence(lrp->clp->cl_session,
6359 &lrp->args.seq_args,
6360 &lrp->res.seq_res,
6361 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006362}
6363
6364static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6365{
6366 struct nfs4_layoutreturn *lrp = calldata;
6367 struct nfs_server *server;
6368
6369 dprintk("--> %s\n", __func__);
6370
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006371 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006372 return;
6373
6374 server = NFS_SERVER(lrp->args.inode);
6375 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006376 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006377 return;
6378 }
Benny Halevycbe82602011-05-22 19:52:37 +03006379 dprintk("<-- %s\n", __func__);
6380}
6381
6382static void nfs4_layoutreturn_release(void *calldata)
6383{
6384 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006385 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006386
6387 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006388 spin_lock(&lo->plh_inode->i_lock);
6389 if (lrp->res.lrs_present)
6390 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6391 lo->plh_block_lgets--;
6392 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006393 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006394 kfree(calldata);
6395 dprintk("<-- %s\n", __func__);
6396}
6397
6398static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6399 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6400 .rpc_call_done = nfs4_layoutreturn_done,
6401 .rpc_release = nfs4_layoutreturn_release,
6402};
6403
6404int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6405{
6406 struct rpc_task *task;
6407 struct rpc_message msg = {
6408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6409 .rpc_argp = &lrp->args,
6410 .rpc_resp = &lrp->res,
6411 };
6412 struct rpc_task_setup task_setup_data = {
6413 .rpc_client = lrp->clp->cl_rpcclient,
6414 .rpc_message = &msg,
6415 .callback_ops = &nfs4_layoutreturn_call_ops,
6416 .callback_data = lrp,
6417 };
6418 int status;
6419
6420 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006421 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006422 task = rpc_run_task(&task_setup_data);
6423 if (IS_ERR(task))
6424 return PTR_ERR(task);
6425 status = task->tk_status;
6426 dprintk("<-- %s status=%d\n", __func__, status);
6427 rpc_put_task(task);
6428 return status;
6429}
6430
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006431/*
6432 * Retrieve the list of Data Server devices from the MDS.
6433 */
6434static int _nfs4_getdevicelist(struct nfs_server *server,
6435 const struct nfs_fh *fh,
6436 struct pnfs_devicelist *devlist)
6437{
6438 struct nfs4_getdevicelist_args args = {
6439 .fh = fh,
6440 .layoutclass = server->pnfs_curr_ld->id,
6441 };
6442 struct nfs4_getdevicelist_res res = {
6443 .devlist = devlist,
6444 };
6445 struct rpc_message msg = {
6446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6447 .rpc_argp = &args,
6448 .rpc_resp = &res,
6449 };
6450 int status;
6451
6452 dprintk("--> %s\n", __func__);
6453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6454 &res.seq_res, 0);
6455 dprintk("<-- %s status=%d\n", __func__, status);
6456 return status;
6457}
6458
6459int nfs4_proc_getdevicelist(struct nfs_server *server,
6460 const struct nfs_fh *fh,
6461 struct pnfs_devicelist *devlist)
6462{
6463 struct nfs4_exception exception = { };
6464 int err;
6465
6466 do {
6467 err = nfs4_handle_exception(server,
6468 _nfs4_getdevicelist(server, fh, devlist),
6469 &exception);
6470 } while (exception.retry);
6471
6472 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6473 err, devlist->num_devs);
6474
6475 return err;
6476}
6477EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6478
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006479static int
6480_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6481{
6482 struct nfs4_getdeviceinfo_args args = {
6483 .pdev = pdev,
6484 };
6485 struct nfs4_getdeviceinfo_res res = {
6486 .pdev = pdev,
6487 };
6488 struct rpc_message msg = {
6489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6490 .rpc_argp = &args,
6491 .rpc_resp = &res,
6492 };
6493 int status;
6494
6495 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006496 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006497 dprintk("<-- %s status=%d\n", __func__, status);
6498
6499 return status;
6500}
6501
6502int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6503{
6504 struct nfs4_exception exception = { };
6505 int err;
6506
6507 do {
6508 err = nfs4_handle_exception(server,
6509 _nfs4_proc_getdeviceinfo(server, pdev),
6510 &exception);
6511 } while (exception.retry);
6512 return err;
6513}
6514EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6515
Andy Adamson863a3c62011-03-23 13:27:54 +00006516static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6517{
6518 struct nfs4_layoutcommit_data *data = calldata;
6519 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006520 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006521
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006522 nfs41_setup_sequence(session,
6523 &data->args.seq_args,
6524 &data->res.seq_res,
6525 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006526}
6527
6528static void
6529nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6530{
6531 struct nfs4_layoutcommit_data *data = calldata;
6532 struct nfs_server *server = NFS_SERVER(data->args.inode);
6533
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006534 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006535 return;
6536
6537 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006538 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6539 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6540 case -NFS4ERR_BADLAYOUT: /* no layout */
6541 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006542 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006543 break;
6544 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006545 nfs_post_op_update_inode_force_wcc(data->args.inode,
6546 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006547 break;
6548 default:
6549 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6550 rpc_restart_call_prepare(task);
6551 return;
6552 }
6553 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006554}
6555
6556static void nfs4_layoutcommit_release(void *calldata)
6557{
6558 struct nfs4_layoutcommit_data *data = calldata;
6559
Andy Adamsondb29c082011-07-30 20:52:38 -04006560 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006561 put_rpccred(data->cred);
6562 kfree(data);
6563}
6564
6565static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6566 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6567 .rpc_call_done = nfs4_layoutcommit_done,
6568 .rpc_release = nfs4_layoutcommit_release,
6569};
6570
6571int
Andy Adamsonef311532011-03-12 02:58:10 -05006572nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006573{
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6576 .rpc_argp = &data->args,
6577 .rpc_resp = &data->res,
6578 .rpc_cred = data->cred,
6579 };
6580 struct rpc_task_setup task_setup_data = {
6581 .task = &data->task,
6582 .rpc_client = NFS_CLIENT(data->args.inode),
6583 .rpc_message = &msg,
6584 .callback_ops = &nfs4_layoutcommit_ops,
6585 .callback_data = data,
6586 .flags = RPC_TASK_ASYNC,
6587 };
6588 struct rpc_task *task;
6589 int status = 0;
6590
6591 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6592 "lbw: %llu inode %lu\n",
6593 data->task.tk_pid, sync,
6594 data->args.lastbytewritten,
6595 data->args.inode->i_ino);
6596
Trond Myklebust9d12b212012-01-17 22:04:25 -05006597 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006598 task = rpc_run_task(&task_setup_data);
6599 if (IS_ERR(task))
6600 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006601 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006602 goto out;
6603 status = nfs4_wait_for_completion_rpc_task(task);
6604 if (status != 0)
6605 goto out;
6606 status = task->tk_status;
6607out:
6608 dprintk("%s: status %d\n", __func__, status);
6609 rpc_put_task(task);
6610 return status;
6611}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006612
6613static int
6614_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6615 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6616{
6617 struct nfs41_secinfo_no_name_args args = {
6618 .style = SECINFO_STYLE_CURRENT_FH,
6619 };
6620 struct nfs4_secinfo_res res = {
6621 .flavors = flavors,
6622 };
6623 struct rpc_message msg = {
6624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6625 .rpc_argp = &args,
6626 .rpc_resp = &res,
6627 };
6628 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6629}
6630
6631static int
6632nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6633 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6634{
6635 struct nfs4_exception exception = { };
6636 int err;
6637 do {
6638 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6639 switch (err) {
6640 case 0:
6641 case -NFS4ERR_WRONGSEC:
6642 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006643 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006644 default:
6645 err = nfs4_handle_exception(server, err, &exception);
6646 }
6647 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006648out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006649 return err;
6650}
6651
6652static int
6653nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6654 struct nfs_fsinfo *info)
6655{
6656 int err;
6657 struct page *page;
6658 rpc_authflavor_t flavor;
6659 struct nfs4_secinfo_flavors *flavors;
6660
6661 page = alloc_page(GFP_KERNEL);
6662 if (!page) {
6663 err = -ENOMEM;
6664 goto out;
6665 }
6666
6667 flavors = page_address(page);
6668 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6669
6670 /*
6671 * Fall back on "guess and check" method if
6672 * the server doesn't support SECINFO_NO_NAME
6673 */
6674 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6675 err = nfs4_find_root_sec(server, fhandle, info);
6676 goto out_freepage;
6677 }
6678 if (err)
6679 goto out_freepage;
6680
6681 flavor = nfs_find_best_sec(flavors);
6682 if (err == 0)
6683 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6684
6685out_freepage:
6686 put_page(page);
6687 if (err == -EACCES)
6688 return -EPERM;
6689out:
6690 return err;
6691}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006692
6693static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006694{
6695 int status;
6696 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006697 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006698 };
6699 struct nfs41_test_stateid_res res;
6700 struct rpc_message msg = {
6701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6702 .rpc_argp = &args,
6703 .rpc_resp = &res,
6704 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006705
Chuck Lever38527b12012-07-11 16:30:23 -04006706 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006707 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006708 nfs4_set_sequence_privileged(&args.seq_args);
6709 status = nfs4_call_sync_sequence(server->client, server, &msg,
6710 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006711 if (status != NFS_OK) {
6712 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006713 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006714 }
6715 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006716 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006717}
6718
Chuck Lever38527b12012-07-11 16:30:23 -04006719/**
6720 * nfs41_test_stateid - perform a TEST_STATEID operation
6721 *
6722 * @server: server / transport on which to perform the operation
6723 * @stateid: state ID to test
6724 *
6725 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6726 * Otherwise a negative NFS4ERR value is returned if the operation
6727 * failed or the state ID is not currently valid.
6728 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006729static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006730{
6731 struct nfs4_exception exception = { };
6732 int err;
6733 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006734 err = _nfs41_test_stateid(server, stateid);
6735 if (err != -NFS4ERR_DELAY)
6736 break;
6737 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006738 } while (exception.retry);
6739 return err;
6740}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006741
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006742static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006743{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006744 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006745 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006746 };
6747 struct nfs41_free_stateid_res res;
6748 struct rpc_message msg = {
6749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6750 .rpc_argp = &args,
6751 .rpc_resp = &res,
6752 };
Chuck Lever38527b12012-07-11 16:30:23 -04006753 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006754
Chuck Lever38527b12012-07-11 16:30:23 -04006755 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006756 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006757 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006758 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006759 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006760 dprintk("NFS reply free_stateid: %d\n", status);
6761 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006762}
6763
Chuck Lever38527b12012-07-11 16:30:23 -04006764/**
6765 * nfs41_free_stateid - perform a FREE_STATEID operation
6766 *
6767 * @server: server / transport on which to perform the operation
6768 * @stateid: state ID to release
6769 *
6770 * Returns NFS_OK if the server freed "stateid". Otherwise a
6771 * negative NFS4ERR value is returned.
6772 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006773static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006774{
6775 struct nfs4_exception exception = { };
6776 int err;
6777 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006778 err = _nfs4_free_stateid(server, stateid);
6779 if (err != -NFS4ERR_DELAY)
6780 break;
6781 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006782 } while (exception.retry);
6783 return err;
6784}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006785
6786static bool nfs41_match_stateid(const nfs4_stateid *s1,
6787 const nfs4_stateid *s2)
6788{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006789 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006790 return false;
6791
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006792 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006793 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006794 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006795 return true;
6796
6797 return false;
6798}
6799
Andy Adamson557134a2009-04-01 09:21:53 -04006800#endif /* CONFIG_NFS_V4_1 */
6801
Trond Myklebust36281ca2012-03-04 18:13:56 -05006802static bool nfs4_match_stateid(const nfs4_stateid *s1,
6803 const nfs4_stateid *s2)
6804{
Trond Myklebustf597c532012-03-04 18:13:56 -05006805 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006806}
6807
6808
Trond Myklebust17280172012-03-11 13:11:00 -04006809static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006810 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006811 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 .recover_open = nfs4_open_reclaim,
6813 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006814 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006815 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006816 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817};
6818
Andy Adamson591d71c2009-04-01 09:22:47 -04006819#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006820static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006821 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6822 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6823 .recover_open = nfs4_open_reclaim,
6824 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006825 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006826 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006827 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006828 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006829};
6830#endif /* CONFIG_NFS_V4_1 */
6831
Trond Myklebust17280172012-03-11 13:11:00 -04006832static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006833 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006834 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 .recover_open = nfs4_open_expired,
6836 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006837 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006838 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839};
6840
Andy Adamson591d71c2009-04-01 09:22:47 -04006841#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006842static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006843 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6844 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006845 .recover_open = nfs41_open_expired,
6846 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006847 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006848 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006849};
6850#endif /* CONFIG_NFS_V4_1 */
6851
Trond Myklebust17280172012-03-11 13:11:00 -04006852static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006853 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006854 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006855 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006856};
6857
6858#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006859static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006860 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006861 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006862 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006863};
6864#endif
6865
Trond Myklebust97dc1352010-06-16 09:52:26 -04006866static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6867 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006868 .init_caps = NFS_CAP_READDIRPLUS
6869 | NFS_CAP_ATOMIC_OPEN
6870 | NFS_CAP_CHANGE_ATTR
6871 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006872 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006873 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006874 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006875 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6876 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6877 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006878};
6879
6880#if defined(CONFIG_NFS_V4_1)
6881static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6882 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006883 .init_caps = NFS_CAP_READDIRPLUS
6884 | NFS_CAP_ATOMIC_OPEN
6885 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006886 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006887 | NFS_CAP_STATEID_NFSV41
6888 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006889 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006890 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006891 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006892 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6893 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6894 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006895};
6896#endif
6897
Trond Myklebust97dc1352010-06-16 09:52:26 -04006898const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6899 [0] = &nfs_v4_0_minor_ops,
6900#if defined(CONFIG_NFS_V4_1)
6901 [1] = &nfs_v4_1_minor_ops,
6902#endif
6903};
6904
Bryan Schumaker73a79702012-07-16 16:39:12 -04006905const struct inode_operations nfs4_dir_inode_operations = {
6906 .create = nfs_create,
6907 .lookup = nfs_lookup,
6908 .atomic_open = nfs_atomic_open,
6909 .link = nfs_link,
6910 .unlink = nfs_unlink,
6911 .symlink = nfs_symlink,
6912 .mkdir = nfs_mkdir,
6913 .rmdir = nfs_rmdir,
6914 .mknod = nfs_mknod,
6915 .rename = nfs_rename,
6916 .permission = nfs_permission,
6917 .getattr = nfs_getattr,
6918 .setattr = nfs_setattr,
6919 .getxattr = generic_getxattr,
6920 .setxattr = generic_setxattr,
6921 .listxattr = generic_listxattr,
6922 .removexattr = generic_removexattr,
6923};
6924
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006925static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006926 .permission = nfs_permission,
6927 .getattr = nfs_getattr,
6928 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006929 .getxattr = generic_getxattr,
6930 .setxattr = generic_setxattr,
6931 .listxattr = generic_listxattr,
6932 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006933};
6934
David Howells509de812006-08-22 20:06:11 -04006935const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 .version = 4, /* protocol version */
6937 .dentry_ops = &nfs4_dentry_operations,
6938 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006939 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006940 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006942 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006943 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 .getattr = nfs4_proc_getattr,
6945 .setattr = nfs4_proc_setattr,
6946 .lookup = nfs4_proc_lookup,
6947 .access = nfs4_proc_access,
6948 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 .create = nfs4_proc_create,
6950 .remove = nfs4_proc_remove,
6951 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006952 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 .unlink_done = nfs4_proc_unlink_done,
6954 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006955 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006956 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006957 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 .link = nfs4_proc_link,
6959 .symlink = nfs4_proc_symlink,
6960 .mkdir = nfs4_proc_mkdir,
6961 .rmdir = nfs4_proc_remove,
6962 .readdir = nfs4_proc_readdir,
6963 .mknod = nfs4_proc_mknod,
6964 .statfs = nfs4_proc_statfs,
6965 .fsinfo = nfs4_proc_fsinfo,
6966 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006967 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 .decode_dirent = nfs4_decode_dirent,
6969 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006970 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006971 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006972 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006974 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006975 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006976 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006978 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006979 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006981 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006982 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006983 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006984 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006985 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006986 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006987 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006988 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006989 .create_server = nfs4_create_server,
6990 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991};
6992
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006993static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6994 .prefix = XATTR_NAME_NFSV4_ACL,
6995 .list = nfs4_xattr_list_nfs4_acl,
6996 .get = nfs4_xattr_get_nfs4_acl,
6997 .set = nfs4_xattr_set_nfs4_acl,
6998};
6999
7000const struct xattr_handler *nfs4_xattr_handlers[] = {
7001 &nfs4_xattr_nfs4_acl_handler,
7002 NULL
7003};
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005/*
7006 * Local variables:
7007 * c-basic-offset: 8
7008 * End:
7009 */