blob: 982b4527551d136fae8db1e6ee06e9ef1ccb8526 [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 Myklebustbdeca1b2013-04-23 14:52:44 -0400772 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100773 int rpc_status;
774 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775};
776
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400777static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
778 int err, struct nfs4_exception *exception)
779{
780 if (err != -EINVAL)
781 return false;
782 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
783 return false;
784 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
785 exception->retry = 1;
786 return true;
787}
788
789static enum open_claim_type4
790nfs4_map_atomic_open_claim(struct nfs_server *server,
791 enum open_claim_type4 claim)
792{
793 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
794 return claim;
795 switch (claim) {
796 default:
797 return claim;
798 case NFS4_OPEN_CLAIM_FH:
799 return NFS4_OPEN_CLAIM_NULL;
800 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
801 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
802 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
804 }
805}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400806
807static void nfs4_init_opendata_res(struct nfs4_opendata *p)
808{
809 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400810 p->o_res.seqid = p->o_arg.seqid;
811 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400813 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500815 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816}
817
Al Viro82a2c1b2011-06-22 18:30:55 -0400818static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400820 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400821 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400822 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Al Viro82a2c1b2011-06-22 18:30:55 -0400824 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct inode *dir = parent->d_inode;
826 struct nfs_server *server = NFS_SERVER(dir);
827 struct nfs4_opendata *p;
828
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400829 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (p == NULL)
831 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400832 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 if (p->o_arg.seqid == NULL)
834 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400835 nfs_sb_active(dentry->d_sb);
836 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->dir = parent;
838 p->owner = sp;
839 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 p->o_arg.open_flags = flags;
841 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700842 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
843 * will return permission denied for all bits until close */
844 if (!(flags & O_EXCL)) {
845 /* ask server to check for all possible rights as results
846 * are cached */
847 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
848 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700849 }
David Howells7539bba2006-08-22 20:06:09 -0400850 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400851 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
852 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 p->o_arg.server = server;
855 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400856 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400857 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
858 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400859 case NFS4_OPEN_CLAIM_NULL:
860 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
861 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
862 p->o_arg.fh = NFS_FH(dir);
863 break;
864 case NFS4_OPEN_CLAIM_PREVIOUS:
865 case NFS4_OPEN_CLAIM_FH:
866 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
867 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
868 p->o_arg.fh = NFS_FH(dentry->d_inode);
869 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500870 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500871 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400872
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873 p->o_arg.u.attrs = &p->attrs;
874 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500875
876 verf[0] = jiffies;
877 verf[1] = current->pid;
878 memcpy(p->o_arg.u.verifier.data, verf,
879 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100881 p->c_arg.fh = &p->o_res.fh;
882 p->c_arg.stateid = &p->o_res.stateid;
883 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400884 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400885 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 return p;
887err_free:
888 kfree(p);
889err:
890 dput(parent);
891 return NULL;
892}
893
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400896 struct nfs4_opendata *p = container_of(kref,
897 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400898 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899
900 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 if (p->state != NULL)
902 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 nfs4_put_state_owner(p->owner);
904 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400905 dput(p->dentry);
906 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500907 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 kfree(p);
909}
910
911static void nfs4_opendata_put(struct nfs4_opendata *p)
912{
913 if (p != NULL)
914 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915}
916
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
918{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919 int ret;
920
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 return ret;
923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400926{
927 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928
Trond Myklebust536e43d2012-01-17 22:04:26 -0500929 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 goto out;
931 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500933 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
934 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 break;
936 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500937 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
938 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939 break;
940 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500941 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
942 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 return ret;
946}
947
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500950 if (delegation == NULL)
951 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500954 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500956 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
957 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500958 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 return 1;
960}
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100963{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100965 case FMODE_WRITE:
966 state->n_wronly++;
967 break;
968 case FMODE_READ:
969 state->n_rdonly++;
970 break;
971 case FMODE_READ|FMODE_WRITE:
972 state->n_rdwr++;
973 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400978{
979 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500980 nfs4_stateid_copy(&state->stateid, stateid);
981 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400982 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 case FMODE_READ:
985 set_bit(NFS_O_RDONLY_STATE, &state->flags);
986 break;
987 case FMODE_WRITE:
988 set_bit(NFS_O_WRONLY_STATE, &state->flags);
989 break;
990 case FMODE_READ|FMODE_WRITE:
991 set_bit(NFS_O_RDWR_STATE, &state->flags);
992 }
993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400996{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001000}
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static 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 -07001003{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001004 /*
1005 * Protect the call to nfs4_state_set_mode_locked and
1006 * serialise the stateid update
1007 */
1008 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001010 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 set_bit(NFS_DELEGATED_STATE, &state->flags);
1012 }
1013 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
1016 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001018 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static 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 -05001022{
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *deleg_cur;
1025 int ret = 0;
1026
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 rcu_read_lock();
1030 deleg_cur = rcu_dereference(nfsi->delegation);
1031 if (deleg_cur == NULL)
1032 goto no_delegation;
1033
1034 spin_lock(&deleg_cur->lock);
1035 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001036 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001038 goto no_delegation_unlock;
1039
1040 if (delegation == NULL)
1041 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001042 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001043 goto no_delegation_unlock;
1044
Trond Myklebustb7391f42008-12-23 15:21:52 -05001045 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001047 ret = 1;
1048no_delegation_unlock:
1049 spin_unlock(&deleg_cur->lock);
1050no_delegation:
1051 rcu_read_unlock();
1052
1053 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001055 ret = 1;
1056 }
1057
1058 return ret;
1059}
1060
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063{
1064 struct nfs_delegation *delegation;
1065
1066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 rcu_read_unlock();
1070 return;
1071 }
1072 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001073 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074}
1075
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077{
1078 struct nfs4_state *state = opendata->state;
1079 struct nfs_inode *nfsi = NFS_I(state->inode);
1080 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001081 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 nfs4_stateid stateid;
1084 int ret = -EAGAIN;
1085
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
1090 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092 goto out_return_state;
1093 }
1094 spin_unlock(&state->owner->so_lock);
1095 }
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_lock();
1097 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001098 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001099 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001101 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001103 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001105 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001106 if (!opendata->is_recover) {
1107 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1108 if (ret != 0)
1109 goto out;
1110 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001112
1113 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001114 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001115 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117out:
1118 return ERR_PTR(ret);
1119out_return_state:
1120 atomic_inc(&state->count);
1121 return state;
1122}
1123
Andy Adamsone23008e2012-10-02 21:07:32 -04001124static void
1125nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1126{
1127 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1128 struct nfs_delegation *delegation;
1129 int delegation_flags = 0;
1130
1131 rcu_read_lock();
1132 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1133 if (delegation)
1134 delegation_flags = delegation->flags;
1135 rcu_read_unlock();
1136 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1137 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1138 "returning a delegation for "
1139 "OPEN(CLAIM_DELEGATE_CUR)\n",
1140 clp->cl_hostname);
1141 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1142 nfs_inode_set_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145 else
1146 nfs_inode_reclaim_delegation(state->inode,
1147 data->owner->so_cred,
1148 &data->o_res);
1149}
1150
1151/*
1152 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1153 * and update the nfs4_state.
1154 */
1155static struct nfs4_state *
1156_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1157{
1158 struct inode *inode = data->state->inode;
1159 struct nfs4_state *state = data->state;
1160 int ret;
1161
1162 if (!data->rpc_done) {
1163 ret = data->rpc_status;
1164 goto err;
1165 }
1166
1167 ret = -ESTALE;
1168 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1169 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1170 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1171 goto err;
1172
1173 ret = -ENOMEM;
1174 state = nfs4_get_open_state(inode, data->owner);
1175 if (state == NULL)
1176 goto err;
1177
1178 ret = nfs_refresh_inode(inode, &data->f_attr);
1179 if (ret)
1180 goto err;
1181
1182 if (data->o_res.delegation_type != 0)
1183 nfs4_opendata_check_deleg(data, state);
1184 update_open_stateid(state, &data->o_res.stateid, NULL,
1185 data->o_arg.fmode);
1186
1187 return state;
1188err:
1189 return ERR_PTR(ret);
1190
1191}
1192
1193static struct nfs4_state *
1194_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001195{
1196 struct inode *inode;
1197 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199
Trond Myklebustaac00a82007-07-05 19:02:21 -04001200 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001201 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 goto out;
1203 }
1204
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001205 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001206 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001207 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001209 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001210 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 goto err;
1212 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001213 state = nfs4_get_open_state(inode, data->owner);
1214 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001216 if (data->o_res.delegation_type != 0)
1217 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001218 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001220 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001222 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001223 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001224err_put_inode:
1225 iput(inode);
1226err:
1227 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001228}
1229
Andy Adamsone23008e2012-10-02 21:07:32 -04001230static struct nfs4_state *
1231nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1232{
1233 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1234 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1235 return _nfs4_opendata_to_nfs4_state(data);
1236}
1237
Trond Myklebust864472e2006-01-03 09:55:15 +01001238static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1239{
1240 struct nfs_inode *nfsi = NFS_I(state->inode);
1241 struct nfs_open_context *ctx;
1242
1243 spin_lock(&state->inode->i_lock);
1244 list_for_each_entry(ctx, &nfsi->open_files, list) {
1245 if (ctx->state != state)
1246 continue;
1247 get_nfs_open_context(ctx);
1248 spin_unlock(&state->inode->i_lock);
1249 return ctx;
1250 }
1251 spin_unlock(&state->inode->i_lock);
1252 return ERR_PTR(-ENOENT);
1253}
1254
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1256 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001257{
1258 struct nfs4_opendata *opendata;
1259
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001260 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1261 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001262 if (opendata == NULL)
1263 return ERR_PTR(-ENOMEM);
1264 opendata->state = state;
1265 atomic_inc(&state->count);
1266 return opendata;
1267}
1268
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001270{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001271 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 int ret;
1273
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274 opendata->o_arg.open_flags = 0;
1275 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001276 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1277 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1278 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001279 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 if (ret != 0)
1281 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001282 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001283 if (IS_ERR(newstate))
1284 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001285 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001286 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 return 0;
1288}
1289
1290static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1291{
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 int ret;
1294
1295 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001296 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001297 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 smp_rmb();
1299 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001300 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001301 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 if (ret != 0)
1303 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001304 if (newstate != state)
1305 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 }
1307 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001308 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001309 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 if (ret != 0)
1311 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001312 if (newstate != state)
1313 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 }
1315 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001316 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001317 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 if (ret != 0)
1319 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001320 if (newstate != state)
1321 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001322 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001323 /*
1324 * We may have performed cached opens for all three recoveries.
1325 * Check if we need to update the current stateid.
1326 */
1327 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001328 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001329 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001330 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001331 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001332 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001333 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 return 0;
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/*
1338 * OPEN_RECLAIM:
1339 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001341static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001343 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001344 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int status;
1347
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001348 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1349 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001350 if (IS_ERR(opendata))
1351 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001352 rcu_read_lock();
1353 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001354 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001355 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001356 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 opendata->o_arg.u.delegation_type = delegation_type;
1358 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001359 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return status;
1361}
1362
Trond Myklebust539cd032007-06-05 11:46:42 -04001363static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct nfs_server *server = NFS_SERVER(state->inode);
1366 struct nfs4_exception exception = { };
1367 int err;
1368 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001369 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001370 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1371 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001372 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001373 break;
1374 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 } while (exception.retry);
1376 return err;
1377}
1378
Trond Myklebust864472e2006-01-03 09:55:15 +01001379static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1380{
1381 struct nfs_open_context *ctx;
1382 int ret;
1383
1384 ctx = nfs4_state_find_open_context(state);
1385 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001386 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001387 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001388 put_nfs_open_context(ctx);
1389 return ret;
1390}
1391
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001392static 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 -07001393{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001394 switch (err) {
1395 default:
1396 printk(KERN_ERR "NFS: %s: unhandled error "
1397 "%d.\n", __func__, err);
1398 case 0:
1399 case -ENOENT:
1400 case -ESTALE:
1401 break;
1402 case -NFS4ERR_BADSESSION:
1403 case -NFS4ERR_BADSLOT:
1404 case -NFS4ERR_BAD_HIGH_SLOT:
1405 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1406 case -NFS4ERR_DEADSESSION:
1407 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1409 return -EAGAIN;
1410 case -NFS4ERR_STALE_CLIENTID:
1411 case -NFS4ERR_STALE_STATEID:
1412 set_bit(NFS_DELEGATED_STATE, &state->flags);
1413 case -NFS4ERR_EXPIRED:
1414 /* Don't recall a delegation if it was lost */
1415 nfs4_schedule_lease_recovery(server->nfs_client);
1416 return -EAGAIN;
1417 case -NFS4ERR_DELEG_REVOKED:
1418 case -NFS4ERR_ADMIN_REVOKED:
1419 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001420 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001421 nfs_inode_find_state_and_recover(state->inode,
1422 stateid);
1423 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001424 return 0;
1425 case -NFS4ERR_DELAY:
1426 case -NFS4ERR_GRACE:
1427 set_bit(NFS_DELEGATED_STATE, &state->flags);
1428 ssleep(1);
1429 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001430 case -ENOMEM:
1431 case -NFS4ERR_DENIED:
1432 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1433 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return err;
1436}
1437
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001438int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1439{
1440 struct nfs_server *server = NFS_SERVER(state->inode);
1441 struct nfs4_opendata *opendata;
1442 int err;
1443
1444 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1445 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1446 if (IS_ERR(opendata))
1447 return PTR_ERR(opendata);
1448 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1449 err = nfs4_open_recover(opendata, state);
1450 nfs4_opendata_put(opendata);
1451 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1452}
1453
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001454static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1455{
1456 struct nfs4_opendata *data = calldata;
1457
1458 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001460 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001461 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001462 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001463 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001464 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001465}
1466
1467static void nfs4_open_confirm_release(void *calldata)
1468{
1469 struct nfs4_opendata *data = calldata;
1470 struct nfs4_state *state = NULL;
1471
1472 /* If this request hasn't been cancelled, do nothing */
1473 if (data->cancelled == 0)
1474 goto out_free;
1475 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001476 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001478 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001479 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001480 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001481out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001482 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001483}
1484
1485static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001486 .rpc_call_done = nfs4_open_confirm_done,
1487 .rpc_release = nfs4_open_confirm_release,
1488};
1489
1490/*
1491 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1492 */
1493static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1494{
1495 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1496 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001497 struct rpc_message msg = {
1498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1499 .rpc_argp = &data->c_arg,
1500 .rpc_resp = &data->c_res,
1501 .rpc_cred = data->owner->so_cred,
1502 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001503 struct rpc_task_setup task_setup_data = {
1504 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001505 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001506 .callback_ops = &nfs4_open_confirm_ops,
1507 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001508 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001509 .flags = RPC_TASK_ASYNC,
1510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int status;
1512
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001513 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001514 data->rpc_done = 0;
1515 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001516 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001517 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001518 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001519 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001520 status = nfs4_wait_for_completion_rpc_task(task);
1521 if (status != 0) {
1522 data->cancelled = 1;
1523 smp_wmb();
1524 } else
1525 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001526 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return status;
1528}
1529
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 struct nfs4_opendata *data = calldata;
1533 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001534 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001535
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001537 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001538 /*
1539 * Check if we still need to send an OPEN call, or if we can use
1540 * a delegation instead.
1541 */
1542 if (data->state != NULL) {
1543 struct nfs_delegation *delegation;
1544
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001546 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001547 rcu_read_lock();
1548 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001549 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001550 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001551 can_open_delegated(delegation, data->o_arg.fmode))
1552 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001553 rcu_read_unlock();
1554 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001555 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001556 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001557 switch (data->o_arg.claim) {
1558 case NFS4_OPEN_CLAIM_PREVIOUS:
1559 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1560 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001561 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001562 case NFS4_OPEN_CLAIM_FH:
1563 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001564 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1565 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001566 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001567 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001568 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001569 &data->o_res.seq_res,
1570 task) != 0)
1571 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001572
1573 /* Set the create mode (note dependency on the session type) */
1574 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1575 if (data->o_arg.open_flags & O_EXCL) {
1576 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1577 if (nfs4_has_persistent_session(clp))
1578 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1579 else if (clp->cl_mvops->minor_version > 0)
1580 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1581 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001582 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001583unlock_no_action:
1584 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001585out_no_action:
1586 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001587out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001588 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589}
1590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591static void nfs4_open_done(struct rpc_task *task, void *calldata)
1592{
1593 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001596
Trond Myklebust14516c32010-07-31 14:29:06 -04001597 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1598 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001601 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1602 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001603 case S_IFREG:
1604 break;
1605 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001607 break;
1608 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001610 break;
1611 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001613 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001614 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001615 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001616 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1617 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001618 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001619 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001621
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622static void nfs4_open_release(void *calldata)
1623{
1624 struct nfs4_opendata *data = calldata;
1625 struct nfs4_state *state = NULL;
1626
1627 /* If this request hasn't been cancelled, do nothing */
1628 if (data->cancelled == 0)
1629 goto out_free;
1630 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001631 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 goto out_free;
1633 /* In case we need an open_confirm, no cleanup! */
1634 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1635 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001637 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001638 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001640 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641}
1642
1643static const struct rpc_call_ops nfs4_open_ops = {
1644 .rpc_call_prepare = nfs4_open_prepare,
1645 .rpc_call_done = nfs4_open_done,
1646 .rpc_release = nfs4_open_release,
1647};
1648
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001649static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001650{
1651 struct inode *dir = data->dir->d_inode;
1652 struct nfs_server *server = NFS_SERVER(dir);
1653 struct nfs_openargs *o_arg = &data->o_arg;
1654 struct nfs_openres *o_res = &data->o_res;
1655 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001656 struct rpc_message msg = {
1657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1658 .rpc_argp = o_arg,
1659 .rpc_resp = o_res,
1660 .rpc_cred = data->owner->so_cred,
1661 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001662 struct rpc_task_setup task_setup_data = {
1663 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001664 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001665 .callback_ops = &nfs4_open_ops,
1666 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001667 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 .flags = RPC_TASK_ASYNC,
1669 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001670 int status;
1671
Trond Myklebust9d12b212012-01-17 22:04:25 -05001672 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001673 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001674 data->rpc_done = 0;
1675 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001676 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001677 data->is_recover = 0;
1678 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001679 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001680 data->is_recover = 1;
1681 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001682 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001683 if (IS_ERR(task))
1684 return PTR_ERR(task);
1685 status = nfs4_wait_for_completion_rpc_task(task);
1686 if (status != 0) {
1687 data->cancelled = 1;
1688 smp_wmb();
1689 } else
1690 status = data->rpc_status;
1691 rpc_put_task(task);
1692
1693 return status;
1694}
1695
1696static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1697{
1698 struct inode *dir = data->dir->d_inode;
1699 struct nfs_openres *o_res = &data->o_res;
1700 int status;
1701
1702 status = nfs4_run_open_task(data, 1);
1703 if (status != 0 || !data->rpc_done)
1704 return status;
1705
Trond Myklebust6926afd2012-01-07 13:22:46 -05001706 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1707
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001708 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1709 status = _nfs4_proc_open_confirm(data);
1710 if (status != 0)
1711 return status;
1712 }
1713
1714 return status;
1715}
1716
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001717static int nfs4_opendata_access(struct rpc_cred *cred,
1718 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001719 struct nfs4_state *state, fmode_t fmode,
1720 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001721{
1722 struct nfs_access_entry cache;
1723 u32 mask;
1724
1725 /* access call failed or for some reason the server doesn't
1726 * support any access modes -- defer access call until later */
1727 if (opendata->o_res.access_supported == 0)
1728 return 0;
1729
1730 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001731 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001732 * write mode bits, but POSIX allows the creating process to write.
1733 * use openflags to check for exec, because fmode won't
1734 * always have FMODE_EXEC set when file open for exec. */
1735 if (openflags & __FMODE_EXEC) {
1736 /* ONLY check for exec rights */
1737 mask = MAY_EXEC;
1738 } else if (fmode & FMODE_READ)
1739 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001740
1741 cache.cred = cred;
1742 cache.jiffies = jiffies;
1743 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1744 nfs_access_add_cache(state->inode, &cache);
1745
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001746 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001747 return 0;
1748
1749 /* even though OPEN succeeded, access is denied. Close the file */
1750 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001751 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001752}
1753
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001754/*
1755 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1756 */
1757static int _nfs4_proc_open(struct nfs4_opendata *data)
1758{
1759 struct inode *dir = data->dir->d_inode;
1760 struct nfs_server *server = NFS_SERVER(dir);
1761 struct nfs_openargs *o_arg = &data->o_arg;
1762 struct nfs_openres *o_res = &data->o_res;
1763 int status;
1764
1765 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001766 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001768 if (status != 0) {
1769 if (status == -NFS4ERR_BADNAME &&
1770 !(o_arg->open_flags & O_CREAT))
1771 return -ENOENT;
1772 return status;
1773 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774
Trond Myklebust6926afd2012-01-07 13:22:46 -05001775 nfs_fattr_map_and_free_names(server, &data->f_attr);
1776
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001777 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001778 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001779 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1780 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001782 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001787 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Andy Adamsond83217c2011-03-01 01:34:17 +00001791static int nfs4_recover_expired_lease(struct nfs_server *server)
1792{
1793 return nfs4_client_recover_expired_lease(server->nfs_client);
1794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
1797 * OPEN_EXPIRED:
1798 * reclaim state on the server after a network partition.
1799 * Assumes caller holds the appropriate lock
1800 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001801static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001803 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001806 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001807 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001808 if (IS_ERR(opendata))
1809 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001810 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001811 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001812 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001813 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001817static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001818{
Trond Myklebust539cd032007-06-05 11:46:42 -04001819 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001820 struct nfs4_exception exception = { };
1821 int err;
1822
1823 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001824 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001825 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1826 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001827 switch (err) {
1828 default:
1829 goto out;
1830 case -NFS4ERR_GRACE:
1831 case -NFS4ERR_DELAY:
1832 nfs4_handle_exception(server, err, &exception);
1833 err = 0;
1834 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001835 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001836out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001837 return err;
1838}
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001843 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 ctx = nfs4_state_find_open_context(state);
1846 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001847 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001848 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001849 put_nfs_open_context(ctx);
1850 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001853#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001854static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001855{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001856 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001857 nfs4_stateid *stateid = &state->stateid;
1858 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001859
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001860 /* If a state reset has been done, test_stateid is unneeded */
1861 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1862 return;
1863
1864 status = nfs41_test_stateid(server, stateid);
1865 if (status != NFS_OK) {
1866 /* Free the stateid unless the server explicitly
1867 * informs us the stateid is unrecognized. */
1868 if (status != -NFS4ERR_BAD_STATEID)
1869 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001870 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001871
Bryan Schumaker69388672012-09-26 15:25:52 -04001872 write_seqlock(&state->seqlock);
1873 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1874 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001875 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1876 }
1877}
1878
1879/**
1880 * nfs41_check_open_stateid - possibly free an open stateid
1881 *
1882 * @state: NFSv4 state for an inode
1883 *
1884 * Returns NFS_OK if recovery for this stateid is now finished.
1885 * Otherwise a negative NFS4ERR value is returned.
1886 */
1887static int nfs41_check_open_stateid(struct nfs4_state *state)
1888{
1889 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001890 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001891 int status;
1892
1893 /* If a state reset has been done, test_stateid is unneeded */
1894 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1895 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1896 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1897 return -NFS4ERR_BAD_STATEID;
1898
1899 status = nfs41_test_stateid(server, stateid);
1900 if (status != NFS_OK) {
1901 /* Free the stateid unless the server explicitly
1902 * informs us the stateid is unrecognized. */
1903 if (status != -NFS4ERR_BAD_STATEID)
1904 nfs41_free_stateid(server, stateid);
1905
1906 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1907 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1908 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001909 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001910 }
1911 return status;
1912}
1913
1914static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1915{
Chuck Levereb64cf92012-07-11 16:30:05 -04001916 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001917
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001918 nfs41_clear_delegation_stateid(state);
1919 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001920 if (status != NFS_OK)
1921 status = nfs4_open_expired(sp, state);
1922 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001923}
1924#endif
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001927 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1928 * fields corresponding to attributes that were used to store the verifier.
1929 * Make sure we clobber those fields in the later setattr call
1930 */
1931static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1932{
1933 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1934 !(sattr->ia_valid & ATTR_ATIME_SET))
1935 sattr->ia_valid |= ATTR_ATIME;
1936
1937 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1938 !(sattr->ia_valid & ATTR_MTIME_SET))
1939 sattr->ia_valid |= ATTR_MTIME;
1940}
1941
Trond Myklebustc21443c2013-02-07 14:26:21 -05001942static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1943 fmode_t fmode,
1944 int flags,
1945 struct nfs4_state **res)
1946{
1947 struct nfs4_state_owner *sp = opendata->owner;
1948 struct nfs_server *server = sp->so_server;
1949 struct nfs4_state *state;
1950 unsigned int seq;
1951 int ret;
1952
1953 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1954
1955 ret = _nfs4_proc_open(opendata);
1956 if (ret != 0)
1957 goto out;
1958
1959 state = nfs4_opendata_to_nfs4_state(opendata);
1960 ret = PTR_ERR(state);
1961 if (IS_ERR(state))
1962 goto out;
1963 if (server->caps & NFS_CAP_POSIX_LOCK)
1964 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1965
1966 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1967 if (ret != 0)
1968 goto out;
1969
Trond Myklebust91876b12013-03-28 14:01:33 -04001970 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001971 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001972 *res = state;
1973out:
1974 return ret;
1975}
1976
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001977/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001978 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Andy Adamson82be4172012-05-23 05:02:35 -04001980static int _nfs4_do_open(struct inode *dir,
1981 struct dentry *dentry,
1982 fmode_t fmode,
1983 int flags,
1984 struct iattr *sattr,
1985 struct rpc_cred *cred,
1986 struct nfs4_state **res,
1987 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct nfs4_state_owner *sp;
1990 struct nfs4_state *state = NULL;
1991 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001992 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001993 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001994 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001998 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1999 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2001 goto out_err;
2002 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002003 status = nfs4_recover_expired_lease(server);
2004 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002005 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002006 if (dentry->d_inode != NULL)
2007 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002008 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002009 if (dentry->d_inode)
2010 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002011 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002012 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002013 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002014 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Andy Adamson82be4172012-05-23 05:02:35 -04002016 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2017 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2018 if (!opendata->f_attr.mdsthreshold)
2019 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002020 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002021 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002022 if (dentry->d_inode != NULL)
2023 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002024
Trond Myklebustc21443c2013-02-07 14:26:21 -05002025 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002026 if (status != 0)
2027 goto err_opendata_put;
2028
Trond Myklebust549b19c2013-04-16 18:42:34 -04002029 if ((opendata->o_arg.open_flags & O_EXCL) &&
2030 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002031 nfs4_exclusive_attrset(opendata, sattr);
2032
2033 nfs_fattr_init(opendata->o_res.f_attr);
2034 status = nfs4_do_setattr(state->inode, cred,
2035 opendata->o_res.f_attr, sattr,
2036 state);
2037 if (status == 0)
2038 nfs_setattr_update_inode(state->inode, sattr);
2039 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2040 }
Andy Adamson82be4172012-05-23 05:02:35 -04002041
2042 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2043 *ctx_th = opendata->f_attr.mdsthreshold;
2044 else
2045 kfree(opendata->f_attr.mdsthreshold);
2046 opendata->f_attr.mdsthreshold = NULL;
2047
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002048 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *res = state;
2051 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002052err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002053 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002054 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002055err_put_state_owner:
2056 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 *res = NULL;
2059 return status;
2060}
2061
2062
Andy Adamson82be4172012-05-23 05:02:35 -04002063static struct nfs4_state *nfs4_do_open(struct inode *dir,
2064 struct dentry *dentry,
2065 fmode_t fmode,
2066 int flags,
2067 struct iattr *sattr,
2068 struct rpc_cred *cred,
2069 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002071 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct nfs4_exception exception = { };
2073 struct nfs4_state *res;
2074 int status;
2075
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002076 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002078 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2079 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (status == 0)
2081 break;
2082 /* NOTE: BAD_SEQID means the server and client disagree about the
2083 * book-keeping w.r.t. state-changing operations
2084 * (OPEN/CLOSE/LOCK/LOCKU...)
2085 * It is actually a sign of a bug on the client or on the server.
2086 *
2087 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002088 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * have unhashed the old state_owner for us, and that we can
2090 * therefore safely retry using a new one. We should still warn
2091 * the user though...
2092 */
2093 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002094 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002095 " returned a bad sequence-id error!\n",
2096 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 exception.retry = 1;
2098 continue;
2099 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002100 /*
2101 * BAD_STATEID on OPEN means that the server cancelled our
2102 * state before it received the OPEN_CONFIRM.
2103 * Recover by retrying the request as per the discussion
2104 * on Page 181 of RFC3530.
2105 */
2106 if (status == -NFS4ERR_BAD_STATEID) {
2107 exception.retry = 1;
2108 continue;
2109 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002110 if (status == -EAGAIN) {
2111 /* We must have found a delegation */
2112 exception.retry = 1;
2113 continue;
2114 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002115 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2116 continue;
2117 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 status, &exception));
2119 } while (exception.retry);
2120 return res;
2121}
2122
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002123static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2124 struct nfs_fattr *fattr, struct iattr *sattr,
2125 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002127 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002129 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 .iap = sattr,
2131 .server = server,
2132 .bitmask = server->attr_bitmask,
2133 };
2134 struct nfs_setattrres res = {
2135 .fattr = fattr,
2136 .server = server,
2137 };
2138 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2140 .rpc_argp = &arg,
2141 .rpc_resp = &res,
2142 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002144 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002145 fmode_t fmode;
2146 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002147 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Trond Myklebust0e574af2005-10-27 22:12:38 -04002149 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Trond Myklebustee3ae842013-04-29 10:35:36 -04002151 /* Servers should only apply open mode checks for file size changes */
2152 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2153 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2154
2155 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2156 /* Use that stateid */
2157 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002158 struct nfs_lockowner lockowner = {
2159 .l_owner = current->files,
2160 .l_pid = current->tgid,
2161 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002162 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002163 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002164 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002165 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Bryan Schumaker7c513052011-03-24 17:12:24 +00002167 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002168 if (status == 0 && state != NULL)
2169 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002170 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002173static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2174 struct nfs_fattr *fattr, struct iattr *sattr,
2175 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002177 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002178 struct nfs4_exception exception = {
2179 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002180 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int err;
2183 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002184 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2185 switch (err) {
2186 case -NFS4ERR_OPENMODE:
2187 if (state && !(state->state & FMODE_WRITE)) {
2188 err = -EBADF;
2189 if (sattr->ia_valid & ATTR_OPEN)
2190 err = -EACCES;
2191 goto out;
2192 }
2193 }
2194 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002196out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return err;
2198}
2199
2200struct nfs4_closedata {
2201 struct inode *inode;
2202 struct nfs4_state *state;
2203 struct nfs_closeargs arg;
2204 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002205 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002206 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002207 bool roc;
2208 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209};
2210
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002211static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002212{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002213 struct nfs4_closedata *calldata = data;
2214 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002215 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002216
Fred Isamanf7e89172011-01-06 11:36:32 +00002217 if (calldata->roc)
2218 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002219 nfs4_put_open_state(calldata->state);
2220 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002221 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002222 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002223 kfree(calldata);
2224}
2225
Trond Myklebust88069f72009-12-08 08:33:16 -05002226static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2227 fmode_t fmode)
2228{
2229 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002230 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002231 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2232 case FMODE_WRITE:
2233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2234 break;
2235 case FMODE_READ:
2236 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2237 break;
2238 case 0:
2239 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2241 clear_bit(NFS_OPEN_STATE, &state->flags);
2242 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002243 spin_unlock(&state->owner->so_lock);
2244}
2245
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002246static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002248 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 struct nfs_server *server = NFS_SERVER(calldata->inode);
2251
Chuck Levera3ca5652012-03-01 17:00:40 -05002252 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002253 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* hmm. we are done with the inode, and in the process of freeing
2256 * the state_owner. we keep this around to process errors
2257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 switch (task->tk_status) {
2259 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002260 if (calldata->roc)
2261 pnfs_roc_set_barrier(state->inode,
2262 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002263 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002264 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002265 nfs4_close_clear_stateid_flags(state,
2266 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 break;
2268 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002269 case -NFS4ERR_OLD_STATEID:
2270 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002272 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002275 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2276 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002278 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002279 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002280 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002283static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002285 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002286 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002287 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002289
Chuck Levera3ca5652012-03-01 17:00:40 -05002290 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002291 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002292 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002293
Trond Myklebust88069f72009-12-08 08:33:16 -05002294 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2295 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002296 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002297 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002298 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002299 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002300 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2301 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2302 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002303 }
2304 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002305 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2306 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2307 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002308 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002309 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002310 if (!nfs4_valid_open_stateid(state))
2311 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002312 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002313
2314 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002315 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002316 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002317 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002318
Fred Isamanf7e89172011-01-06 11:36:32 +00002319 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002320 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002321 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002322 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2323 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002324 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002325 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002326 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002327
Trond Myklebust516a6af2005-10-27 22:12:41 -04002328 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002329 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002330 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002331 &calldata->arg.seq_args,
2332 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002333 task) != 0)
2334 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002335 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002336 return;
2337out_no_action:
2338 task->tk_action = NULL;
2339out_wait:
2340 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002343static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002344 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002345 .rpc_call_done = nfs4_close_done,
2346 .rpc_release = nfs4_free_closedata,
2347};
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/*
2350 * It is possible for data to be read/written from a mem-mapped file
2351 * after the sys_close call (which hits the vfs layer as a flush).
2352 * This means that we can't safely call nfsv4 close on a file until
2353 * the inode is cleared. This in turn means that we are not good
2354 * NFSv4 citizens - we do not indicate to the server to update the file's
2355 * share state even when we are done with one of the three share
2356 * stateid's in the inode.
2357 *
2358 * NOTE: Caller must be holding the sp->so_owner semaphore!
2359 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002360int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002362 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002364 struct nfs4_state_owner *sp = state->owner;
2365 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2368 .rpc_cred = state->owner->so_cred,
2369 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002370 struct rpc_task_setup task_setup_data = {
2371 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002372 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002373 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002374 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 .flags = RPC_TASK_ASYNC,
2376 };
Trond Myklebust95121352005-10-18 14:20:12 -07002377 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002379 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002381 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002382 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002383 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002385 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002386 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002388 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002389 if (calldata->arg.seqid == NULL)
2390 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002391 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002392 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002393 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002394 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002395 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002396 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002397 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002398
Trond Myklebust1174dd12010-12-21 10:52:24 -05002399 msg.rpc_argp = &calldata->arg;
2400 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002401 task_setup_data.callback_data = calldata;
2402 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002403 if (IS_ERR(task))
2404 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002405 status = 0;
2406 if (wait)
2407 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002408 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002409 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002410out_free_calldata:
2411 kfree(calldata);
2412out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002413 nfs4_put_open_state(state);
2414 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002415 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Trond Myklebust2b484292010-09-17 10:56:51 -04002418static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002419nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct nfs4_state *state;
2422
Trond Myklebust565277f2007-10-15 18:17:53 -04002423 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002424 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2425 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002426 if (IS_ERR(state))
2427 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002428 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002429 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Trond Myklebust1185a552009-12-03 15:54:02 -05002432static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002433{
2434 if (ctx->state == NULL)
2435 return;
2436 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002437 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002438 else
Al Viro643168c2011-06-22 18:20:23 -04002439 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2443{
Benny Halevy43652ad2009-04-01 09:21:54 -04002444 struct nfs4_server_caps_arg args = {
2445 .fhandle = fhandle,
2446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct nfs4_server_caps_res res = {};
2448 struct rpc_message msg = {
2449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002450 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 .rpc_resp = &res,
2452 };
2453 int status;
2454
Bryan Schumaker7c513052011-03-24 17:12:24 +00002455 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (status == 0) {
2457 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002458 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2459 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2460 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2461 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2462 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2464 server->caps |= NFS_CAP_ACLS;
2465 if (res.has_links != 0)
2466 server->caps |= NFS_CAP_HARDLINKS;
2467 if (res.has_symlinks != 0)
2468 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002469 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2470 server->caps |= NFS_CAP_FILEID;
2471 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2472 server->caps |= NFS_CAP_MODE;
2473 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2474 server->caps |= NFS_CAP_NLINK;
2475 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2476 server->caps |= NFS_CAP_OWNER;
2477 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2478 server->caps |= NFS_CAP_OWNER_GROUP;
2479 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2480 server->caps |= NFS_CAP_ATIME;
2481 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2482 server->caps |= NFS_CAP_CTIME;
2483 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2484 server->caps |= NFS_CAP_MTIME;
2485
Trond Myklebusta65318b2009-03-11 14:10:28 -04002486 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2487 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2488 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002490 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return status;
2494}
2495
Trond Myklebust55a97592006-06-09 09:34:19 -04002496int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct nfs4_exception exception = { };
2499 int err;
2500 do {
2501 err = nfs4_handle_exception(server,
2502 _nfs4_server_capabilities(server, fhandle),
2503 &exception);
2504 } while (exception.retry);
2505 return err;
2506}
2507
2508static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2509 struct nfs_fsinfo *info)
2510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct nfs4_lookup_root_arg args = {
2512 .bitmask = nfs4_fattr_bitmap,
2513 };
2514 struct nfs4_lookup_res res = {
2515 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002516 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 .fh = fhandle,
2518 };
2519 struct rpc_message msg = {
2520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2521 .rpc_argp = &args,
2522 .rpc_resp = &res,
2523 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002524
Trond Myklebust0e574af2005-10-27 22:12:38 -04002525 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002526 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2530 struct nfs_fsinfo *info)
2531{
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002535 err = _nfs4_lookup_root(server, fhandle, info);
2536 switch (err) {
2537 case 0:
2538 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002539 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002540 default:
2541 err = nfs4_handle_exception(server, err, &exception);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002544out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return err;
2546}
2547
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002548static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2549 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2550{
2551 struct rpc_auth *auth;
2552 int ret;
2553
2554 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002555 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002556 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002557 goto out;
2558 }
2559 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002560out:
2561 return ret;
2562}
2563
Chuck Lever9a744ba2013-03-16 15:56:02 -04002564/*
2565 * Retry pseudoroot lookup with various security flavors. We do this when:
2566 *
2567 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2568 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2569 *
2570 * Returns zero on success, or a negative NFS4ERR value, or a
2571 * negative errno value.
2572 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002573static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002574 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002576 /* Per 3530bis 15.33.5 */
2577 static const rpc_authflavor_t flav_array[] = {
2578 RPC_AUTH_GSS_KRB5P,
2579 RPC_AUTH_GSS_KRB5I,
2580 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002581 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002582 RPC_AUTH_NULL,
2583 };
2584 int status = -EPERM;
2585 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Chuck Lever9a744ba2013-03-16 15:56:02 -04002587 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002588 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002589 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002590 continue;
2591 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002592 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002593
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002594 /*
2595 * -EACCESS could mean that the user doesn't have correct permissions
2596 * to access the mount. It could also mean that we tried to mount
2597 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2598 * existing mount programs don't handle -EACCES very well so it should
2599 * be mapped to -EPERM instead.
2600 */
2601 if (status == -EACCES)
2602 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002603 return status;
2604}
2605
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002606static int nfs4_do_find_root_sec(struct nfs_server *server,
2607 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2608{
2609 int mv = server->nfs_client->cl_minorversion;
2610 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2611}
2612
2613/**
2614 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2615 * @server: initialized nfs_server handle
2616 * @fhandle: we fill in the pseudo-fs root file handle
2617 * @info: we fill in an FSINFO struct
2618 *
2619 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002620 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002621int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2622 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002623{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002624 int status;
2625
2626 status = nfs4_lookup_root(server, fhandle, info);
2627 if ((status == -NFS4ERR_WRONGSEC) &&
2628 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2629 status = nfs4_do_find_root_sec(server, fhandle, info);
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (status == 0)
2632 status = nfs4_server_capabilities(server, fhandle);
2633 if (status == 0)
2634 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002635
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002636 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
Bryan Schumakerbae36242012-05-10 15:07:31 -04002639static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2640 struct nfs_fsinfo *info)
2641{
2642 int error;
2643 struct nfs_fattr *fattr = info->fattr;
2644
2645 error = nfs4_server_capabilities(server, mntfh);
2646 if (error < 0) {
2647 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2648 return error;
2649 }
2650
2651 error = nfs4_proc_getattr(server, mntfh, fattr);
2652 if (error < 0) {
2653 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2654 return error;
2655 }
2656
2657 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2658 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2659 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2660
2661 return error;
2662}
2663
Manoj Naik6b97fd32006-06-09 09:34:29 -04002664/*
2665 * Get locations and (maybe) other attributes of a referral.
2666 * Note that we'll actually follow the referral later when
2667 * we detect fsid mismatch in inode revalidation
2668 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002669static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2670 const struct qstr *name, struct nfs_fattr *fattr,
2671 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002672{
2673 int status = -ENOMEM;
2674 struct page *page = NULL;
2675 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002676
2677 page = alloc_page(GFP_KERNEL);
2678 if (page == NULL)
2679 goto out;
2680 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2681 if (locations == NULL)
2682 goto out;
2683
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002684 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002685 if (status != 0)
2686 goto out;
2687 /* Make sure server returned a different fsid for the referral */
2688 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002689 dprintk("%s: server did not return a different fsid for"
2690 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002691 status = -EIO;
2692 goto out;
2693 }
Andy Adamson533eb462011-06-13 18:25:56 -04002694 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2695 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002696
Andy Adamson533eb462011-06-13 18:25:56 -04002697 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002698 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002699 memset(fhandle, 0, sizeof(struct nfs_fh));
2700out:
2701 if (page)
2702 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002703 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002704 return status;
2705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2708{
2709 struct nfs4_getattr_arg args = {
2710 .fh = fhandle,
2711 .bitmask = server->attr_bitmask,
2712 };
2713 struct nfs4_getattr_res res = {
2714 .fattr = fattr,
2715 .server = server,
2716 };
2717 struct rpc_message msg = {
2718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2719 .rpc_argp = &args,
2720 .rpc_resp = &res,
2721 };
2722
Trond Myklebust0e574af2005-10-27 22:12:38 -04002723 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2728{
2729 struct nfs4_exception exception = { };
2730 int err;
2731 do {
2732 err = nfs4_handle_exception(server,
2733 _nfs4_proc_getattr(server, fhandle, fattr),
2734 &exception);
2735 } while (exception.retry);
2736 return err;
2737}
2738
2739/*
2740 * The file is not closed if it is opened due to the a request to change
2741 * the size of the file. The open call will not be needed once the
2742 * VFS layer lookup-intents are implemented.
2743 *
2744 * Close is called when the inode is destroyed.
2745 * If we haven't opened the file for O_WRONLY, we
2746 * need to in the size_change case to obtain a stateid.
2747 *
2748 * Got race?
2749 * Because OPEN is always done by name in nfsv4, it is
2750 * possible that we opened a different file by the same
2751 * name. We can recognize this race condition, but we
2752 * can't do anything about it besides returning an error.
2753 *
2754 * This will be fixed with VFS changes (lookup-intent).
2755 */
2756static int
2757nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2758 struct iattr *sattr)
2759{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002760 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002761 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002762 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 int status;
2764
Benny Halevy8a1636c2010-07-14 15:43:57 -04002765 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002766 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002767
Trond Myklebust0e574af2005-10-27 22:12:38 -04002768 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Andy Adamson26699402012-05-30 16:12:24 -04002770 /* Deal with open(O_TRUNC) */
2771 if (sattr->ia_valid & ATTR_OPEN)
2772 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2773
2774 /* Optimization: if the end result is no change, don't RPC */
2775 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2776 return 0;
2777
Trond Myklebustd5308382005-11-04 15:33:38 -05002778 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002779 if (sattr->ia_valid & ATTR_FILE) {
2780 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002781
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002782 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002783 if (ctx) {
2784 cred = ctx->cred;
2785 state = ctx->state;
2786 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002787 }
2788
2789 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002790 if (status == 0)
2791 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return status;
2793}
2794
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002795static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2796 const struct qstr *name, struct nfs_fh *fhandle,
2797 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002798{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002799 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002800 int status;
2801 struct nfs4_lookup_arg args = {
2802 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002803 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002804 .name = name,
2805 };
2806 struct nfs4_lookup_res res = {
2807 .server = server,
2808 .fattr = fattr,
2809 .fh = fhandle,
2810 };
2811 struct rpc_message msg = {
2812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2813 .rpc_argp = &args,
2814 .rpc_resp = &res,
2815 };
2816
2817 nfs_fattr_init(fattr);
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002820 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 dprintk("NFS reply lookup: %d\n", status);
2822 return status;
2823}
2824
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002825static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002826{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002827 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002828 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002829 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2830 fattr->nlink = 2;
2831}
2832
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002833static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2834 struct qstr *name, struct nfs_fh *fhandle,
2835 struct nfs_fattr *fattr)
2836{
2837 struct nfs4_exception exception = { };
2838 struct rpc_clnt *client = *clnt;
2839 int err;
2840 do {
2841 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2842 switch (err) {
2843 case -NFS4ERR_BADNAME:
2844 err = -ENOENT;
2845 goto out;
2846 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002847 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002848 goto out;
2849 case -NFS4ERR_WRONGSEC:
2850 err = -EPERM;
2851 if (client != *clnt)
2852 goto out;
2853
2854 client = nfs4_create_sec_client(client, dir, name);
2855 if (IS_ERR(client))
2856 return PTR_ERR(client);
2857
2858 exception.retry = 1;
2859 break;
2860 default:
2861 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2862 }
2863 } while (exception.retry);
2864
2865out:
2866 if (err == 0)
2867 *clnt = client;
2868 else if (client != *clnt)
2869 rpc_shutdown_client(client);
2870
2871 return err;
2872}
2873
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002874static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002875 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002877 int status;
2878 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002879
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002880 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2881 if (client != NFS_CLIENT(dir)) {
2882 rpc_shutdown_client(client);
2883 nfs_fixup_secinfo_attributes(fattr);
2884 }
2885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002888struct rpc_clnt *
2889nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2890 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2891{
2892 int status;
2893 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2894
2895 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2896 if (status < 0) {
2897 rpc_shutdown_client(client);
2898 return ERR_PTR(status);
2899 }
2900 return client;
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2904{
Trond Myklebust76b32992007-08-10 17:45:11 -04002905 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 struct nfs4_accessargs args = {
2907 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002908 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002910 struct nfs4_accessres res = {
2911 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002912 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 struct rpc_message msg = {
2914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2915 .rpc_argp = &args,
2916 .rpc_resp = &res,
2917 .rpc_cred = entry->cred,
2918 };
2919 int mode = entry->mask;
2920 int status;
2921
2922 /*
2923 * Determine which access bits we want to ask for...
2924 */
2925 if (mode & MAY_READ)
2926 args.access |= NFS4_ACCESS_READ;
2927 if (S_ISDIR(inode->i_mode)) {
2928 if (mode & MAY_WRITE)
2929 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2930 if (mode & MAY_EXEC)
2931 args.access |= NFS4_ACCESS_LOOKUP;
2932 } else {
2933 if (mode & MAY_WRITE)
2934 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2935 if (mode & MAY_EXEC)
2936 args.access |= NFS4_ACCESS_EXECUTE;
2937 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002938
2939 res.fattr = nfs_alloc_fattr();
2940 if (res.fattr == NULL)
2941 return -ENOMEM;
2942
Bryan Schumaker7c513052011-03-24 17:12:24 +00002943 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002945 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002946 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002948 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
2952static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
2956 do {
2957 err = nfs4_handle_exception(NFS_SERVER(inode),
2958 _nfs4_proc_access(inode, entry),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
2964/*
2965 * TODO: For the time being, we don't try to get any attributes
2966 * along with any of the zero-copy operations READ, READDIR,
2967 * READLINK, WRITE.
2968 *
2969 * In the case of the first three, we want to put the GETATTR
2970 * after the read-type operation -- this is because it is hard
2971 * to predict the length of a GETATTR response in v4, and thus
2972 * align the READ data correctly. This means that the GETATTR
2973 * may end up partially falling into the page cache, and we should
2974 * shift it into the 'tail' of the xdr_buf before processing.
2975 * To do this efficiently, we need to know the total length
2976 * of data received, which doesn't seem to be available outside
2977 * of the RPC layer.
2978 *
2979 * In the case of WRITE, we also want to put the GETATTR after
2980 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002981 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 *
2983 * Both of these changes to the XDR layer would in fact be quite
2984 * minor, but I decided to leave them for a subsequent patch.
2985 */
2986static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2987 unsigned int pgbase, unsigned int pglen)
2988{
2989 struct nfs4_readlink args = {
2990 .fh = NFS_FH(inode),
2991 .pgbase = pgbase,
2992 .pglen = pglen,
2993 .pages = &page,
2994 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002995 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 struct rpc_message msg = {
2997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2998 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002999 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 };
3001
Bryan Schumaker7c513052011-03-24 17:12:24 +00003002 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 -07003003}
3004
3005static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3006 unsigned int pgbase, unsigned int pglen)
3007{
3008 struct nfs4_exception exception = { };
3009 int err;
3010 do {
3011 err = nfs4_handle_exception(NFS_SERVER(inode),
3012 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003019 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021static int
3022nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003023 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003025 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 int status = 0;
3028
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003029 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3030 if (IS_ERR(ctx))
3031 return PTR_ERR(ctx);
3032
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003033 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003034 state = nfs4_do_open(dir, dentry, ctx->mode,
3035 flags, sattr, ctx->cred,
3036 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003037 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (IS_ERR(state)) {
3039 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003042 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003043 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003044 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003046 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return status;
3048}
3049
3050static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3051{
Trond Myklebust16e42952005-10-27 22:12:44 -04003052 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003053 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003055 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003057 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003058 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003059 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3062 .rpc_argp = &args,
3063 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003065 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003066
Bryan Schumaker7c513052011-03-24 17:12:24 +00003067 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003068 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003069 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return status;
3071}
3072
3073static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3074{
3075 struct nfs4_exception exception = { };
3076 int err;
3077 do {
3078 err = nfs4_handle_exception(NFS_SERVER(dir),
3079 _nfs4_proc_remove(dir, name),
3080 &exception);
3081 } while (exception.retry);
3082 return err;
3083}
3084
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003085static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003087 struct nfs_server *server = NFS_SERVER(dir);
3088 struct nfs_removeargs *args = msg->rpc_argp;
3089 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003091 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003093 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003096static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3097{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003098 nfs4_setup_sequence(NFS_SERVER(data->dir),
3099 &data->args.seq_args,
3100 &data->res.seq_res,
3101 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003104static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003106 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3107
Trond Myklebust14516c32010-07-31 14:29:06 -04003108 if (!nfs4_sequence_done(task, &res->seq_res))
3109 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003110 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003111 return 0;
3112 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Jeff Laytond3d41522010-09-17 17:31:57 -04003116static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3117{
3118 struct nfs_server *server = NFS_SERVER(dir);
3119 struct nfs_renameargs *arg = msg->rpc_argp;
3120 struct nfs_renameres *res = msg->rpc_resp;
3121
3122 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003123 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003124 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003125}
3126
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003127static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3128{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003129 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3130 &data->args.seq_args,
3131 &data->res.seq_res,
3132 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003133}
3134
3135static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3136 struct inode *new_dir)
3137{
3138 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3139
3140 if (!nfs4_sequence_done(task, &res->seq_res))
3141 return 0;
3142 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3143 return 0;
3144
3145 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003146 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003147 return 1;
3148}
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3151 struct inode *new_dir, struct qstr *new_name)
3152{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003153 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003154 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 .old_dir = NFS_FH(old_dir),
3156 .new_dir = NFS_FH(new_dir),
3157 .old_name = old_name,
3158 .new_name = new_name,
3159 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003160 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003161 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 struct rpc_message msg = {
3164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3165 .rpc_argp = &arg,
3166 .rpc_resp = &res,
3167 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003168 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Bryan Schumaker7c513052011-03-24 17:12:24 +00003170 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (!status) {
3172 update_changeattr(old_dir, &res.old_cinfo);
3173 update_changeattr(new_dir, &res.new_cinfo);
3174 }
3175 return status;
3176}
3177
3178static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3179 struct inode *new_dir, struct qstr *new_name)
3180{
3181 struct nfs4_exception exception = { };
3182 int err;
3183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3185 _nfs4_proc_rename(old_dir, old_name,
3186 new_dir, new_name),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3193{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003194 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 struct nfs4_link_arg arg = {
3196 .fh = NFS_FH(inode),
3197 .dir_fh = NFS_FH(dir),
3198 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003199 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003201 struct nfs4_link_res res = {
3202 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct rpc_message msg = {
3205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3206 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003207 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003209 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Trond Myklebust136f2622010-04-16 16:22:49 -04003211 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003212 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003213 goto out;
3214
Bryan Schumaker7c513052011-03-24 17:12:24 +00003215 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003216 if (!status) {
3217 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003218 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003219 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003220out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003221 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return status;
3223}
3224
3225static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3226{
3227 struct nfs4_exception exception = { };
3228 int err;
3229 do {
3230 err = nfs4_handle_exception(NFS_SERVER(inode),
3231 _nfs4_proc_link(inode, dir, name),
3232 &exception);
3233 } while (exception.retry);
3234 return err;
3235}
3236
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003237struct nfs4_createdata {
3238 struct rpc_message msg;
3239 struct nfs4_create_arg arg;
3240 struct nfs4_create_res res;
3241 struct nfs_fh fh;
3242 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003243};
3244
3245static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3246 struct qstr *name, struct iattr *sattr, u32 ftype)
3247{
3248 struct nfs4_createdata *data;
3249
3250 data = kzalloc(sizeof(*data), GFP_KERNEL);
3251 if (data != NULL) {
3252 struct nfs_server *server = NFS_SERVER(dir);
3253
3254 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3255 data->msg.rpc_argp = &data->arg;
3256 data->msg.rpc_resp = &data->res;
3257 data->arg.dir_fh = NFS_FH(dir);
3258 data->arg.server = server;
3259 data->arg.name = name;
3260 data->arg.attrs = sattr;
3261 data->arg.ftype = ftype;
3262 data->arg.bitmask = server->attr_bitmask;
3263 data->res.server = server;
3264 data->res.fh = &data->fh;
3265 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003266 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 }
3268 return data;
3269}
3270
3271static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3272{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003273 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003274 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003275 if (status == 0) {
3276 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3278 }
3279 return status;
3280}
3281
3282static void nfs4_free_createdata(struct nfs4_createdata *data)
3283{
3284 kfree(data);
3285}
3286
Chuck Lever4f390c12006-08-22 20:06:22 -04003287static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003288 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003290 struct nfs4_createdata *data;
3291 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Chuck Lever94a6d752006-08-22 20:06:23 -04003293 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003294 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003295
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 status = -ENOMEM;
3297 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3298 if (data == NULL)
3299 goto out;
3300
3301 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3302 data->arg.u.symlink.pages = &page;
3303 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 status = nfs4_do_create(dir, dentry, data);
3306
3307 nfs4_free_createdata(data);
3308out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return status;
3310}
3311
Chuck Lever4f390c12006-08-22 20:06:22 -04003312static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003313 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct nfs4_exception exception = { };
3316 int err;
3317 do {
3318 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003319 _nfs4_proc_symlink(dir, dentry, page,
3320 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 &exception);
3322 } while (exception.retry);
3323 return err;
3324}
3325
3326static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3327 struct iattr *sattr)
3328{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003329 struct nfs4_createdata *data;
3330 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003332 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3333 if (data == NULL)
3334 goto out;
3335
3336 status = nfs4_do_create(dir, dentry, data);
3337
3338 nfs4_free_createdata(data);
3339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return status;
3341}
3342
3343static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3344 struct iattr *sattr)
3345{
3346 struct nfs4_exception exception = { };
3347 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003348
3349 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 do {
3351 err = nfs4_handle_exception(NFS_SERVER(dir),
3352 _nfs4_proc_mkdir(dir, dentry, sattr),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3356}
3357
3358static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003359 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
3361 struct inode *dir = dentry->d_inode;
3362 struct nfs4_readdir_arg args = {
3363 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003364 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 .pgbase = 0,
3366 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003367 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003368 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 };
3370 struct nfs4_readdir_res res;
3371 struct rpc_message msg = {
3372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3373 .rpc_argp = &args,
3374 .rpc_resp = &res,
3375 .rpc_cred = cred,
3376 };
3377 int status;
3378
Harvey Harrison3110ff82008-05-02 13:42:44 -07003379 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003380 dentry->d_parent->d_name.name,
3381 dentry->d_name.name,
3382 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003383 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003385 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 -05003386 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003387 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003388 status += args.pgbase;
3389 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003390
3391 nfs_invalidate_atime(dir);
3392
Harvey Harrison3110ff82008-05-02 13:42:44 -07003393 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return status;
3395}
3396
3397static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003398 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402 do {
3403 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3404 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003405 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
3411static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3412 struct iattr *sattr, dev_t rdev)
3413{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003414 struct nfs4_createdata *data;
3415 int mode = sattr->ia_mode;
3416 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003418 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3419 if (data == NULL)
3420 goto out;
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003423 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003425 data->arg.ftype = NF4BLK;
3426 data->arg.u.device.specdata1 = MAJOR(rdev);
3427 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
3429 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003430 data->arg.ftype = NF4CHR;
3431 data->arg.u.device.specdata1 = MAJOR(rdev);
3432 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003433 } else if (!S_ISSOCK(mode)) {
3434 status = -EINVAL;
3435 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003438 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003439out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003440 nfs4_free_createdata(data);
3441out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return status;
3443}
3444
3445static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3446 struct iattr *sattr, dev_t rdev)
3447{
3448 struct nfs4_exception exception = { };
3449 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003450
3451 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 do {
3453 err = nfs4_handle_exception(NFS_SERVER(dir),
3454 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3455 &exception);
3456 } while (exception.retry);
3457 return err;
3458}
3459
3460static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3461 struct nfs_fsstat *fsstat)
3462{
3463 struct nfs4_statfs_arg args = {
3464 .fh = fhandle,
3465 .bitmask = server->attr_bitmask,
3466 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003467 struct nfs4_statfs_res res = {
3468 .fsstat = fsstat,
3469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct rpc_message msg = {
3471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3472 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003473 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 };
3475
Trond Myklebust0e574af2005-10-27 22:12:38 -04003476 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003477 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
3480static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3481{
3482 struct nfs4_exception exception = { };
3483 int err;
3484 do {
3485 err = nfs4_handle_exception(server,
3486 _nfs4_proc_statfs(server, fhandle, fsstat),
3487 &exception);
3488 } while (exception.retry);
3489 return err;
3490}
3491
3492static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3493 struct nfs_fsinfo *fsinfo)
3494{
3495 struct nfs4_fsinfo_arg args = {
3496 .fh = fhandle,
3497 .bitmask = server->attr_bitmask,
3498 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003499 struct nfs4_fsinfo_res res = {
3500 .fsinfo = fsinfo,
3501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 struct rpc_message msg = {
3503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3504 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003505 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 };
3507
Bryan Schumaker7c513052011-03-24 17:12:24 +00003508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
3511static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3512{
3513 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003514 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 int err;
3516
3517 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003518 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3519 if (err == 0) {
3520 struct nfs_client *clp = server->nfs_client;
3521
3522 spin_lock(&clp->cl_lock);
3523 clp->cl_lease_time = fsinfo->lease_time * HZ;
3524 clp->cl_last_renewal = now;
3525 spin_unlock(&clp->cl_lock);
3526 break;
3527 }
3528 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 } while (exception.retry);
3530 return err;
3531}
3532
3533static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3534{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003535 int error;
3536
Trond Myklebust0e574af2005-10-27 22:12:38 -04003537 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003538 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003539 if (error == 0) {
3540 /* block layout checks this! */
3541 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003542 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003543 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003544
3545 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
3548static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3549 struct nfs_pathconf *pathconf)
3550{
3551 struct nfs4_pathconf_arg args = {
3552 .fh = fhandle,
3553 .bitmask = server->attr_bitmask,
3554 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003555 struct nfs4_pathconf_res res = {
3556 .pathconf = pathconf,
3557 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3560 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003561 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 };
3563
3564 /* None of the pathconf attributes are mandatory to implement */
3565 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3566 memset(pathconf, 0, sizeof(*pathconf));
3567 return 0;
3568 }
3569
Trond Myklebust0e574af2005-10-27 22:12:38 -04003570 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003571 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
3574static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3575 struct nfs_pathconf *pathconf)
3576{
3577 struct nfs4_exception exception = { };
3578 int err;
3579
3580 do {
3581 err = nfs4_handle_exception(server,
3582 _nfs4_proc_pathconf(server, fhandle, pathconf),
3583 &exception);
3584 } while (exception.retry);
3585 return err;
3586}
3587
Trond Myklebust5521abf2013-03-16 20:54:34 -04003588int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003589 const struct nfs_open_context *ctx,
3590 const struct nfs_lock_context *l_ctx,
3591 fmode_t fmode)
3592{
3593 const struct nfs_lockowner *lockowner = NULL;
3594
3595 if (l_ctx != NULL)
3596 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003597 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003598}
3599EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3600
Trond Myklebust5521abf2013-03-16 20:54:34 -04003601static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3602 const struct nfs_open_context *ctx,
3603 const struct nfs_lock_context *l_ctx,
3604 fmode_t fmode)
3605{
3606 nfs4_stateid current_stateid;
3607
3608 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3609 return false;
3610 return nfs4_stateid_match(stateid, &current_stateid);
3611}
3612
3613static bool nfs4_error_stateid_expired(int err)
3614{
3615 switch (err) {
3616 case -NFS4ERR_DELEG_REVOKED:
3617 case -NFS4ERR_ADMIN_REVOKED:
3618 case -NFS4ERR_BAD_STATEID:
3619 case -NFS4ERR_STALE_STATEID:
3620 case -NFS4ERR_OLD_STATEID:
3621 case -NFS4ERR_OPENMODE:
3622 case -NFS4ERR_EXPIRED:
3623 return true;
3624 }
3625 return false;
3626}
3627
Benny Halevyd20581a2011-05-22 19:52:03 +03003628void __nfs4_read_done_cb(struct nfs_read_data *data)
3629{
Fred Isamancd841602012-04-20 14:47:44 -04003630 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003631}
3632
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003633static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Fred Isamancd841602012-04-20 14:47:44 -04003635 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003637 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003638 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003639 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003641
Benny Halevyd20581a2011-05-22 19:52:03 +03003642 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003644 renew_lease(server, data->timestamp);
3645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
Trond Myklebust5521abf2013-03-16 20:54:34 -04003648static bool nfs4_read_stateid_changed(struct rpc_task *task,
3649 struct nfs_readargs *args)
3650{
3651
3652 if (!nfs4_error_stateid_expired(task->tk_status) ||
3653 nfs4_stateid_is_current(&args->stateid,
3654 args->context,
3655 args->lock_context,
3656 FMODE_READ))
3657 return false;
3658 rpc_restart_call_prepare(task);
3659 return true;
3660}
3661
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003662static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3663{
3664
3665 dprintk("--> %s\n", __func__);
3666
3667 if (!nfs4_sequence_done(task, &data->res.seq_res))
3668 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003669 if (nfs4_read_stateid_changed(task, &data->args))
3670 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003671 return data->read_done_cb ? data->read_done_cb(task, data) :
3672 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003673}
3674
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003675static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003678 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003680 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003683static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3684{
Trond Myklebust9b206142013-03-17 15:52:00 -04003685 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003686 &data->args.seq_args,
3687 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003688 task))
3689 return;
3690 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3691 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Fred Isamanb029bc92011-03-03 15:13:42 +00003694static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Fred Isamancd841602012-04-20 14:47:44 -04003696 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003698 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003699 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003700 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003702 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003704 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003705 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Trond Myklebust5521abf2013-03-16 20:54:34 -04003709static bool nfs4_write_stateid_changed(struct rpc_task *task,
3710 struct nfs_writeargs *args)
3711{
3712
3713 if (!nfs4_error_stateid_expired(task->tk_status) ||
3714 nfs4_stateid_is_current(&args->stateid,
3715 args->context,
3716 args->lock_context,
3717 FMODE_WRITE))
3718 return false;
3719 rpc_restart_call_prepare(task);
3720 return true;
3721}
3722
Fred Isamanb029bc92011-03-03 15:13:42 +00003723static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3724{
3725 if (!nfs4_sequence_done(task, &data->res.seq_res))
3726 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003727 if (nfs4_write_stateid_changed(task, &data->args))
3728 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003729 return data->write_done_cb ? data->write_done_cb(task, data) :
3730 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003731}
3732
Trond Myklebust5a37f852012-04-28 14:55:16 -04003733static
3734bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003735{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003736 const struct nfs_pgio_header *hdr = data->header;
3737
3738 /* Don't request attributes for pNFS or O_DIRECT writes */
3739 if (data->ds_clp != NULL || hdr->dreq != NULL)
3740 return false;
3741 /* Otherwise, request attributes if and only if we don't hold
3742 * a delegation
3743 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003744 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003745}
Fred Isamana69aef12011-03-03 15:13:47 +00003746
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003747static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Fred Isamancd841602012-04-20 14:47:44 -04003749 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003750
Trond Myklebust5a37f852012-04-28 14:55:16 -04003751 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003752 data->args.bitmask = NULL;
3753 data->res.fattr = NULL;
3754 } else
3755 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003756
Fred Isamanb029bc92011-03-03 15:13:42 +00003757 if (!data->write_done_cb)
3758 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003759 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 data->timestamp = jiffies;
3761
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003762 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003763 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003766static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3767{
Trond Myklebust9b206142013-03-17 15:52:00 -04003768 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003769 &data->args.seq_args,
3770 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003771 task))
3772 return;
3773 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3774 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
Fred Isaman0b7c0152012-04-20 14:47:39 -04003777static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3778{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003779 nfs4_setup_sequence(NFS_SERVER(data->inode),
3780 &data->args.seq_args,
3781 &data->res.seq_res,
3782 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
Fred Isaman0b7c0152012-04-20 14:47:39 -04003785static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003788
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003789 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003790 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003791 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Fred Isaman0b7c0152012-04-20 14:47:39 -04003796static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003797{
3798 if (!nfs4_sequence_done(task, &data->res.seq_res))
3799 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003800 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003801}
3802
Fred Isaman0b7c0152012-04-20 14:47:39 -04003803static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003805 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003806
Fred Isaman0b7c0152012-04-20 14:47:39 -04003807 if (data->commit_done_cb == NULL)
3808 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003809 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003810 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003811 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003814struct nfs4_renewdata {
3815 struct nfs_client *client;
3816 unsigned long timestamp;
3817};
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819/*
3820 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3821 * standalone procedure for queueing an asynchronous RENEW.
3822 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003823static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003824{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003825 struct nfs4_renewdata *data = calldata;
3826 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003827
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003828 if (atomic_read(&clp->cl_count) > 1)
3829 nfs4_schedule_state_renewal(clp);
3830 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003831 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003832}
3833
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003834static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003836 struct nfs4_renewdata *data = calldata;
3837 struct nfs_client *clp = data->client;
3838 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003841 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003842 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3843 return;
3844 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003845 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003846 return;
3847 }
3848 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003850 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851}
3852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003853static const struct rpc_call_ops nfs4_renew_ops = {
3854 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003855 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003856};
3857
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003858static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
3860 struct rpc_message msg = {
3861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3862 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003863 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003865 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003867 if (renew_flags == 0)
3868 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003869 if (!atomic_inc_not_zero(&clp->cl_count))
3870 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003871 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003872 if (data == NULL)
3873 return -ENOMEM;
3874 data->client = clp;
3875 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003876 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003877 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003880static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 struct rpc_message msg = {
3883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3884 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003885 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 };
3887 unsigned long now = jiffies;
3888 int status;
3889
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003890 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (status < 0)
3892 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003893 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return 0;
3895}
3896
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897static inline int nfs4_server_supports_acls(struct nfs_server *server)
3898{
3899 return (server->caps & NFS_CAP_ACLS)
3900 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3901 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3902}
3903
Trond Myklebust21f498c2012-08-24 10:59:25 -04003904/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3905 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003906 * the stack.
3907 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003908#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003909
Neil Hormane9e3d722011-03-04 19:26:03 -05003910static int buf_to_pages_noslab(const void *buf, size_t buflen,
3911 struct page **pages, unsigned int *pgbase)
3912{
3913 struct page *newpage, **spages;
3914 int rc = 0;
3915 size_t len;
3916 spages = pages;
3917
3918 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003919 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003920 newpage = alloc_page(GFP_KERNEL);
3921
3922 if (newpage == NULL)
3923 goto unwind;
3924 memcpy(page_address(newpage), buf, len);
3925 buf += len;
3926 buflen -= len;
3927 *pages++ = newpage;
3928 rc++;
3929 } while (buflen != 0);
3930
3931 return rc;
3932
3933unwind:
3934 for(; rc > 0; rc--)
3935 __free_page(spages[rc-1]);
3936 return -ENOMEM;
3937}
3938
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003939struct nfs4_cached_acl {
3940 int cached;
3941 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003942 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003943};
3944
3945static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003946{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003947 struct nfs_inode *nfsi = NFS_I(inode);
3948
3949 spin_lock(&inode->i_lock);
3950 kfree(nfsi->nfs4_acl);
3951 nfsi->nfs4_acl = acl;
3952 spin_unlock(&inode->i_lock);
3953}
3954
3955static void nfs4_zap_acl_attr(struct inode *inode)
3956{
3957 nfs4_set_cached_acl(inode, NULL);
3958}
3959
3960static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3961{
3962 struct nfs_inode *nfsi = NFS_I(inode);
3963 struct nfs4_cached_acl *acl;
3964 int ret = -ENOENT;
3965
3966 spin_lock(&inode->i_lock);
3967 acl = nfsi->nfs4_acl;
3968 if (acl == NULL)
3969 goto out;
3970 if (buf == NULL) /* user is just asking for length */
3971 goto out_len;
3972 if (acl->cached == 0)
3973 goto out;
3974 ret = -ERANGE; /* see getxattr(2) man page */
3975 if (acl->len > buflen)
3976 goto out;
3977 memcpy(buf, acl->data, acl->len);
3978out_len:
3979 ret = acl->len;
3980out:
3981 spin_unlock(&inode->i_lock);
3982 return ret;
3983}
3984
Sachin Prabhu5794d212012-04-17 14:36:40 +01003985static 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 +00003986{
3987 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003988 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003989
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003990 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003991 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003992 if (acl == NULL)
3993 goto out;
3994 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003995 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003996 } else {
3997 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3998 if (acl == NULL)
3999 goto out;
4000 acl->cached = 0;
4001 }
4002 acl->len = acl_len;
4003out:
4004 nfs4_set_cached_acl(inode, acl);
4005}
4006
Andy Adamsonbf118a32011-12-07 11:55:27 -05004007/*
4008 * The getxattr API returns the required buffer length when called with a
4009 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4010 * the required buf. On a NULL buf, we send a page of data to the server
4011 * guessing that the ACL request can be serviced by a page. If so, we cache
4012 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4013 * the cache. If not so, we throw away the page, and cache the required
4014 * length. The next getxattr call will then produce another round trip to
4015 * the server, this time with the input buf of the required size.
4016 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004017static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004018{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004019 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004020 struct nfs_getaclargs args = {
4021 .fh = NFS_FH(inode),
4022 .acl_pages = pages,
4023 .acl_len = buflen,
4024 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004025 struct nfs_getaclres res = {
4026 .acl_len = buflen,
4027 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4030 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004031 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004032 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004033 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4034 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004035
Andy Adamsonbf118a32011-12-07 11:55:27 -05004036 /* As long as we're doing a round trip to the server anyway,
4037 * let's be prepared for a page of acl data. */
4038 if (npages == 0)
4039 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004040 if (npages > ARRAY_SIZE(pages))
4041 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004042
Andy Adamsonbf118a32011-12-07 11:55:27 -05004043 for (i = 0; i < npages; i++) {
4044 pages[i] = alloc_page(GFP_KERNEL);
4045 if (!pages[i])
4046 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004047 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004048
4049 /* for decoding across pages */
4050 res.acl_scratch = alloc_page(GFP_KERNEL);
4051 if (!res.acl_scratch)
4052 goto out_free;
4053
Andy Adamsonbf118a32011-12-07 11:55:27 -05004054 args.acl_len = npages * PAGE_SIZE;
4055 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004056
Peng Taode040be2012-01-10 22:42:47 +08004057 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004058 __func__, buf, buflen, npages, args.acl_len);
4059 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4060 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004061 if (ret)
4062 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004063
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004064 /* Handle the case where the passed-in buffer is too short */
4065 if (res.acl_flags & NFS4_ACL_TRUNC) {
4066 /* Did the user only issue a request for the acl length? */
4067 if (buf == NULL)
4068 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004069 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004070 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004071 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004072 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004073 if (buf) {
4074 if (res.acl_len > buflen) {
4075 ret = -ERANGE;
4076 goto out_free;
4077 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004078 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004079 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004080out_ok:
4081 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004082out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004083 for (i = 0; i < npages; i++)
4084 if (pages[i])
4085 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004086 if (res.acl_scratch)
4087 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004088 return ret;
4089}
4090
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004091static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4092{
4093 struct nfs4_exception exception = { };
4094 ssize_t ret;
4095 do {
4096 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4097 if (ret >= 0)
4098 break;
4099 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4100 } while (exception.retry);
4101 return ret;
4102}
4103
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004104static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4105{
4106 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004107 int ret;
4108
4109 if (!nfs4_server_supports_acls(server))
4110 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004111 ret = nfs_revalidate_inode(server, inode);
4112 if (ret < 0)
4113 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004114 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4115 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004116 ret = nfs4_read_cached_acl(inode, buf, buflen);
4117 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004118 /* -ENOENT is returned if there is no ACL or if there is an ACL
4119 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004120 return ret;
4121 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004122}
4123
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004124static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004125{
4126 struct nfs_server *server = NFS_SERVER(inode);
4127 struct page *pages[NFS4ACL_MAXPAGES];
4128 struct nfs_setaclargs arg = {
4129 .fh = NFS_FH(inode),
4130 .acl_pages = pages,
4131 .acl_len = buflen,
4132 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004133 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004134 struct rpc_message msg = {
4135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4136 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004137 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004138 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004139 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004140 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004141
4142 if (!nfs4_server_supports_acls(server))
4143 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004144 if (npages > ARRAY_SIZE(pages))
4145 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004146 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4147 if (i < 0)
4148 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004149 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004150 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004151
4152 /*
4153 * Free each page after tx, so the only ref left is
4154 * held by the network stack
4155 */
4156 for (; i > 0; i--)
4157 put_page(pages[i-1]);
4158
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004159 /*
4160 * Acl update can result in inode attribute update.
4161 * so mark the attribute cache invalid.
4162 */
4163 spin_lock(&inode->i_lock);
4164 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4165 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004166 nfs_access_zap_cache(inode);
4167 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004168 return ret;
4169}
4170
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004171static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4172{
4173 struct nfs4_exception exception = { };
4174 int err;
4175 do {
4176 err = nfs4_handle_exception(NFS_SERVER(inode),
4177 __nfs4_proc_set_acl(inode, buf, buflen),
4178 &exception);
4179 } while (exception.retry);
4180 return err;
4181}
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004184nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004186 struct nfs_client *clp = server->nfs_client;
4187
4188 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return 0;
4190 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004191 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004192 case -NFS4ERR_ADMIN_REVOKED:
4193 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004194 if (state == NULL)
4195 break;
4196 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004197 case -NFS4ERR_OPENMODE:
4198 if (state == NULL)
4199 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004200 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4201 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004202 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004203 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004204 if (state != NULL) {
4205 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4206 goto stateid_invalid;
4207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004209 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004210 nfs4_schedule_lease_recovery(clp);
4211 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004212#if defined(CONFIG_NFS_V4_1)
4213 case -NFS4ERR_BADSESSION:
4214 case -NFS4ERR_BADSLOT:
4215 case -NFS4ERR_BAD_HIGH_SLOT:
4216 case -NFS4ERR_DEADSESSION:
4217 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4218 case -NFS4ERR_SEQ_FALSE_RETRY:
4219 case -NFS4ERR_SEQ_MISORDERED:
4220 dprintk("%s ERROR %d, Reset session\n", __func__,
4221 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004222 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004223 task->tk_status = 0;
4224 return -EAGAIN;
4225#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004227 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004228 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4230 task->tk_status = 0;
4231 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004232 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 case -NFS4ERR_OLD_STATEID:
4234 task->tk_status = 0;
4235 return -EAGAIN;
4236 }
4237 task->tk_status = nfs4_map_errors(task->tk_status);
4238 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004239stateid_invalid:
4240 task->tk_status = -EIO;
4241 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004242wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004243 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004244 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4245 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4246 task->tk_status = 0;
4247 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Chuck Leverf0920752012-05-21 22:45:41 -04004250static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4251 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004252{
4253 __be32 verf[2];
4254
Chuck Lever2c820d92012-05-21 22:45:33 -04004255 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4256 /* An impossible timestamp guarantees this value
4257 * will never match a generated boot time. */
4258 verf[0] = 0;
4259 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4260 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004261 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4262 verf[0] = (__be32)nn->boot_time.tv_sec;
4263 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004264 }
Chuck Levercd937102012-03-02 17:14:31 -05004265 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4266}
4267
Chuck Levere984a552012-09-14 17:24:21 -04004268static unsigned int
4269nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4270 char *buf, size_t len)
4271{
4272 unsigned int result;
4273
4274 rcu_read_lock();
4275 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4276 clp->cl_ipaddr,
4277 rpc_peeraddr2str(clp->cl_rpcclient,
4278 RPC_DISPLAY_ADDR),
4279 rpc_peeraddr2str(clp->cl_rpcclient,
4280 RPC_DISPLAY_PROTO));
4281 rcu_read_unlock();
4282 return result;
4283}
4284
4285static unsigned int
4286nfs4_init_uniform_client_string(const struct nfs_client *clp,
4287 char *buf, size_t len)
4288{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004289 char *nodename = clp->cl_rpcclient->cl_nodename;
4290
4291 if (nfs4_client_id_uniquifier[0] != '\0')
4292 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004293 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4294 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004295 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004296}
4297
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004298/**
4299 * nfs4_proc_setclientid - Negotiate client ID
4300 * @clp: state data structure
4301 * @program: RPC program for NFSv4 callback service
4302 * @port: IP port number for NFS4 callback service
4303 * @cred: RPC credential to use for this call
4304 * @res: where to place the result
4305 *
4306 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4307 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004308int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4309 unsigned short port, struct rpc_cred *cred,
4310 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
4312 nfs4_verifier sc_verifier;
4313 struct nfs4_setclientid setclientid = {
4314 .sc_verifier = &sc_verifier,
4315 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004316 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 };
4318 struct rpc_message msg = {
4319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4320 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004321 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004322 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004324 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Chuck Leverde734832012-07-11 16:30:50 -04004326 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004327 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004328 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4329 setclientid.sc_name_len =
4330 nfs4_init_uniform_client_string(clp,
4331 setclientid.sc_name,
4332 sizeof(setclientid.sc_name));
4333 else
4334 setclientid.sc_name_len =
4335 nfs4_init_nonuniform_client_string(clp,
4336 setclientid.sc_name,
4337 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004338 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004339 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004340 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004341 sizeof(setclientid.sc_netid),
4342 rpc_peeraddr2str(clp->cl_rpcclient,
4343 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004344 rcu_read_unlock();
4345 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004346 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 clp->cl_ipaddr, port >> 8, port & 255);
4348
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004349 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4350 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4351 setclientid.sc_name_len, setclientid.sc_name);
4352 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4353 dprintk("NFS reply setclientid: %d\n", status);
4354 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004357/**
4358 * nfs4_proc_setclientid_confirm - Confirm client ID
4359 * @clp: state data structure
4360 * @res: result of a previous SETCLIENTID
4361 * @cred: RPC credential to use for this call
4362 *
4363 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4364 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004365int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004366 struct nfs4_setclientid_res *arg,
4367 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 struct rpc_message msg = {
4370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004371 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004372 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 int status;
4375
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004376 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4377 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4378 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004379 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004380 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return status;
4382}
4383
Trond Myklebustfe650402006-01-03 09:55:18 +01004384struct nfs4_delegreturndata {
4385 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004386 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004387 struct nfs_fh fh;
4388 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004389 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004390 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004391 int rpc_status;
4392};
4393
Trond Myklebustfe650402006-01-03 09:55:18 +01004394static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4395{
4396 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004397
Trond Myklebust14516c32010-07-31 14:29:06 -04004398 if (!nfs4_sequence_done(task, &data->res.seq_res))
4399 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004400
Ricardo Labiaga79708862009-12-07 09:23:21 -05004401 switch (task->tk_status) {
4402 case -NFS4ERR_STALE_STATEID:
4403 case -NFS4ERR_EXPIRED:
4404 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004405 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004406 break;
4407 default:
4408 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4409 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004410 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004411 return;
4412 }
4413 }
4414 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004415}
4416
4417static void nfs4_delegreturn_release(void *calldata)
4418{
Trond Myklebustfe650402006-01-03 09:55:18 +01004419 kfree(calldata);
4420}
4421
Andy Adamson938e1012009-04-01 09:22:28 -04004422#if defined(CONFIG_NFS_V4_1)
4423static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4424{
4425 struct nfs4_delegreturndata *d_data;
4426
4427 d_data = (struct nfs4_delegreturndata *)data;
4428
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004429 nfs4_setup_sequence(d_data->res.server,
4430 &d_data->args.seq_args,
4431 &d_data->res.seq_res,
4432 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004433}
4434#endif /* CONFIG_NFS_V4_1 */
4435
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004436static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004437#if defined(CONFIG_NFS_V4_1)
4438 .rpc_call_prepare = nfs4_delegreturn_prepare,
4439#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004440 .rpc_call_done = nfs4_delegreturn_done,
4441 .rpc_release = nfs4_delegreturn_release,
4442};
4443
Trond Myklebuste6f81072008-01-24 18:14:34 -05004444static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004445{
4446 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004447 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004448 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004449 struct rpc_message msg = {
4450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4451 .rpc_cred = cred,
4452 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004453 struct rpc_task_setup task_setup_data = {
4454 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004455 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004456 .callback_ops = &nfs4_delegreturn_ops,
4457 .flags = RPC_TASK_ASYNC,
4458 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004459 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004460
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004461 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004462 if (data == NULL)
4463 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004464 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004465 data->args.fhandle = &data->fh;
4466 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004467 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004468 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004469 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004470 data->res.fattr = &data->fattr;
4471 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004472 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004473 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004474 data->rpc_status = 0;
4475
Trond Myklebustc970aa82007-07-14 15:39:59 -04004476 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004477 msg.rpc_argp = &data->args;
4478 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004479 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004480 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004481 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004482 if (!issync)
4483 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004484 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004485 if (status != 0)
4486 goto out;
4487 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004488 if (status == 0)
4489 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4490 else
4491 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004492out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004493 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004494 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495}
4496
Trond Myklebuste6f81072008-01-24 18:14:34 -05004497int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
4499 struct nfs_server *server = NFS_SERVER(inode);
4500 struct nfs4_exception exception = { };
4501 int err;
4502 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004503 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 switch (err) {
4505 case -NFS4ERR_STALE_STATEID:
4506 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 case 0:
4508 return 0;
4509 }
4510 err = nfs4_handle_exception(server, err, &exception);
4511 } while (exception.retry);
4512 return err;
4513}
4514
4515#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4516#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4517
4518/*
4519 * sleep, with exponential backoff, and retry the LOCK operation.
4520 */
4521static unsigned long
4522nfs4_set_lock_task_retry(unsigned long timeout)
4523{
Jeff Laytond3103102011-12-01 22:44:39 +01004524 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 timeout <<= 1;
4526 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4527 return NFS4_LOCK_MAXTIMEOUT;
4528 return timeout;
4529}
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4532{
4533 struct inode *inode = state->inode;
4534 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004535 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004536 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004538 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540 struct nfs_lockt_res res = {
4541 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 };
4543 struct rpc_message msg = {
4544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4545 .rpc_argp = &arg,
4546 .rpc_resp = &res,
4547 .rpc_cred = state->owner->so_cred,
4548 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 struct nfs4_lock_state *lsp;
4550 int status;
4551
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004552 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004553 status = nfs4_set_lock_state(state, request);
4554 if (status != 0)
4555 goto out;
4556 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004557 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004558 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004559 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004560 switch (status) {
4561 case 0:
4562 request->fl_type = F_UNLCK;
4563 break;
4564 case -NFS4ERR_DENIED:
4565 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004567 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return status;
4570}
4571
4572static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4573{
4574 struct nfs4_exception exception = { };
4575 int err;
4576
4577 do {
4578 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4579 _nfs4_proc_getlk(state, cmd, request),
4580 &exception);
4581 } while (exception.retry);
4582 return err;
4583}
4584
4585static int do_vfs_lock(struct file *file, struct file_lock *fl)
4586{
4587 int res = 0;
4588 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4589 case FL_POSIX:
4590 res = posix_lock_file_wait(file, fl);
4591 break;
4592 case FL_FLOCK:
4593 res = flock_lock_file_wait(file, fl);
4594 break;
4595 default:
4596 BUG();
4597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return res;
4599}
4600
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004602 struct nfs_locku_args arg;
4603 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604 struct nfs4_lock_state *lsp;
4605 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004606 struct file_lock fl;
4607 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004608 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004609};
4610
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004611static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4612 struct nfs_open_context *ctx,
4613 struct nfs4_lock_state *lsp,
4614 struct nfs_seqid *seqid)
4615{
4616 struct nfs4_unlockdata *p;
4617 struct inode *inode = lsp->ls_state->inode;
4618
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004619 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004620 if (p == NULL)
4621 return NULL;
4622 p->arg.fh = NFS_FH(inode);
4623 p->arg.fl = &p->fl;
4624 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004625 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004626 p->arg.stateid = &lsp->ls_stateid;
4627 p->lsp = lsp;
4628 atomic_inc(&lsp->ls_count);
4629 /* Ensure we don't close file until we're done freeing locks! */
4630 p->ctx = get_nfs_open_context(ctx);
4631 memcpy(&p->fl, fl, sizeof(p->fl));
4632 p->server = NFS_SERVER(inode);
4633 return p;
4634}
4635
Trond Myklebust06f814a2006-01-03 09:55:07 +01004636static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004637{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004638 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004639 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004640 nfs4_put_lock_state(calldata->lsp);
4641 put_nfs_open_context(calldata->ctx);
4642 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004643}
4644
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004645static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004646{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004647 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004648
Trond Myklebust14516c32010-07-31 14:29:06 -04004649 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4650 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004651 switch (task->tk_status) {
4652 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004653 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4654 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004655 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004656 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004657 case -NFS4ERR_BAD_STATEID:
4658 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004659 case -NFS4ERR_STALE_STATEID:
4660 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004661 break;
4662 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004663 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004664 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004665 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004666 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004667}
4668
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004669static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004670{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004671 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004673 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004674 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004675 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004676 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004677 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004678 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004679 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004680 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004681 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004682 &calldata->res.seq_res,
4683 task) != 0)
4684 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004685 return;
4686out_no_action:
4687 task->tk_action = NULL;
4688out_wait:
4689 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004692static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004693 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004694 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004695 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004696};
4697
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4699 struct nfs_open_context *ctx,
4700 struct nfs4_lock_state *lsp,
4701 struct nfs_seqid *seqid)
4702{
4703 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004704 struct rpc_message msg = {
4705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4706 .rpc_cred = ctx->cred,
4707 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004708 struct rpc_task_setup task_setup_data = {
4709 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004710 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004711 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004712 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004713 .flags = RPC_TASK_ASYNC,
4714 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004715
Frank Filz137d6ac2007-07-09 15:32:29 -07004716 /* Ensure this is an unlock - when canceling a lock, the
4717 * canceled lock is passed in, and it won't be an unlock.
4718 */
4719 fl->fl_type = F_UNLCK;
4720
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4722 if (data == NULL) {
4723 nfs_free_seqid(seqid);
4724 return ERR_PTR(-ENOMEM);
4725 }
4726
Trond Myklebust9d12b212012-01-17 22:04:25 -05004727 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004728 msg.rpc_argp = &data->arg;
4729 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004730 task_setup_data.callback_data = data;
4731 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004732}
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4735{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004736 struct inode *inode = state->inode;
4737 struct nfs4_state_owner *sp = state->owner;
4738 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004739 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004740 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004741 struct rpc_task *task;
4742 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004743 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Trond Myklebust9b073572006-06-29 16:38:34 -04004745 status = nfs4_set_lock_state(state, request);
4746 /* Unlock _before_ we do the RPC call */
4747 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004748 /* Exclude nfs_delegation_claim_locks() */
4749 mutex_lock(&sp->so_delegreturn_mutex);
4750 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004751 down_read(&nfsi->rwsem);
4752 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4753 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004754 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004755 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004756 }
4757 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004758 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004759 if (status != 0)
4760 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004761 /* Is this a delegated lock? */
4762 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004763 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004764 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004765 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004766 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004767 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004768 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004769 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770 status = PTR_ERR(task);
4771 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004772 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004774 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004775out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004776 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004780struct nfs4_lockdata {
4781 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004782 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004783 struct nfs4_lock_state *lsp;
4784 struct nfs_open_context *ctx;
4785 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004786 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004787 int rpc_status;
4788 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004789 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790};
4791
4792static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004793 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4794 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004795{
4796 struct nfs4_lockdata *p;
4797 struct inode *inode = lsp->ls_state->inode;
4798 struct nfs_server *server = NFS_SERVER(inode);
4799
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004800 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801 if (p == NULL)
4802 return NULL;
4803
4804 p->arg.fh = NFS_FH(inode);
4805 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004806 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004807 if (p->arg.open_seqid == NULL)
4808 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004809 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004810 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004811 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004813 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004814 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004815 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004816 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004817 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004818 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004819 atomic_inc(&lsp->ls_count);
4820 p->ctx = get_nfs_open_context(ctx);
4821 memcpy(&p->fl, fl, sizeof(p->fl));
4822 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004823out_free_seqid:
4824 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825out_free:
4826 kfree(p);
4827 return NULL;
4828}
4829
4830static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4831{
4832 struct nfs4_lockdata *data = calldata;
4833 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Harvey Harrison3110ff82008-05-02 13:42:44 -07004835 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004836 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004837 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004838 /* Do we need to do an open_to_lock_owner? */
4839 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004840 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004841 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004842 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004843 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004844 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004845 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004846 } else
4847 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004848 if (!nfs4_valid_open_stateid(state)) {
4849 data->rpc_status = -EBADF;
4850 task->tk_action = NULL;
4851 goto out_release_open_seqid;
4852 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004853 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004854 if (nfs4_setup_sequence(data->server,
4855 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004856 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004857 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004858 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004859out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004860 nfs_release_seqid(data->arg.open_seqid);
4861out_release_lock_seqid:
4862 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004863out_wait:
4864 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004865 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004866}
4867
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004868static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4869{
4870 struct nfs4_lockdata *data = calldata;
4871
Harvey Harrison3110ff82008-05-02 13:42:44 -07004872 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004873
Trond Myklebust14516c32010-07-31 14:29:06 -04004874 if (!nfs4_sequence_done(task, &data->res.seq_res))
4875 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004876
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004877 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004878 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004879 if (data->rpc_status == 0)
4880 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4881 else
4882 goto out;
4883 }
4884 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004885 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004886 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004887 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004888 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004889out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004890 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891}
4892
4893static void nfs4_lock_release(void *calldata)
4894{
4895 struct nfs4_lockdata *data = calldata;
4896
Harvey Harrison3110ff82008-05-02 13:42:44 -07004897 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004898 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004899 if (data->cancelled != 0) {
4900 struct rpc_task *task;
4901 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4902 data->arg.lock_seqid);
4903 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004904 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004905 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004906 } else
4907 nfs_free_seqid(data->arg.lock_seqid);
4908 nfs4_put_lock_state(data->lsp);
4909 put_nfs_open_context(data->ctx);
4910 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004911 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004912}
4913
4914static const struct rpc_call_ops nfs4_lock_ops = {
4915 .rpc_call_prepare = nfs4_lock_prepare,
4916 .rpc_call_done = nfs4_lock_done,
4917 .rpc_release = nfs4_lock_release,
4918};
4919
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004920static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4921{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004922 switch (error) {
4923 case -NFS4ERR_ADMIN_REVOKED:
4924 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004925 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004926 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004927 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004928 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004929 break;
4930 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004931 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004932 case -NFS4ERR_EXPIRED:
4933 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004934 };
4935}
4936
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004937static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004938{
4939 struct nfs4_lockdata *data;
4940 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004941 struct rpc_message msg = {
4942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4943 .rpc_cred = state->owner->so_cred,
4944 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004945 struct rpc_task_setup task_setup_data = {
4946 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004947 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004948 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004949 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004950 .flags = RPC_TASK_ASYNC,
4951 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004952 int ret;
4953
Harvey Harrison3110ff82008-05-02 13:42:44 -07004954 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004955 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004956 fl->fl_u.nfs4_fl.owner,
4957 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004958 if (data == NULL)
4959 return -ENOMEM;
4960 if (IS_SETLKW(cmd))
4961 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004962 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004963 msg.rpc_argp = &data->arg;
4964 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004965 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004966 if (recovery_type > NFS_LOCK_NEW) {
4967 if (recovery_type == NFS_LOCK_RECLAIM)
4968 data->arg.reclaim = NFS_LOCK_RECLAIM;
4969 nfs4_set_sequence_privileged(&data->arg.seq_args);
4970 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004971 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004972 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004973 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004974 ret = nfs4_wait_for_completion_rpc_task(task);
4975 if (ret == 0) {
4976 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004977 if (ret)
4978 nfs4_handle_setlk_error(data->server, data->lsp,
4979 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004980 } else
4981 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004982 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004983 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004984 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
4987static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4988{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004989 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004990 struct nfs4_exception exception = {
4991 .inode = state->inode,
4992 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004993 int err;
4994
4995 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004996 /* Cache the lock if possible... */
4997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4998 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004999 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005000 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005001 break;
5002 nfs4_handle_exception(server, err, &exception);
5003 } while (exception.retry);
5004 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005}
5006
5007static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5008{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005009 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005010 struct nfs4_exception exception = {
5011 .inode = state->inode,
5012 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005013 int err;
5014
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005015 err = nfs4_set_lock_state(state, request);
5016 if (err != 0)
5017 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005018 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005019 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5020 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005021 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005022 switch (err) {
5023 default:
5024 goto out;
5025 case -NFS4ERR_GRACE:
5026 case -NFS4ERR_DELAY:
5027 nfs4_handle_exception(server, err, &exception);
5028 err = 0;
5029 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005030 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005031out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005035#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005036/**
5037 * nfs41_check_expired_locks - possibly free a lock stateid
5038 *
5039 * @state: NFSv4 state for an inode
5040 *
5041 * Returns NFS_OK if recovery for this stateid is now finished.
5042 * Otherwise a negative NFS4ERR value is returned.
5043 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005044static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005045{
Chuck Levereb64cf92012-07-11 16:30:05 -04005046 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005047 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005048 struct nfs_server *server = NFS_SERVER(state->inode);
5049
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005050 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005051 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005052 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5053 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005054 /* Free the stateid unless the server
5055 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005056 if (status != -NFS4ERR_BAD_STATEID)
5057 nfs41_free_stateid(server,
5058 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005059 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005060 ret = status;
5061 }
5062 }
5063 };
5064
5065 return ret;
5066}
5067
5068static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5069{
5070 int status = NFS_OK;
5071
5072 if (test_bit(LK_STATE_IN_USE, &state->flags))
5073 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005074 if (status != NFS_OK)
5075 status = nfs4_lock_expired(state, request);
5076 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005077}
5078#endif
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5081{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005082 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005083 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005084 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005085 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005086 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005088 if ((fl_flags & FL_POSIX) &&
5089 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5090 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005091 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005092 status = nfs4_set_lock_state(state, request);
5093 if (status != 0)
5094 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005095 request->fl_flags |= FL_ACCESS;
5096 status = do_vfs_lock(request->fl_file, request);
5097 if (status < 0)
5098 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005099 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005100 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005101 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005102 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005103 request->fl_flags = fl_flags & ~FL_SLEEP;
5104 status = do_vfs_lock(request->fl_file, request);
5105 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005106 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005107 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5108 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005109 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005110 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005111 goto out;
5112 down_read(&nfsi->rwsem);
5113 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5114 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005115 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005116 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005117 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005118 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005119 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005120 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5121 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005122out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005123 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005124out:
5125 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return status;
5127}
5128
5129static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5130{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005131 struct nfs4_exception exception = {
5132 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005133 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005134 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 int err;
5136
5137 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005138 err = _nfs4_proc_setlk(state, cmd, request);
5139 if (err == -NFS4ERR_DENIED)
5140 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005142 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 } while (exception.retry);
5144 return err;
5145}
5146
5147static int
5148nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5149{
5150 struct nfs_open_context *ctx;
5151 struct nfs4_state *state;
5152 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5153 int status;
5154
5155 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005156 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 state = ctx->state;
5158
5159 if (request->fl_start < 0 || request->fl_end < 0)
5160 return -EINVAL;
5161
Trond Myklebustd9531262009-07-21 19:22:38 -04005162 if (IS_GETLK(cmd)) {
5163 if (state != NULL)
5164 return nfs4_proc_getlk(state, F_GETLK, request);
5165 return 0;
5166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5169 return -EINVAL;
5170
Trond Myklebustd9531262009-07-21 19:22:38 -04005171 if (request->fl_type == F_UNLCK) {
5172 if (state != NULL)
5173 return nfs4_proc_unlck(state, cmd, request);
5174 return 0;
5175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Trond Myklebustd9531262009-07-21 19:22:38 -04005177 if (state == NULL)
5178 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005179 /*
5180 * Don't rely on the VFS having checked the file open mode,
5181 * since it won't do this for flock() locks.
5182 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005183 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005184 case F_RDLCK:
5185 if (!(filp->f_mode & FMODE_READ))
5186 return -EBADF;
5187 break;
5188 case F_WRLCK:
5189 if (!(filp->f_mode & FMODE_WRITE))
5190 return -EBADF;
5191 }
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 do {
5194 status = nfs4_proc_setlk(state, cmd, request);
5195 if ((status != -EAGAIN) || IS_SETLK(cmd))
5196 break;
5197 timeout = nfs4_set_lock_task_retry(timeout);
5198 status = -ERESTARTSYS;
5199 if (signalled())
5200 break;
5201 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 return status;
5203}
5204
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005205int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005206{
5207 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005208 int err;
5209
5210 err = nfs4_set_lock_state(state, fl);
5211 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005212 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005213 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005214 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005215}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005216
Trond Myklebustcf470c32012-03-07 13:49:12 -05005217struct nfs_release_lockowner_data {
5218 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005219 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005220 struct nfs_release_lockowner_args args;
5221};
5222
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005223static void nfs4_release_lockowner_release(void *calldata)
5224{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005225 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005226 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005227 kfree(calldata);
5228}
5229
Trond Myklebust17280172012-03-11 13:11:00 -04005230static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005231 .rpc_release = nfs4_release_lockowner_release,
5232};
5233
Trond Myklebustcf470c32012-03-07 13:49:12 -05005234int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005235{
5236 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005237 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005238 struct rpc_message msg = {
5239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5240 };
5241
5242 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005243 return -EINVAL;
5244 data = kmalloc(sizeof(*data), GFP_NOFS);
5245 if (!data)
5246 return -ENOMEM;
5247 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005248 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005249 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5250 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5251 data->args.lock_owner.s_dev = server->s_dev;
5252 msg.rpc_argp = &data->args;
5253 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5254 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005255}
5256
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005257#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5258
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005259static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5260 const void *buf, size_t buflen,
5261 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005262{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005263 if (strcmp(key, "") != 0)
5264 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005265
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005266 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005267}
5268
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005269static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5270 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005271{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005272 if (strcmp(key, "") != 0)
5273 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005274
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005275 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005276}
5277
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005278static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5279 size_t list_len, const char *name,
5280 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005281{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005282 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005283
J. Bruce Fields096455a2006-03-20 23:23:42 -05005284 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5285 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005286
5287 if (list && len <= list_len)
5288 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005289 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005290}
5291
Andy Adamson533eb462011-06-13 18:25:56 -04005292/*
5293 * nfs_fhget will use either the mounted_on_fileid or the fileid
5294 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005295static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5296{
Andy Adamson533eb462011-06-13 18:25:56 -04005297 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5298 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5299 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005300 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005301 return;
5302
5303 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005304 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005305 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5306 fattr->nlink = 2;
5307}
5308
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005309static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5310 const struct qstr *name,
5311 struct nfs4_fs_locations *fs_locations,
5312 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005313{
5314 struct nfs_server *server = NFS_SERVER(dir);
5315 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005316 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005317 };
5318 struct nfs4_fs_locations_arg args = {
5319 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005320 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005321 .page = page,
5322 .bitmask = bitmask,
5323 };
Benny Halevy22958462009-04-01 09:22:02 -04005324 struct nfs4_fs_locations_res res = {
5325 .fs_locations = fs_locations,
5326 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005327 struct rpc_message msg = {
5328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5329 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005330 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005331 };
5332 int status;
5333
Harvey Harrison3110ff82008-05-02 13:42:44 -07005334 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005335
5336 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5337 * is not supported */
5338 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5339 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5340 else
5341 bitmask[0] |= FATTR4_WORD0_FILEID;
5342
Trond Myklebustc228fd32007-01-13 02:28:11 -05005343 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005344 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005345 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005346 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005347 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005348 return status;
5349}
5350
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005351int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5352 const struct qstr *name,
5353 struct nfs4_fs_locations *fs_locations,
5354 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005355{
5356 struct nfs4_exception exception = { };
5357 int err;
5358 do {
5359 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005360 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005361 &exception);
5362 } while (exception.retry);
5363 return err;
5364}
5365
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005366static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5367{
5368 int status;
5369 struct nfs4_secinfo_arg args = {
5370 .dir_fh = NFS_FH(dir),
5371 .name = name,
5372 };
5373 struct nfs4_secinfo_res res = {
5374 .flavors = flavors,
5375 };
5376 struct rpc_message msg = {
5377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5378 .rpc_argp = &args,
5379 .rpc_resp = &res,
5380 };
5381
5382 dprintk("NFS call secinfo %s\n", name->name);
5383 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5384 dprintk("NFS reply secinfo: %d\n", status);
5385 return status;
5386}
5387
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005388int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5389 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005390{
5391 struct nfs4_exception exception = { };
5392 int err;
5393 do {
5394 err = nfs4_handle_exception(NFS_SERVER(dir),
5395 _nfs4_proc_secinfo(dir, name, flavors),
5396 &exception);
5397 } while (exception.retry);
5398 return err;
5399}
5400
Andy Adamson557134a2009-04-01 09:21:53 -04005401#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005402/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005403 * Check the exchange flags returned by the server for invalid flags, having
5404 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5405 * DS flags set.
5406 */
5407static int nfs4_check_cl_exchange_flags(u32 flags)
5408{
5409 if (flags & ~EXCHGID4_FLAG_MASK_R)
5410 goto out_inval;
5411 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5412 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5413 goto out_inval;
5414 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5415 goto out_inval;
5416 return NFS_OK;
5417out_inval:
5418 return -NFS4ERR_INVAL;
5419}
5420
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005421static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005422nfs41_same_server_scope(struct nfs41_server_scope *a,
5423 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005424{
5425 if (a->server_scope_sz == b->server_scope_sz &&
5426 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5427 return true;
5428
5429 return false;
5430}
5431
Andy Adamson357f54d2010-12-14 10:11:57 -05005432/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005433 * nfs4_proc_bind_conn_to_session()
5434 *
5435 * The 4.1 client currently uses the same TCP connection for the
5436 * fore and backchannel.
5437 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005438int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005439{
5440 int status;
5441 struct nfs41_bind_conn_to_session_res res;
5442 struct rpc_message msg = {
5443 .rpc_proc =
5444 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5445 .rpc_argp = clp,
5446 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005447 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005448 };
5449
5450 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005451
5452 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5453 if (unlikely(res.session == NULL)) {
5454 status = -ENOMEM;
5455 goto out;
5456 }
5457
5458 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5459 if (status == 0) {
5460 if (memcmp(res.session->sess_id.data,
5461 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5462 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5463 status = -EIO;
5464 goto out_session;
5465 }
5466 if (res.dir != NFS4_CDFS4_BOTH) {
5467 dprintk("NFS: %s: Unexpected direction from server\n",
5468 __func__);
5469 status = -EIO;
5470 goto out_session;
5471 }
5472 if (res.use_conn_in_rdma_mode) {
5473 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5474 __func__);
5475 status = -EIO;
5476 goto out_session;
5477 }
5478 }
5479out_session:
5480 kfree(res.session);
5481out:
5482 dprintk("<-- %s status= %d\n", __func__, status);
5483 return status;
5484}
5485
5486/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005487 * nfs4_proc_exchange_id()
5488 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005489 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5490 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005491 * Since the clientid has expired, all compounds using sessions
5492 * associated with the stale clientid will be returning
5493 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5494 * be in some phase of session reset.
5495 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005496int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005497{
5498 nfs4_verifier verifier;
5499 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005500 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005501 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005502 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005503 };
5504 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005505 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005506 };
5507 int status;
5508 struct rpc_message msg = {
5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5510 .rpc_argp = &args,
5511 .rpc_resp = &res,
5512 .rpc_cred = cred,
5513 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005514
Chuck Leverf0920752012-05-21 22:45:41 -04005515 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005516 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5517 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5519 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5520 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005521
Chuck Leveracdeb692012-05-21 22:46:16 -04005522 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005523 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005524 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005525 status = -ENOMEM;
5526 goto out;
5527 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005528
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005529 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005530 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005531 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005532 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005533 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005534 }
5535
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005536 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005537 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005538 status = -ENOMEM;
5539 goto out_server_scope;
5540 }
5541
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005542 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005543 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005544 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005545
Chuck Lever177313f2012-05-21 22:44:58 -04005546 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005547 clp->cl_clientid = res.clientid;
5548 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5549 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5550 clp->cl_seqid = res.seqid;
5551
Chuck Leveracdeb692012-05-21 22:46:16 -04005552 kfree(clp->cl_serverowner);
5553 clp->cl_serverowner = res.server_owner;
5554 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005555
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005556 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005557 kfree(clp->cl_implid);
5558 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005559
Chuck Lever177313f2012-05-21 22:44:58 -04005560 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005561 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005562 res.server_scope)) {
5563 dprintk("%s: server_scope mismatch detected\n",
5564 __func__);
5565 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005566 kfree(clp->cl_serverscope);
5567 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005568 }
5569
Chuck Lever177313f2012-05-21 22:44:58 -04005570 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005571 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005572 goto out;
5573 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005574 } else
5575 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005576
Chuck Leveracdeb692012-05-21 22:46:16 -04005577out_server_owner:
5578 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005579out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005580 kfree(res.server_scope);
5581out:
Chuck Lever177313f2012-05-21 22:44:58 -04005582 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005583 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005584 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005585 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005586 clp->cl_implid->date.seconds,
5587 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005588 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005589 return status;
5590}
5591
Trond Myklebust66245532012-05-25 17:18:09 -04005592static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5593 struct rpc_cred *cred)
5594{
5595 struct rpc_message msg = {
5596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5597 .rpc_argp = clp,
5598 .rpc_cred = cred,
5599 };
5600 int status;
5601
5602 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5603 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005604 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005605 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5606 return status;
5607}
5608
5609static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5610 struct rpc_cred *cred)
5611{
5612 unsigned int loop;
5613 int ret;
5614
5615 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5616 ret = _nfs4_proc_destroy_clientid(clp, cred);
5617 switch (ret) {
5618 case -NFS4ERR_DELAY:
5619 case -NFS4ERR_CLIENTID_BUSY:
5620 ssleep(1);
5621 break;
5622 default:
5623 return ret;
5624 }
5625 }
5626 return 0;
5627}
5628
5629int nfs4_destroy_clientid(struct nfs_client *clp)
5630{
5631 struct rpc_cred *cred;
5632 int ret = 0;
5633
5634 if (clp->cl_mvops->minor_version < 1)
5635 goto out;
5636 if (clp->cl_exchange_flags == 0)
5637 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005638 if (clp->cl_preserve_clid)
5639 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005640 cred = nfs4_get_exchange_id_cred(clp);
5641 ret = nfs4_proc_destroy_clientid(clp, cred);
5642 if (cred)
5643 put_rpccred(cred);
5644 switch (ret) {
5645 case 0:
5646 case -NFS4ERR_STALE_CLIENTID:
5647 clp->cl_exchange_flags = 0;
5648 }
5649out:
5650 return ret;
5651}
5652
Andy Adamson2050f0c2009-04-01 09:22:30 -04005653struct nfs4_get_lease_time_data {
5654 struct nfs4_get_lease_time_args *args;
5655 struct nfs4_get_lease_time_res *res;
5656 struct nfs_client *clp;
5657};
5658
5659static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5660 void *calldata)
5661{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005662 struct nfs4_get_lease_time_data *data =
5663 (struct nfs4_get_lease_time_data *)calldata;
5664
5665 dprintk("--> %s\n", __func__);
5666 /* just setup sequence, do not trigger session recovery
5667 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005668 nfs41_setup_sequence(data->clp->cl_session,
5669 &data->args->la_seq_args,
5670 &data->res->lr_seq_res,
5671 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005672 dprintk("<-- %s\n", __func__);
5673}
5674
5675/*
5676 * Called from nfs4_state_manager thread for session setup, so don't recover
5677 * from sequence operation or clientid errors.
5678 */
5679static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5680{
5681 struct nfs4_get_lease_time_data *data =
5682 (struct nfs4_get_lease_time_data *)calldata;
5683
5684 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005685 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5686 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005687 switch (task->tk_status) {
5688 case -NFS4ERR_DELAY:
5689 case -NFS4ERR_GRACE:
5690 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5691 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5692 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005693 /* fall through */
5694 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005695 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005696 return;
5697 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005698 dprintk("<-- %s\n", __func__);
5699}
5700
Trond Myklebust17280172012-03-11 13:11:00 -04005701static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005702 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5703 .rpc_call_done = nfs4_get_lease_time_done,
5704};
5705
5706int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5707{
5708 struct rpc_task *task;
5709 struct nfs4_get_lease_time_args args;
5710 struct nfs4_get_lease_time_res res = {
5711 .lr_fsinfo = fsinfo,
5712 };
5713 struct nfs4_get_lease_time_data data = {
5714 .args = &args,
5715 .res = &res,
5716 .clp = clp,
5717 };
5718 struct rpc_message msg = {
5719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5720 .rpc_argp = &args,
5721 .rpc_resp = &res,
5722 };
5723 struct rpc_task_setup task_setup = {
5724 .rpc_client = clp->cl_rpcclient,
5725 .rpc_message = &msg,
5726 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005727 .callback_data = &data,
5728 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005729 };
5730 int status;
5731
Trond Myklebust9d12b212012-01-17 22:04:25 -05005732 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005733 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005734 dprintk("--> %s\n", __func__);
5735 task = rpc_run_task(&task_setup);
5736
5737 if (IS_ERR(task))
5738 status = PTR_ERR(task);
5739 else {
5740 status = task->tk_status;
5741 rpc_put_task(task);
5742 }
5743 dprintk("<-- %s return %d\n", __func__, status);
5744
5745 return status;
5746}
5747
Andy Adamsonfc931582009-04-01 09:22:31 -04005748/*
5749 * Initialize the values to be used by the client in CREATE_SESSION
5750 * If nfs4_init_session set the fore channel request and response sizes,
5751 * use them.
5752 *
5753 * Set the back channel max_resp_sz_cached to zero to force the client to
5754 * always set csa_cachethis to FALSE because the current implementation
5755 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5756 */
5757static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5758{
5759 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005760 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5761 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005762
5763 if (mxrqst_sz == 0)
5764 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5765 if (mxresp_sz == 0)
5766 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5767 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005768 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5769 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005770 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005771 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005772
5773 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005774 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005775 __func__,
5776 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005777 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005778
5779 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005780 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5781 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5782 args->bc_attrs.max_resp_sz_cached = 0;
5783 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5784 args->bc_attrs.max_reqs = 1;
5785
5786 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5787 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5788 __func__,
5789 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5790 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5791 args->bc_attrs.max_reqs);
5792}
5793
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005794static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005795{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005796 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5797 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5798
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005799 if (rcvd->max_resp_sz > sent->max_resp_sz)
5800 return -EINVAL;
5801 /*
5802 * Our requested max_ops is the minimum we need; we're not
5803 * prepared to break up compounds into smaller pieces than that.
5804 * So, no point even trying to continue if the server won't
5805 * cooperate:
5806 */
5807 if (rcvd->max_ops < sent->max_ops)
5808 return -EINVAL;
5809 if (rcvd->max_reqs == 0)
5810 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005811 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5812 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005813 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005814}
5815
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005816static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5817{
5818 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5819 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005820
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005821 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5822 return -EINVAL;
5823 if (rcvd->max_resp_sz < sent->max_resp_sz)
5824 return -EINVAL;
5825 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5826 return -EINVAL;
5827 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005828 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005829 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005830 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005831 return -EINVAL;
5832 return 0;
5833}
Andy Adamson8d353012009-04-01 09:22:32 -04005834
Andy Adamson8d353012009-04-01 09:22:32 -04005835static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5836 struct nfs4_session *session)
5837{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005838 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005839
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005840 ret = nfs4_verify_fore_channel_attrs(args, session);
5841 if (ret)
5842 return ret;
5843 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005844}
5845
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005846static int _nfs4_proc_create_session(struct nfs_client *clp,
5847 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005848{
5849 struct nfs4_session *session = clp->cl_session;
5850 struct nfs41_create_session_args args = {
5851 .client = clp,
5852 .cb_program = NFS4_CALLBACK,
5853 };
5854 struct nfs41_create_session_res res = {
5855 .client = clp,
5856 };
5857 struct rpc_message msg = {
5858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5859 .rpc_argp = &args,
5860 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005861 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005862 };
5863 int status;
5864
5865 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005866 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005867
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005868 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005869
Trond Myklebust43095d32012-11-20 11:13:12 -05005870 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005871 /* Verify the session's negotiated channel_attrs values */
5872 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005873 /* Increment the clientid slot sequence id */
5874 clp->cl_seqid++;
5875 }
5876
5877 return status;
5878}
5879
5880/*
5881 * Issues a CREATE_SESSION operation to the server.
5882 * It is the responsibility of the caller to verify the session is
5883 * expired before calling this routine.
5884 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005885int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005886{
5887 int status;
5888 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005889 struct nfs4_session *session = clp->cl_session;
5890
5891 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5892
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005893 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005894 if (status)
5895 goto out;
5896
Andy Adamsonaacd5532011-11-09 13:58:21 -05005897 /* Init or reset the session slot tables */
5898 status = nfs4_setup_session_slot_tables(session);
5899 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005900 if (status)
5901 goto out;
5902
5903 ptr = (unsigned *)&session->sess_id.data[0];
5904 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5905 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005906out:
5907 dprintk("<-- %s\n", __func__);
5908 return status;
5909}
5910
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005911/*
5912 * Issue the over-the-wire RPC DESTROY_SESSION.
5913 * The caller must serialize access to this routine.
5914 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005915int nfs4_proc_destroy_session(struct nfs4_session *session,
5916 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005917{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005918 struct rpc_message msg = {
5919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5920 .rpc_argp = session,
5921 .rpc_cred = cred,
5922 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005923 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005924
5925 dprintk("--> nfs4_proc_destroy_session\n");
5926
5927 /* session is still being setup */
5928 if (session->clp->cl_cons_state != NFS_CS_READY)
5929 return status;
5930
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005931 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005932
5933 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005934 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005935 "Session has been destroyed regardless...\n", status);
5936
5937 dprintk("<-- nfs4_proc_destroy_session\n");
5938 return status;
5939}
5940
Trond Myklebust7b38c362012-05-23 13:23:31 -04005941/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005942 * Renew the cl_session lease.
5943 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005944struct nfs4_sequence_data {
5945 struct nfs_client *clp;
5946 struct nfs4_sequence_args args;
5947 struct nfs4_sequence_res res;
5948};
5949
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005950static void nfs41_sequence_release(void *data)
5951{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005952 struct nfs4_sequence_data *calldata = data;
5953 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005954
Alexandros Batsakis71358402010-02-05 03:45:05 -08005955 if (atomic_read(&clp->cl_count) > 1)
5956 nfs4_schedule_state_renewal(clp);
5957 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005958 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005959}
5960
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005961static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5962{
5963 switch(task->tk_status) {
5964 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005965 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5966 return -EAGAIN;
5967 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005968 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005969 }
5970 return 0;
5971}
5972
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005973static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005975 struct nfs4_sequence_data *calldata = data;
5976 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977
Trond Myklebust14516c32010-07-31 14:29:06 -04005978 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5979 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005980
5981 if (task->tk_status < 0) {
5982 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005983 if (atomic_read(&clp->cl_count) == 1)
5984 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005985
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005986 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5987 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005988 return;
5989 }
5990 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005991 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005992out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005993 dprintk("<-- %s\n", __func__);
5994}
5995
5996static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5997{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005998 struct nfs4_sequence_data *calldata = data;
5999 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006000 struct nfs4_sequence_args *args;
6001 struct nfs4_sequence_res *res;
6002
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006003 args = task->tk_msg.rpc_argp;
6004 res = task->tk_msg.rpc_resp;
6005
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006006 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006007}
6008
6009static const struct rpc_call_ops nfs41_sequence_ops = {
6010 .rpc_call_done = nfs41_sequence_call_done,
6011 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006012 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006013};
6014
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006015static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6016 struct rpc_cred *cred,
6017 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006018{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006019 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6022 .rpc_cred = cred,
6023 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024 struct rpc_task_setup task_setup_data = {
6025 .rpc_client = clp->cl_rpcclient,
6026 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006027 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006028 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006029 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006030
Alexandros Batsakis71358402010-02-05 03:45:05 -08006031 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006032 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006033 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006034 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006035 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006036 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006037 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006038 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006039 if (is_privileged)
6040 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006041 msg.rpc_argp = &calldata->args;
6042 msg.rpc_resp = &calldata->res;
6043 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006044 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006045
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006046 return rpc_run_task(&task_setup_data);
6047}
6048
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006049static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006050{
6051 struct rpc_task *task;
6052 int ret = 0;
6053
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006054 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6055 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006056 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006057 if (IS_ERR(task))
6058 ret = PTR_ERR(task);
6059 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006060 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006061 dprintk("<-- %s status=%d\n", __func__, ret);
6062 return ret;
6063}
6064
6065static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6066{
6067 struct rpc_task *task;
6068 int ret;
6069
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006070 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006071 if (IS_ERR(task)) {
6072 ret = PTR_ERR(task);
6073 goto out;
6074 }
6075 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006076 if (!ret) {
6077 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6078
6079 if (task->tk_status == 0)
6080 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006081 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006082 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083 rpc_put_task(task);
6084out:
6085 dprintk("<-- %s status=%d\n", __func__, ret);
6086 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006087}
6088
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006089struct nfs4_reclaim_complete_data {
6090 struct nfs_client *clp;
6091 struct nfs41_reclaim_complete_args arg;
6092 struct nfs41_reclaim_complete_res res;
6093};
6094
6095static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6096{
6097 struct nfs4_reclaim_complete_data *calldata = data;
6098
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006099 nfs41_setup_sequence(calldata->clp->cl_session,
6100 &calldata->arg.seq_args,
6101 &calldata->res.seq_res,
6102 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006103}
6104
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006105static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6106{
6107 switch(task->tk_status) {
6108 case 0:
6109 case -NFS4ERR_COMPLETE_ALREADY:
6110 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6111 break;
6112 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006113 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006114 /* fall through */
6115 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006116 return -EAGAIN;
6117 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006118 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006119 }
6120 return 0;
6121}
6122
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006123static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6124{
6125 struct nfs4_reclaim_complete_data *calldata = data;
6126 struct nfs_client *clp = calldata->clp;
6127 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6128
6129 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006130 if (!nfs41_sequence_done(task, res))
6131 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006132
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006133 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6134 rpc_restart_call_prepare(task);
6135 return;
6136 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006137 dprintk("<-- %s\n", __func__);
6138}
6139
6140static void nfs4_free_reclaim_complete_data(void *data)
6141{
6142 struct nfs4_reclaim_complete_data *calldata = data;
6143
6144 kfree(calldata);
6145}
6146
6147static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6148 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6149 .rpc_call_done = nfs4_reclaim_complete_done,
6150 .rpc_release = nfs4_free_reclaim_complete_data,
6151};
6152
6153/*
6154 * Issue a global reclaim complete.
6155 */
6156static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6157{
6158 struct nfs4_reclaim_complete_data *calldata;
6159 struct rpc_task *task;
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6162 };
6163 struct rpc_task_setup task_setup_data = {
6164 .rpc_client = clp->cl_rpcclient,
6165 .rpc_message = &msg,
6166 .callback_ops = &nfs4_reclaim_complete_call_ops,
6167 .flags = RPC_TASK_ASYNC,
6168 };
6169 int status = -ENOMEM;
6170
6171 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006172 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006173 if (calldata == NULL)
6174 goto out;
6175 calldata->clp = clp;
6176 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006177
Trond Myklebust9d12b212012-01-17 22:04:25 -05006178 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006179 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006180 msg.rpc_argp = &calldata->arg;
6181 msg.rpc_resp = &calldata->res;
6182 task_setup_data.callback_data = calldata;
6183 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006184 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006185 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006186 goto out;
6187 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006188 status = nfs4_wait_for_completion_rpc_task(task);
6189 if (status == 0)
6190 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006191 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006192 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006193out:
6194 dprintk("<-- %s status=%d\n", __func__, status);
6195 return status;
6196}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197
6198static void
6199nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6200{
6201 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006202 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006203 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006204
6205 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006206 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6207 * right now covering the LAYOUTGET we are about to send.
6208 * However, that is not so catastrophic, and there seems
6209 * to be no way to prevent it completely.
6210 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006211 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006212 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006213 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006214 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6215 NFS_I(lgp->args.inode)->layout,
6216 lgp->args.ctx->state)) {
6217 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006218 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006219}
6220
6221static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6222{
6223 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006224 struct inode *inode = lgp->args.inode;
6225 struct nfs_server *server = NFS_SERVER(inode);
6226 struct pnfs_layout_hdr *lo;
6227 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006228 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006229
6230 dprintk("--> %s\n", __func__);
6231
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006232 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006233 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006234
6235 switch (task->tk_status) {
6236 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006237 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006238 case -NFS4ERR_LAYOUTTRYLATER:
6239 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006240 timeo = rpc_get_timeout(task->tk_client);
6241 giveup = lgp->args.timestamp + timeo;
6242 if (time_after(giveup, jiffies))
6243 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006244 break;
6245 case -NFS4ERR_EXPIRED:
6246 case -NFS4ERR_BAD_STATEID:
6247 spin_lock(&inode->i_lock);
6248 lo = NFS_I(inode)->layout;
6249 if (!lo || list_empty(&lo->plh_segs)) {
6250 spin_unlock(&inode->i_lock);
6251 /* If the open stateid was bad, then recover it. */
6252 state = lgp->args.ctx->state;
6253 } else {
6254 LIST_HEAD(head);
6255
6256 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6257 spin_unlock(&inode->i_lock);
6258 /* Mark the bad layout state as invalid, then
6259 * retry using the open stateid. */
6260 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006261 }
6262 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006263 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6264 rpc_restart_call_prepare(task);
6265out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266 dprintk("<-- %s\n", __func__);
6267}
6268
Idan Kedar85541162012-08-02 11:47:10 +03006269static size_t max_response_pages(struct nfs_server *server)
6270{
6271 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6272 return nfs_page_array_len(0, max_resp_sz);
6273}
6274
6275static void nfs4_free_pages(struct page **pages, size_t size)
6276{
6277 int i;
6278
6279 if (!pages)
6280 return;
6281
6282 for (i = 0; i < size; i++) {
6283 if (!pages[i])
6284 break;
6285 __free_page(pages[i]);
6286 }
6287 kfree(pages);
6288}
6289
6290static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6291{
6292 struct page **pages;
6293 int i;
6294
6295 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6296 if (!pages) {
6297 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6298 return NULL;
6299 }
6300
6301 for (i = 0; i < size; i++) {
6302 pages[i] = alloc_page(gfp_flags);
6303 if (!pages[i]) {
6304 dprintk("%s: failed to allocate page\n", __func__);
6305 nfs4_free_pages(pages, size);
6306 return NULL;
6307 }
6308 }
6309
6310 return pages;
6311}
6312
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006313static void nfs4_layoutget_release(void *calldata)
6314{
6315 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006316 struct inode *inode = lgp->args.inode;
6317 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006318 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006319
6320 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006321 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006322 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323 put_nfs_open_context(lgp->args.ctx);
6324 kfree(calldata);
6325 dprintk("<-- %s\n", __func__);
6326}
6327
6328static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6329 .rpc_call_prepare = nfs4_layoutget_prepare,
6330 .rpc_call_done = nfs4_layoutget_done,
6331 .rpc_release = nfs4_layoutget_release,
6332};
6333
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006334struct pnfs_layout_segment *
6335nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006336{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006337 struct inode *inode = lgp->args.inode;
6338 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006339 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006340 struct rpc_task *task;
6341 struct rpc_message msg = {
6342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6343 .rpc_argp = &lgp->args,
6344 .rpc_resp = &lgp->res,
6345 };
6346 struct rpc_task_setup task_setup_data = {
6347 .rpc_client = server->client,
6348 .rpc_message = &msg,
6349 .callback_ops = &nfs4_layoutget_call_ops,
6350 .callback_data = lgp,
6351 .flags = RPC_TASK_ASYNC,
6352 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006353 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006354 int status = 0;
6355
6356 dprintk("--> %s\n", __func__);
6357
Idan Kedar85541162012-08-02 11:47:10 +03006358 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6359 if (!lgp->args.layout.pages) {
6360 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006361 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006362 }
6363 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006364 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006365
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006366 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006367 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006368 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006369
6370 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6371 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6372
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006373 task = rpc_run_task(&task_setup_data);
6374 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006375 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006376 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006377 if (status == 0)
6378 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006379 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6380 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006381 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006382 rpc_put_task(task);
6383 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006384 if (status)
6385 return ERR_PTR(status);
6386 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006387}
6388
Benny Halevycbe82602011-05-22 19:52:37 +03006389static void
6390nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6391{
6392 struct nfs4_layoutreturn *lrp = calldata;
6393
6394 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006395 nfs41_setup_sequence(lrp->clp->cl_session,
6396 &lrp->args.seq_args,
6397 &lrp->res.seq_res,
6398 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006399}
6400
6401static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6402{
6403 struct nfs4_layoutreturn *lrp = calldata;
6404 struct nfs_server *server;
6405
6406 dprintk("--> %s\n", __func__);
6407
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006408 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006409 return;
6410
6411 server = NFS_SERVER(lrp->args.inode);
6412 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006413 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006414 return;
6415 }
Benny Halevycbe82602011-05-22 19:52:37 +03006416 dprintk("<-- %s\n", __func__);
6417}
6418
6419static void nfs4_layoutreturn_release(void *calldata)
6420{
6421 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006422 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006423
6424 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006425 spin_lock(&lo->plh_inode->i_lock);
6426 if (lrp->res.lrs_present)
6427 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6428 lo->plh_block_lgets--;
6429 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006430 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006431 kfree(calldata);
6432 dprintk("<-- %s\n", __func__);
6433}
6434
6435static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6436 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6437 .rpc_call_done = nfs4_layoutreturn_done,
6438 .rpc_release = nfs4_layoutreturn_release,
6439};
6440
6441int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6442{
6443 struct rpc_task *task;
6444 struct rpc_message msg = {
6445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6446 .rpc_argp = &lrp->args,
6447 .rpc_resp = &lrp->res,
6448 };
6449 struct rpc_task_setup task_setup_data = {
6450 .rpc_client = lrp->clp->cl_rpcclient,
6451 .rpc_message = &msg,
6452 .callback_ops = &nfs4_layoutreturn_call_ops,
6453 .callback_data = lrp,
6454 };
6455 int status;
6456
6457 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006458 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006459 task = rpc_run_task(&task_setup_data);
6460 if (IS_ERR(task))
6461 return PTR_ERR(task);
6462 status = task->tk_status;
6463 dprintk("<-- %s status=%d\n", __func__, status);
6464 rpc_put_task(task);
6465 return status;
6466}
6467
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006468/*
6469 * Retrieve the list of Data Server devices from the MDS.
6470 */
6471static int _nfs4_getdevicelist(struct nfs_server *server,
6472 const struct nfs_fh *fh,
6473 struct pnfs_devicelist *devlist)
6474{
6475 struct nfs4_getdevicelist_args args = {
6476 .fh = fh,
6477 .layoutclass = server->pnfs_curr_ld->id,
6478 };
6479 struct nfs4_getdevicelist_res res = {
6480 .devlist = devlist,
6481 };
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6484 .rpc_argp = &args,
6485 .rpc_resp = &res,
6486 };
6487 int status;
6488
6489 dprintk("--> %s\n", __func__);
6490 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6491 &res.seq_res, 0);
6492 dprintk("<-- %s status=%d\n", __func__, status);
6493 return status;
6494}
6495
6496int nfs4_proc_getdevicelist(struct nfs_server *server,
6497 const struct nfs_fh *fh,
6498 struct pnfs_devicelist *devlist)
6499{
6500 struct nfs4_exception exception = { };
6501 int err;
6502
6503 do {
6504 err = nfs4_handle_exception(server,
6505 _nfs4_getdevicelist(server, fh, devlist),
6506 &exception);
6507 } while (exception.retry);
6508
6509 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6510 err, devlist->num_devs);
6511
6512 return err;
6513}
6514EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6515
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006516static int
6517_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6518{
6519 struct nfs4_getdeviceinfo_args args = {
6520 .pdev = pdev,
6521 };
6522 struct nfs4_getdeviceinfo_res res = {
6523 .pdev = pdev,
6524 };
6525 struct rpc_message msg = {
6526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6527 .rpc_argp = &args,
6528 .rpc_resp = &res,
6529 };
6530 int status;
6531
6532 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006533 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006534 dprintk("<-- %s status=%d\n", __func__, status);
6535
6536 return status;
6537}
6538
6539int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6540{
6541 struct nfs4_exception exception = { };
6542 int err;
6543
6544 do {
6545 err = nfs4_handle_exception(server,
6546 _nfs4_proc_getdeviceinfo(server, pdev),
6547 &exception);
6548 } while (exception.retry);
6549 return err;
6550}
6551EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6552
Andy Adamson863a3c62011-03-23 13:27:54 +00006553static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6554{
6555 struct nfs4_layoutcommit_data *data = calldata;
6556 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006557 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006558
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006559 nfs41_setup_sequence(session,
6560 &data->args.seq_args,
6561 &data->res.seq_res,
6562 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006563}
6564
6565static void
6566nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6567{
6568 struct nfs4_layoutcommit_data *data = calldata;
6569 struct nfs_server *server = NFS_SERVER(data->args.inode);
6570
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006571 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006572 return;
6573
6574 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006575 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6576 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6577 case -NFS4ERR_BADLAYOUT: /* no layout */
6578 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006579 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006580 break;
6581 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006582 nfs_post_op_update_inode_force_wcc(data->args.inode,
6583 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006584 break;
6585 default:
6586 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6587 rpc_restart_call_prepare(task);
6588 return;
6589 }
6590 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006591}
6592
6593static void nfs4_layoutcommit_release(void *calldata)
6594{
6595 struct nfs4_layoutcommit_data *data = calldata;
6596
Andy Adamsondb29c082011-07-30 20:52:38 -04006597 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006598 put_rpccred(data->cred);
6599 kfree(data);
6600}
6601
6602static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6603 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6604 .rpc_call_done = nfs4_layoutcommit_done,
6605 .rpc_release = nfs4_layoutcommit_release,
6606};
6607
6608int
Andy Adamsonef311532011-03-12 02:58:10 -05006609nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006610{
6611 struct rpc_message msg = {
6612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6613 .rpc_argp = &data->args,
6614 .rpc_resp = &data->res,
6615 .rpc_cred = data->cred,
6616 };
6617 struct rpc_task_setup task_setup_data = {
6618 .task = &data->task,
6619 .rpc_client = NFS_CLIENT(data->args.inode),
6620 .rpc_message = &msg,
6621 .callback_ops = &nfs4_layoutcommit_ops,
6622 .callback_data = data,
6623 .flags = RPC_TASK_ASYNC,
6624 };
6625 struct rpc_task *task;
6626 int status = 0;
6627
6628 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6629 "lbw: %llu inode %lu\n",
6630 data->task.tk_pid, sync,
6631 data->args.lastbytewritten,
6632 data->args.inode->i_ino);
6633
Trond Myklebust9d12b212012-01-17 22:04:25 -05006634 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006635 task = rpc_run_task(&task_setup_data);
6636 if (IS_ERR(task))
6637 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006638 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006639 goto out;
6640 status = nfs4_wait_for_completion_rpc_task(task);
6641 if (status != 0)
6642 goto out;
6643 status = task->tk_status;
6644out:
6645 dprintk("%s: status %d\n", __func__, status);
6646 rpc_put_task(task);
6647 return status;
6648}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006649
6650static int
6651_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6652 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6653{
6654 struct nfs41_secinfo_no_name_args args = {
6655 .style = SECINFO_STYLE_CURRENT_FH,
6656 };
6657 struct nfs4_secinfo_res res = {
6658 .flavors = flavors,
6659 };
6660 struct rpc_message msg = {
6661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6662 .rpc_argp = &args,
6663 .rpc_resp = &res,
6664 };
6665 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6666}
6667
6668static int
6669nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6670 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6671{
6672 struct nfs4_exception exception = { };
6673 int err;
6674 do {
6675 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6676 switch (err) {
6677 case 0:
6678 case -NFS4ERR_WRONGSEC:
6679 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006680 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006681 default:
6682 err = nfs4_handle_exception(server, err, &exception);
6683 }
6684 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006685out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006686 return err;
6687}
6688
6689static int
6690nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6691 struct nfs_fsinfo *info)
6692{
6693 int err;
6694 struct page *page;
6695 rpc_authflavor_t flavor;
6696 struct nfs4_secinfo_flavors *flavors;
6697
6698 page = alloc_page(GFP_KERNEL);
6699 if (!page) {
6700 err = -ENOMEM;
6701 goto out;
6702 }
6703
6704 flavors = page_address(page);
6705 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6706
6707 /*
6708 * Fall back on "guess and check" method if
6709 * the server doesn't support SECINFO_NO_NAME
6710 */
6711 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6712 err = nfs4_find_root_sec(server, fhandle, info);
6713 goto out_freepage;
6714 }
6715 if (err)
6716 goto out_freepage;
6717
6718 flavor = nfs_find_best_sec(flavors);
6719 if (err == 0)
6720 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6721
6722out_freepage:
6723 put_page(page);
6724 if (err == -EACCES)
6725 return -EPERM;
6726out:
6727 return err;
6728}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006729
6730static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006731{
6732 int status;
6733 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006734 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006735 };
6736 struct nfs41_test_stateid_res res;
6737 struct rpc_message msg = {
6738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6739 .rpc_argp = &args,
6740 .rpc_resp = &res,
6741 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006742
Chuck Lever38527b12012-07-11 16:30:23 -04006743 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006744 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006745 nfs4_set_sequence_privileged(&args.seq_args);
6746 status = nfs4_call_sync_sequence(server->client, server, &msg,
6747 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006748 if (status != NFS_OK) {
6749 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006750 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006751 }
6752 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006753 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006754}
6755
Chuck Lever38527b12012-07-11 16:30:23 -04006756/**
6757 * nfs41_test_stateid - perform a TEST_STATEID operation
6758 *
6759 * @server: server / transport on which to perform the operation
6760 * @stateid: state ID to test
6761 *
6762 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6763 * Otherwise a negative NFS4ERR value is returned if the operation
6764 * failed or the state ID is not currently valid.
6765 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006766static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006767{
6768 struct nfs4_exception exception = { };
6769 int err;
6770 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006771 err = _nfs41_test_stateid(server, stateid);
6772 if (err != -NFS4ERR_DELAY)
6773 break;
6774 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006775 } while (exception.retry);
6776 return err;
6777}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006778
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006779static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006780{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006781 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006782 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006783 };
6784 struct nfs41_free_stateid_res res;
6785 struct rpc_message msg = {
6786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6787 .rpc_argp = &args,
6788 .rpc_resp = &res,
6789 };
Chuck Lever38527b12012-07-11 16:30:23 -04006790 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006791
Chuck Lever38527b12012-07-11 16:30:23 -04006792 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006793 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006794 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006795 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006796 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006797 dprintk("NFS reply free_stateid: %d\n", status);
6798 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006799}
6800
Chuck Lever38527b12012-07-11 16:30:23 -04006801/**
6802 * nfs41_free_stateid - perform a FREE_STATEID operation
6803 *
6804 * @server: server / transport on which to perform the operation
6805 * @stateid: state ID to release
6806 *
6807 * Returns NFS_OK if the server freed "stateid". Otherwise a
6808 * negative NFS4ERR value is returned.
6809 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006810static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006811{
6812 struct nfs4_exception exception = { };
6813 int err;
6814 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006815 err = _nfs4_free_stateid(server, stateid);
6816 if (err != -NFS4ERR_DELAY)
6817 break;
6818 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006819 } while (exception.retry);
6820 return err;
6821}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006822
6823static bool nfs41_match_stateid(const nfs4_stateid *s1,
6824 const nfs4_stateid *s2)
6825{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006826 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006827 return false;
6828
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006829 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006830 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006831 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006832 return true;
6833
6834 return false;
6835}
6836
Andy Adamson557134a2009-04-01 09:21:53 -04006837#endif /* CONFIG_NFS_V4_1 */
6838
Trond Myklebust36281ca2012-03-04 18:13:56 -05006839static bool nfs4_match_stateid(const nfs4_stateid *s1,
6840 const nfs4_stateid *s2)
6841{
Trond Myklebustf597c532012-03-04 18:13:56 -05006842 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006843}
6844
6845
Trond Myklebust17280172012-03-11 13:11:00 -04006846static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006847 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006848 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 .recover_open = nfs4_open_reclaim,
6850 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006851 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006852 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006853 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854};
6855
Andy Adamson591d71c2009-04-01 09:22:47 -04006856#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006857static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006858 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6859 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6860 .recover_open = nfs4_open_reclaim,
6861 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006862 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006863 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006864 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006865 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006866};
6867#endif /* CONFIG_NFS_V4_1 */
6868
Trond Myklebust17280172012-03-11 13:11:00 -04006869static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006870 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006871 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 .recover_open = nfs4_open_expired,
6873 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006874 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006875 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876};
6877
Andy Adamson591d71c2009-04-01 09:22:47 -04006878#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006879static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006880 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6881 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006882 .recover_open = nfs41_open_expired,
6883 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006884 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006885 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006886};
6887#endif /* CONFIG_NFS_V4_1 */
6888
Trond Myklebust17280172012-03-11 13:11:00 -04006889static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006890 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006891 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006892 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006893};
6894
6895#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006896static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006897 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006898 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006899 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006900};
6901#endif
6902
Trond Myklebust97dc1352010-06-16 09:52:26 -04006903static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6904 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006905 .init_caps = NFS_CAP_READDIRPLUS
6906 | NFS_CAP_ATOMIC_OPEN
6907 | NFS_CAP_CHANGE_ATTR
6908 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006909 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006910 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006911 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006912 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6913 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6914 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006915};
6916
6917#if defined(CONFIG_NFS_V4_1)
6918static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6919 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006920 .init_caps = NFS_CAP_READDIRPLUS
6921 | NFS_CAP_ATOMIC_OPEN
6922 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006923 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006924 | NFS_CAP_STATEID_NFSV41
6925 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006926 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006927 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006928 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006929 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6930 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6931 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006932};
6933#endif
6934
Trond Myklebust97dc1352010-06-16 09:52:26 -04006935const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6936 [0] = &nfs_v4_0_minor_ops,
6937#if defined(CONFIG_NFS_V4_1)
6938 [1] = &nfs_v4_1_minor_ops,
6939#endif
6940};
6941
Bryan Schumaker73a79702012-07-16 16:39:12 -04006942const struct inode_operations nfs4_dir_inode_operations = {
6943 .create = nfs_create,
6944 .lookup = nfs_lookup,
6945 .atomic_open = nfs_atomic_open,
6946 .link = nfs_link,
6947 .unlink = nfs_unlink,
6948 .symlink = nfs_symlink,
6949 .mkdir = nfs_mkdir,
6950 .rmdir = nfs_rmdir,
6951 .mknod = nfs_mknod,
6952 .rename = nfs_rename,
6953 .permission = nfs_permission,
6954 .getattr = nfs_getattr,
6955 .setattr = nfs_setattr,
6956 .getxattr = generic_getxattr,
6957 .setxattr = generic_setxattr,
6958 .listxattr = generic_listxattr,
6959 .removexattr = generic_removexattr,
6960};
6961
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006962static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006963 .permission = nfs_permission,
6964 .getattr = nfs_getattr,
6965 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006966 .getxattr = generic_getxattr,
6967 .setxattr = generic_setxattr,
6968 .listxattr = generic_listxattr,
6969 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006970};
6971
David Howells509de812006-08-22 20:06:11 -04006972const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 .version = 4, /* protocol version */
6974 .dentry_ops = &nfs4_dentry_operations,
6975 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006976 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006977 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006979 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006980 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 .getattr = nfs4_proc_getattr,
6982 .setattr = nfs4_proc_setattr,
6983 .lookup = nfs4_proc_lookup,
6984 .access = nfs4_proc_access,
6985 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 .create = nfs4_proc_create,
6987 .remove = nfs4_proc_remove,
6988 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006989 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 .unlink_done = nfs4_proc_unlink_done,
6991 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006992 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006993 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006994 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 .link = nfs4_proc_link,
6996 .symlink = nfs4_proc_symlink,
6997 .mkdir = nfs4_proc_mkdir,
6998 .rmdir = nfs4_proc_remove,
6999 .readdir = nfs4_proc_readdir,
7000 .mknod = nfs4_proc_mknod,
7001 .statfs = nfs4_proc_statfs,
7002 .fsinfo = nfs4_proc_fsinfo,
7003 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007004 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 .decode_dirent = nfs4_decode_dirent,
7006 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007007 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007008 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007009 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007011 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007012 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007013 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007015 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007016 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007018 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007019 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007020 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007021 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007022 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007023 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007024 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007025 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007026 .create_server = nfs4_create_server,
7027 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028};
7029
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007030static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7031 .prefix = XATTR_NAME_NFSV4_ACL,
7032 .list = nfs4_xattr_list_nfs4_acl,
7033 .get = nfs4_xattr_get_nfs4_acl,
7034 .set = nfs4_xattr_set_nfs4_acl,
7035};
7036
7037const struct xattr_handler *nfs4_xattr_handlers[] = {
7038 &nfs4_xattr_nfs4_acl_handler,
7039 NULL
7040};
7041
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042/*
7043 * Local variables:
7044 * c-basic-offset: 8
7045 * End:
7046 */