blob: e78b8c2656ee9d120ce164ffd22b828c1d1dace5 [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);
Andy Adamson774d5f12013-05-20 14:13:50 -0400575 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_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 Myklebustf448bad2013-05-29 15:36:40 -04001081 int open_mode = opendata->o_arg.open_flags;
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
Weston Andros Adamson231817e2013-10-21 13:10:13 -04001162 if (!data->rpc_done) {
1163 if (data->rpc_status) {
1164 ret = data->rpc_status;
1165 goto err;
1166 }
1167 /* cached opens have already been processed */
1168 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001169 }
1170
Andy Adamsone23008e2012-10-02 21:07:32 -04001171 ret = nfs_refresh_inode(inode, &data->f_attr);
1172 if (ret)
1173 goto err;
1174
1175 if (data->o_res.delegation_type != 0)
1176 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamson231817e2013-10-21 13:10:13 -04001177update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001178 update_open_stateid(state, &data->o_res.stateid, NULL,
1179 data->o_arg.fmode);
Trond Myklebust13111572013-10-28 14:57:12 -04001180 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001181
1182 return state;
1183err:
1184 return ERR_PTR(ret);
1185
1186}
1187
1188static struct nfs4_state *
1189_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190{
1191 struct inode *inode;
1192 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001193 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001194
Trond Myklebustaac00a82007-07-05 19:02:21 -04001195 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001196 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001197 goto out;
1198 }
1199
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001201 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001203 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001204 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001205 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err;
1207 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208 state = nfs4_get_open_state(inode, data->owner);
1209 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001211 if (data->o_res.delegation_type != 0)
1212 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001215 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001216out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001217 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219err_put_inode:
1220 iput(inode);
1221err:
1222 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001223}
1224
Andy Adamsone23008e2012-10-02 21:07:32 -04001225static struct nfs4_state *
1226nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1227{
1228 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1229 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1230 return _nfs4_opendata_to_nfs4_state(data);
1231}
1232
Trond Myklebust864472e2006-01-03 09:55:15 +01001233static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1234{
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_open_context *ctx;
1237
1238 spin_lock(&state->inode->i_lock);
1239 list_for_each_entry(ctx, &nfsi->open_files, list) {
1240 if (ctx->state != state)
1241 continue;
1242 get_nfs_open_context(ctx);
1243 spin_unlock(&state->inode->i_lock);
1244 return ctx;
1245 }
1246 spin_unlock(&state->inode->i_lock);
1247 return ERR_PTR(-ENOENT);
1248}
1249
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001250static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1251 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001252{
1253 struct nfs4_opendata *opendata;
1254
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1256 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001257 if (opendata == NULL)
1258 return ERR_PTR(-ENOMEM);
1259 opendata->state = state;
1260 atomic_inc(&state->count);
1261 return opendata;
1262}
1263
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001264static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001265{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001266 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
1268
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269 opendata->o_arg.open_flags = 0;
1270 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001271 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1272 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1273 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001274 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 if (ret != 0)
1276 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 if (IS_ERR(newstate))
1279 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001280 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 return 0;
1283}
1284
1285static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1286{
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 int ret;
1289
1290 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001292 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 smp_rmb();
1294 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001295 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001296 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 if (ret != 0)
1298 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 if (newstate != state)
1300 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 }
1302 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001303 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001304 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 if (ret != 0)
1306 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 if (newstate != state)
1308 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 }
1310 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001311 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001312 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 if (ret != 0)
1314 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001315 if (newstate != state)
1316 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001318 /*
1319 * We may have performed cached opens for all three recoveries.
1320 * Check if we need to update the current stateid.
1321 */
1322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001323 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001324 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001325 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001326 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001327 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001328 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 return 0;
1330}
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332/*
1333 * OPEN_RECLAIM:
1334 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001336static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001338 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int status;
1342
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001343 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1344 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001345 if (IS_ERR(opendata))
1346 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001347 rcu_read_lock();
1348 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001349 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001350 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001351 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 opendata->o_arg.u.delegation_type = delegation_type;
1353 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001354 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return status;
1356}
1357
Trond Myklebust539cd032007-06-05 11:46:42 -04001358static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct nfs_server *server = NFS_SERVER(state->inode);
1361 struct nfs4_exception exception = { };
1362 int err;
1363 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001364 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001365 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1366 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001367 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001368 break;
1369 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 } while (exception.retry);
1371 return err;
1372}
1373
Trond Myklebust864472e2006-01-03 09:55:15 +01001374static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1375{
1376 struct nfs_open_context *ctx;
1377 int ret;
1378
1379 ctx = nfs4_state_find_open_context(state);
1380 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001381 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001382 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 put_nfs_open_context(ctx);
1384 return ret;
1385}
1386
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001387static 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 -07001388{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001389 switch (err) {
1390 default:
1391 printk(KERN_ERR "NFS: %s: unhandled error "
1392 "%d.\n", __func__, err);
1393 case 0:
1394 case -ENOENT:
1395 case -ESTALE:
1396 break;
1397 case -NFS4ERR_BADSESSION:
1398 case -NFS4ERR_BADSLOT:
1399 case -NFS4ERR_BAD_HIGH_SLOT:
1400 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1401 case -NFS4ERR_DEADSESSION:
1402 set_bit(NFS_DELEGATED_STATE, &state->flags);
1403 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1404 return -EAGAIN;
1405 case -NFS4ERR_STALE_CLIENTID:
1406 case -NFS4ERR_STALE_STATEID:
1407 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 case -NFS4ERR_EXPIRED:
1409 /* Don't recall a delegation if it was lost */
1410 nfs4_schedule_lease_recovery(server->nfs_client);
1411 return -EAGAIN;
1412 case -NFS4ERR_DELEG_REVOKED:
1413 case -NFS4ERR_ADMIN_REVOKED:
1414 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001415 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001416 nfs_inode_find_state_and_recover(state->inode,
1417 stateid);
1418 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001419 return 0;
1420 case -NFS4ERR_DELAY:
1421 case -NFS4ERR_GRACE:
1422 set_bit(NFS_DELEGATED_STATE, &state->flags);
1423 ssleep(1);
1424 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001425 case -ENOMEM:
1426 case -NFS4ERR_DENIED:
1427 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1428 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return err;
1431}
1432
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001433int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1434{
1435 struct nfs_server *server = NFS_SERVER(state->inode);
1436 struct nfs4_opendata *opendata;
1437 int err;
1438
1439 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1440 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1441 if (IS_ERR(opendata))
1442 return PTR_ERR(opendata);
1443 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1444 err = nfs4_open_recover(opendata, state);
1445 nfs4_opendata_put(opendata);
1446 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1447}
1448
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1450{
1451 struct nfs4_opendata *data = calldata;
1452
1453 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001455 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001456 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001458 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460}
1461
1462static void nfs4_open_confirm_release(void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
1465 struct nfs4_state *state = NULL;
1466
1467 /* If this request hasn't been cancelled, do nothing */
1468 if (data->cancelled == 0)
1469 goto out_free;
1470 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001471 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001474 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001475 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001477 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001478}
1479
1480static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001481 .rpc_call_done = nfs4_open_confirm_done,
1482 .rpc_release = nfs4_open_confirm_release,
1483};
1484
1485/*
1486 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1487 */
1488static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1489{
1490 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1491 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001492 struct rpc_message msg = {
1493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1494 .rpc_argp = &data->c_arg,
1495 .rpc_resp = &data->c_res,
1496 .rpc_cred = data->owner->so_cred,
1497 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001498 struct rpc_task_setup task_setup_data = {
1499 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001500 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001501 .callback_ops = &nfs4_open_confirm_ops,
1502 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001503 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001504 .flags = RPC_TASK_ASYNC,
1505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int status;
1507
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001508 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001509 data->rpc_done = 0;
1510 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001511 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001512 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001513 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001514 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001515 status = nfs4_wait_for_completion_rpc_task(task);
1516 if (status != 0) {
1517 data->cancelled = 1;
1518 smp_wmb();
1519 } else
1520 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001521 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return status;
1523}
1524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001529 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001530
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001532 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001533 /*
1534 * Check if we still need to send an OPEN call, or if we can use
1535 * a delegation instead.
1536 */
1537 if (data->state != NULL) {
1538 struct nfs_delegation *delegation;
1539
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001541 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542 rcu_read_lock();
1543 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001544 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001545 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001546 can_open_delegated(delegation, data->o_arg.fmode))
1547 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548 rcu_read_unlock();
1549 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001550 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001551 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001552 switch (data->o_arg.claim) {
1553 case NFS4_OPEN_CLAIM_PREVIOUS:
1554 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1555 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001556 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001557 case NFS4_OPEN_CLAIM_FH:
1558 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001559 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1560 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001561 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001562 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001563 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001564 &data->o_res.seq_res,
1565 task) != 0)
1566 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001567
1568 /* Set the create mode (note dependency on the session type) */
1569 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1570 if (data->o_arg.open_flags & O_EXCL) {
1571 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1572 if (nfs4_has_persistent_session(clp))
1573 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1574 else if (clp->cl_mvops->minor_version > 0)
1575 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1576 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001577 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001578unlock_no_action:
1579 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001580out_no_action:
1581 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001582out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001583 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584}
1585
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586static void nfs4_open_done(struct rpc_task *task, void *calldata)
1587{
1588 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001591
Trond Myklebust14516c32010-07-31 14:29:06 -04001592 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1593 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001594
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001595 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001596 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1597 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001598 case S_IFREG:
1599 break;
1600 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001602 break;
1603 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001605 break;
1606 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001608 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001610 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001611 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1612 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001613 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001614 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001616
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617static void nfs4_open_release(void *calldata)
1618{
1619 struct nfs4_opendata *data = calldata;
1620 struct nfs4_state *state = NULL;
1621
1622 /* If this request hasn't been cancelled, do nothing */
1623 if (data->cancelled == 0)
1624 goto out_free;
1625 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001626 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 goto out_free;
1628 /* In case we need an open_confirm, no cleanup! */
1629 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1630 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001631 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001632 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001633 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001635 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636}
1637
1638static const struct rpc_call_ops nfs4_open_ops = {
1639 .rpc_call_prepare = nfs4_open_prepare,
1640 .rpc_call_done = nfs4_open_done,
1641 .rpc_release = nfs4_open_release,
1642};
1643
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001644static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645{
1646 struct inode *dir = data->dir->d_inode;
1647 struct nfs_server *server = NFS_SERVER(dir);
1648 struct nfs_openargs *o_arg = &data->o_arg;
1649 struct nfs_openres *o_res = &data->o_res;
1650 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001651 struct rpc_message msg = {
1652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1653 .rpc_argp = o_arg,
1654 .rpc_resp = o_res,
1655 .rpc_cred = data->owner->so_cred,
1656 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001657 struct rpc_task_setup task_setup_data = {
1658 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001659 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001660 .callback_ops = &nfs4_open_ops,
1661 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001662 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001663 .flags = RPC_TASK_ASYNC,
1664 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665 int status;
1666
Trond Myklebust9d12b212012-01-17 22:04:25 -05001667 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001669 data->rpc_done = 0;
1670 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001671 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001672 data->is_recover = 0;
1673 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001674 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001675 data->is_recover = 1;
1676 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001677 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678 if (IS_ERR(task))
1679 return PTR_ERR(task);
1680 status = nfs4_wait_for_completion_rpc_task(task);
1681 if (status != 0) {
1682 data->cancelled = 1;
1683 smp_wmb();
1684 } else
1685 status = data->rpc_status;
1686 rpc_put_task(task);
1687
1688 return status;
1689}
1690
1691static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1692{
1693 struct inode *dir = data->dir->d_inode;
1694 struct nfs_openres *o_res = &data->o_res;
1695 int status;
1696
1697 status = nfs4_run_open_task(data, 1);
1698 if (status != 0 || !data->rpc_done)
1699 return status;
1700
Trond Myklebust6926afd2012-01-07 13:22:46 -05001701 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1702
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001703 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1704 status = _nfs4_proc_open_confirm(data);
1705 if (status != 0)
1706 return status;
1707 }
1708
1709 return status;
1710}
1711
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001712static int nfs4_opendata_access(struct rpc_cred *cred,
1713 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001714 struct nfs4_state *state, fmode_t fmode,
1715 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001716{
1717 struct nfs_access_entry cache;
1718 u32 mask;
1719
1720 /* access call failed or for some reason the server doesn't
1721 * support any access modes -- defer access call until later */
1722 if (opendata->o_res.access_supported == 0)
1723 return 0;
1724
1725 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001726 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001727 * write mode bits, but POSIX allows the creating process to write.
1728 * use openflags to check for exec, because fmode won't
1729 * always have FMODE_EXEC set when file open for exec. */
1730 if (openflags & __FMODE_EXEC) {
1731 /* ONLY check for exec rights */
1732 mask = MAY_EXEC;
1733 } else if (fmode & FMODE_READ)
1734 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001735
1736 cache.cred = cred;
1737 cache.jiffies = jiffies;
1738 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1739 nfs_access_add_cache(state->inode, &cache);
1740
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001741 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001742 return 0;
1743
1744 /* even though OPEN succeeded, access is denied. Close the file */
1745 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001746 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001747}
1748
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001749/*
1750 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1751 */
1752static int _nfs4_proc_open(struct nfs4_opendata *data)
1753{
1754 struct inode *dir = data->dir->d_inode;
1755 struct nfs_server *server = NFS_SERVER(dir);
1756 struct nfs_openargs *o_arg = &data->o_arg;
1757 struct nfs_openres *o_res = &data->o_res;
1758 int status;
1759
1760 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001761 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001763 if (status != 0) {
1764 if (status == -NFS4ERR_BADNAME &&
1765 !(o_arg->open_flags & O_CREAT))
1766 return -ENOENT;
1767 return status;
1768 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769
Trond Myklebust6926afd2012-01-07 13:22:46 -05001770 nfs_fattr_map_and_free_names(server, &data->f_attr);
1771
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001772 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001773 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001774 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1775 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001777 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001782 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Andy Adamsond83217c2011-03-01 01:34:17 +00001786static int nfs4_recover_expired_lease(struct nfs_server *server)
1787{
1788 return nfs4_client_recover_expired_lease(server->nfs_client);
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
1792 * OPEN_EXPIRED:
1793 * reclaim state on the server after a network partition.
1794 * Assumes caller holds the appropriate lock
1795 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001796static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001798 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001799 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001801 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001802 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001803 if (IS_ERR(opendata))
1804 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001805 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001806 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001807 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001808 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001812static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001813{
Trond Myklebust539cd032007-06-05 11:46:42 -04001814 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001815 struct nfs4_exception exception = { };
1816 int err;
1817
1818 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001819 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001820 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1821 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001822 switch (err) {
1823 default:
1824 goto out;
1825 case -NFS4ERR_GRACE:
1826 case -NFS4ERR_DELAY:
1827 nfs4_handle_exception(server, err, &exception);
1828 err = 0;
1829 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001830 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001831out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001832 return err;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Trond Myklebust864472e2006-01-03 09:55:15 +01001840 ctx = nfs4_state_find_open_context(state);
1841 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001842 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001843 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 put_nfs_open_context(ctx);
1845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001848#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001849static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001850{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001851 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001852 nfs4_stateid *stateid = &state->stateid;
1853 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001854
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001855 /* If a state reset has been done, test_stateid is unneeded */
1856 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1857 return;
1858
1859 status = nfs41_test_stateid(server, stateid);
1860 if (status != NFS_OK) {
1861 /* Free the stateid unless the server explicitly
1862 * informs us the stateid is unrecognized. */
1863 if (status != -NFS4ERR_BAD_STATEID)
1864 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001865 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001866
Bryan Schumaker69388672012-09-26 15:25:52 -04001867 write_seqlock(&state->seqlock);
1868 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1869 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001870 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1871 }
1872}
1873
1874/**
1875 * nfs41_check_open_stateid - possibly free an open stateid
1876 *
1877 * @state: NFSv4 state for an inode
1878 *
1879 * Returns NFS_OK if recovery for this stateid is now finished.
1880 * Otherwise a negative NFS4ERR value is returned.
1881 */
1882static int nfs41_check_open_stateid(struct nfs4_state *state)
1883{
1884 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001885 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001886 int status;
1887
1888 /* If a state reset has been done, test_stateid is unneeded */
1889 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1890 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1891 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1892 return -NFS4ERR_BAD_STATEID;
1893
1894 status = nfs41_test_stateid(server, stateid);
1895 if (status != NFS_OK) {
1896 /* Free the stateid unless the server explicitly
1897 * informs us the stateid is unrecognized. */
1898 if (status != -NFS4ERR_BAD_STATEID)
1899 nfs41_free_stateid(server, stateid);
1900
1901 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1902 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1903 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001904 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001905 }
1906 return status;
1907}
1908
1909static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1910{
Chuck Levereb64cf92012-07-11 16:30:05 -04001911 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001912
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001913 nfs41_clear_delegation_stateid(state);
1914 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001915 if (status != NFS_OK)
1916 status = nfs4_open_expired(sp, state);
1917 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001918}
1919#endif
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001922 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1923 * fields corresponding to attributes that were used to store the verifier.
1924 * Make sure we clobber those fields in the later setattr call
1925 */
1926static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1927{
1928 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1929 !(sattr->ia_valid & ATTR_ATIME_SET))
1930 sattr->ia_valid |= ATTR_ATIME;
1931
1932 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1933 !(sattr->ia_valid & ATTR_MTIME_SET))
1934 sattr->ia_valid |= ATTR_MTIME;
1935}
1936
Trond Myklebustc21443c2013-02-07 14:26:21 -05001937static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1938 fmode_t fmode,
1939 int flags,
1940 struct nfs4_state **res)
1941{
1942 struct nfs4_state_owner *sp = opendata->owner;
1943 struct nfs_server *server = sp->so_server;
1944 struct nfs4_state *state;
1945 unsigned int seq;
1946 int ret;
1947
1948 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1949
1950 ret = _nfs4_proc_open(opendata);
1951 if (ret != 0)
1952 goto out;
1953
1954 state = nfs4_opendata_to_nfs4_state(opendata);
1955 ret = PTR_ERR(state);
1956 if (IS_ERR(state))
1957 goto out;
1958 if (server->caps & NFS_CAP_POSIX_LOCK)
1959 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1960
1961 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1962 if (ret != 0)
1963 goto out;
1964
Trond Myklebust91876b12013-03-28 14:01:33 -04001965 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001966 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001967 *res = state;
1968out:
1969 return ret;
1970}
1971
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001972/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001973 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Andy Adamson82be4172012-05-23 05:02:35 -04001975static int _nfs4_do_open(struct inode *dir,
1976 struct dentry *dentry,
1977 fmode_t fmode,
1978 int flags,
1979 struct iattr *sattr,
1980 struct rpc_cred *cred,
1981 struct nfs4_state **res,
1982 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 struct nfs4_state_owner *sp;
1985 struct nfs4_state *state = NULL;
1986 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001987 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001988 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001989 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001993 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1994 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1996 goto out_err;
1997 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001998 status = nfs4_recover_expired_lease(server);
1999 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002000 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002001 if (dentry->d_inode != NULL)
2002 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002003 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002004 if (dentry->d_inode)
2005 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002006 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002007 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002008 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002009 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Andy Adamson82be4172012-05-23 05:02:35 -04002011 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2012 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2013 if (!opendata->f_attr.mdsthreshold)
2014 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002015 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002016 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002017 if (dentry->d_inode != NULL)
2018 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002019
Trond Myklebustc21443c2013-02-07 14:26:21 -05002020 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002021 if (status != 0)
2022 goto err_opendata_put;
2023
Trond Myklebust549b19c2013-04-16 18:42:34 -04002024 if ((opendata->o_arg.open_flags & O_EXCL) &&
2025 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002026 nfs4_exclusive_attrset(opendata, sattr);
2027
2028 nfs_fattr_init(opendata->o_res.f_attr);
2029 status = nfs4_do_setattr(state->inode, cred,
2030 opendata->o_res.f_attr, sattr,
2031 state);
2032 if (status == 0)
2033 nfs_setattr_update_inode(state->inode, sattr);
2034 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2035 }
Andy Adamson82be4172012-05-23 05:02:35 -04002036
2037 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2038 *ctx_th = opendata->f_attr.mdsthreshold;
2039 else
2040 kfree(opendata->f_attr.mdsthreshold);
2041 opendata->f_attr.mdsthreshold = NULL;
2042
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002043 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *res = state;
2046 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002047err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002048 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002049 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002050err_put_state_owner:
2051 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 *res = NULL;
2054 return status;
2055}
2056
2057
Andy Adamson82be4172012-05-23 05:02:35 -04002058static struct nfs4_state *nfs4_do_open(struct inode *dir,
2059 struct dentry *dentry,
2060 fmode_t fmode,
2061 int flags,
2062 struct iattr *sattr,
2063 struct rpc_cred *cred,
2064 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002066 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 struct nfs4_exception exception = { };
2068 struct nfs4_state *res;
2069 int status;
2070
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002071 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002073 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2074 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (status == 0)
2076 break;
2077 /* NOTE: BAD_SEQID means the server and client disagree about the
2078 * book-keeping w.r.t. state-changing operations
2079 * (OPEN/CLOSE/LOCK/LOCKU...)
2080 * It is actually a sign of a bug on the client or on the server.
2081 *
2082 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002083 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * have unhashed the old state_owner for us, and that we can
2085 * therefore safely retry using a new one. We should still warn
2086 * the user though...
2087 */
2088 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002089 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002090 " returned a bad sequence-id error!\n",
2091 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 exception.retry = 1;
2093 continue;
2094 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002095 /*
2096 * BAD_STATEID on OPEN means that the server cancelled our
2097 * state before it received the OPEN_CONFIRM.
2098 * Recover by retrying the request as per the discussion
2099 * on Page 181 of RFC3530.
2100 */
2101 if (status == -NFS4ERR_BAD_STATEID) {
2102 exception.retry = 1;
2103 continue;
2104 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002105 if (status == -EAGAIN) {
2106 /* We must have found a delegation */
2107 exception.retry = 1;
2108 continue;
2109 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002110 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2111 continue;
2112 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 status, &exception));
2114 } while (exception.retry);
2115 return res;
2116}
2117
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002118static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2119 struct nfs_fattr *fattr, struct iattr *sattr,
2120 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002122 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002124 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 .iap = sattr,
2126 .server = server,
2127 .bitmask = server->attr_bitmask,
2128 };
2129 struct nfs_setattrres res = {
2130 .fattr = fattr,
2131 .server = server,
2132 };
2133 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2135 .rpc_argp = &arg,
2136 .rpc_resp = &res,
2137 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002139 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002140 fmode_t fmode;
2141 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002142 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Trond Myklebust0e574af2005-10-27 22:12:38 -04002144 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Trond Myklebustee3ae842013-04-29 10:35:36 -04002146 /* Servers should only apply open mode checks for file size changes */
2147 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2148 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2149
2150 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2151 /* Use that stateid */
2152 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002153 struct nfs_lockowner lockowner = {
2154 .l_owner = current->files,
2155 .l_pid = current->tgid,
2156 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002157 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002158 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002159 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002160 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Bryan Schumaker7c513052011-03-24 17:12:24 +00002162 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 if (status == 0 && state != NULL)
2164 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002165 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002168static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2169 struct nfs_fattr *fattr, struct iattr *sattr,
2170 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002172 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002173 struct nfs4_exception exception = {
2174 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002175 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 int err;
2178 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002179 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2180 switch (err) {
2181 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002182 if (!(sattr->ia_valid & ATTR_SIZE)) {
2183 pr_warn_once("NFSv4: server %s is incorrectly "
2184 "applying open mode checks to "
2185 "a SETATTR that is not "
2186 "changing file size.\n",
2187 server->nfs_client->cl_hostname);
2188 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002189 if (state && !(state->state & FMODE_WRITE)) {
2190 err = -EBADF;
2191 if (sattr->ia_valid & ATTR_OPEN)
2192 err = -EACCES;
2193 goto out;
2194 }
2195 }
2196 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002198out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return err;
2200}
2201
2202struct nfs4_closedata {
2203 struct inode *inode;
2204 struct nfs4_state *state;
2205 struct nfs_closeargs arg;
2206 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002207 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002208 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002209 bool roc;
2210 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211};
2212
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002213static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002214{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002215 struct nfs4_closedata *calldata = data;
2216 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002217 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002218
Fred Isamanf7e89172011-01-06 11:36:32 +00002219 if (calldata->roc)
2220 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002221 nfs4_put_open_state(calldata->state);
2222 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002223 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002224 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002225 kfree(calldata);
2226}
2227
Trond Myklebust88069f72009-12-08 08:33:16 -05002228static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2229 fmode_t fmode)
2230{
2231 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002233 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2234 case FMODE_WRITE:
2235 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2236 break;
2237 case FMODE_READ:
2238 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2239 break;
2240 case 0:
2241 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2242 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2243 clear_bit(NFS_OPEN_STATE, &state->flags);
2244 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002245 spin_unlock(&state->owner->so_lock);
2246}
2247
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002248static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002250 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct nfs_server *server = NFS_SERVER(calldata->inode);
2253
Chuck Levera3ca5652012-03-01 17:00:40 -05002254 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002255 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2256 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /* hmm. we are done with the inode, and in the process of freeing
2258 * the state_owner. we keep this around to process errors
2259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 switch (task->tk_status) {
2261 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002262 if (calldata->roc)
2263 pnfs_roc_set_barrier(state->inode,
2264 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002265 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002266 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002267 nfs4_close_clear_stateid_flags(state,
2268 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 break;
2270 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002271 case -NFS4ERR_OLD_STATEID:
2272 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002274 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002277 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2278 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002280 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002281 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002282 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002285static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002287 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002288 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002289 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002290 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002291
Chuck Levera3ca5652012-03-01 17:00:40 -05002292 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002293 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002294 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002295
Trond Myklebust88069f72009-12-08 08:33:16 -05002296 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2297 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002298 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002299 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002300 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002301 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002302 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2303 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2304 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002305 }
2306 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002307 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2308 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2309 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002310 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002311 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002312 if (!nfs4_valid_open_stateid(state))
2313 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002314 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002315
2316 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002317 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002318 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002319 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002320
Fred Isamanf7e89172011-01-06 11:36:32 +00002321 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002322 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002323 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002324 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2325 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002326 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002327 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002328 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002329
Trond Myklebust516a6af2005-10-27 22:12:41 -04002330 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002331 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002332 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002333 &calldata->arg.seq_args,
2334 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002335 task) != 0)
2336 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002337 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002338 return;
2339out_no_action:
2340 task->tk_action = NULL;
2341out_wait:
2342 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002345static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002346 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002347 .rpc_call_done = nfs4_close_done,
2348 .rpc_release = nfs4_free_closedata,
2349};
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/*
2352 * It is possible for data to be read/written from a mem-mapped file
2353 * after the sys_close call (which hits the vfs layer as a flush).
2354 * This means that we can't safely call nfsv4 close on a file until
2355 * the inode is cleared. This in turn means that we are not good
2356 * NFSv4 citizens - we do not indicate to the server to update the file's
2357 * share state even when we are done with one of the three share
2358 * stateid's in the inode.
2359 *
2360 * NOTE: Caller must be holding the sp->so_owner semaphore!
2361 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002362int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002364 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002366 struct nfs4_state_owner *sp = state->owner;
2367 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002368 struct rpc_message msg = {
2369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2370 .rpc_cred = state->owner->so_cred,
2371 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002372 struct rpc_task_setup task_setup_data = {
2373 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002374 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002376 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002377 .flags = RPC_TASK_ASYNC,
2378 };
Trond Myklebust95121352005-10-18 14:20:12 -07002379 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002381 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002383 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002384 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002385 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002387 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002388 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002390 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002391 if (calldata->arg.seqid == NULL)
2392 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002393 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002394 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002395 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002396 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002397 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002398 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002399 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002400
Trond Myklebust1174dd12010-12-21 10:52:24 -05002401 msg.rpc_argp = &calldata->arg;
2402 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002403 task_setup_data.callback_data = calldata;
2404 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002405 if (IS_ERR(task))
2406 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002407 status = 0;
2408 if (wait)
2409 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002410 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002411 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002412out_free_calldata:
2413 kfree(calldata);
2414out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002415 nfs4_put_open_state(state);
2416 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002417 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Trond Myklebust2b484292010-09-17 10:56:51 -04002420static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002421nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 struct nfs4_state *state;
2424
Trond Myklebust565277f2007-10-15 18:17:53 -04002425 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002426 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2427 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002428 if (IS_ERR(state))
2429 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002430 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002431 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Trond Myklebust1185a552009-12-03 15:54:02 -05002434static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002435{
2436 if (ctx->state == NULL)
2437 return;
2438 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002439 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002440 else
Al Viro643168c2011-06-22 18:20:23 -04002441 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2445{
Benny Halevy43652ad2009-04-01 09:21:54 -04002446 struct nfs4_server_caps_arg args = {
2447 .fhandle = fhandle,
2448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 struct nfs4_server_caps_res res = {};
2450 struct rpc_message msg = {
2451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002452 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 .rpc_resp = &res,
2454 };
2455 int status;
2456
Bryan Schumaker7c513052011-03-24 17:12:24 +00002457 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (status == 0) {
2459 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002460 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2461 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2462 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2463 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2464 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2466 server->caps |= NFS_CAP_ACLS;
2467 if (res.has_links != 0)
2468 server->caps |= NFS_CAP_HARDLINKS;
2469 if (res.has_symlinks != 0)
2470 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002471 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2472 server->caps |= NFS_CAP_FILEID;
2473 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2474 server->caps |= NFS_CAP_MODE;
2475 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2476 server->caps |= NFS_CAP_NLINK;
2477 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2478 server->caps |= NFS_CAP_OWNER;
2479 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2480 server->caps |= NFS_CAP_OWNER_GROUP;
2481 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2482 server->caps |= NFS_CAP_ATIME;
2483 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2484 server->caps |= NFS_CAP_CTIME;
2485 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2486 server->caps |= NFS_CAP_MTIME;
2487
Trond Myklebusta65318b2009-03-11 14:10:28 -04002488 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2489 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2490 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002492 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return status;
2496}
2497
Trond Myklebust55a97592006-06-09 09:34:19 -04002498int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 struct nfs4_exception exception = { };
2501 int err;
2502 do {
2503 err = nfs4_handle_exception(server,
2504 _nfs4_server_capabilities(server, fhandle),
2505 &exception);
2506 } while (exception.retry);
2507 return err;
2508}
2509
2510static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2511 struct nfs_fsinfo *info)
2512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct nfs4_lookup_root_arg args = {
2514 .bitmask = nfs4_fattr_bitmap,
2515 };
2516 struct nfs4_lookup_res res = {
2517 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002518 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 .fh = fhandle,
2520 };
2521 struct rpc_message msg = {
2522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2523 .rpc_argp = &args,
2524 .rpc_resp = &res,
2525 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002526
Trond Myklebust0e574af2005-10-27 22:12:38 -04002527 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002528 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2532 struct nfs_fsinfo *info)
2533{
2534 struct nfs4_exception exception = { };
2535 int err;
2536 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002537 err = _nfs4_lookup_root(server, fhandle, info);
2538 switch (err) {
2539 case 0:
2540 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002541 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002542 default:
2543 err = nfs4_handle_exception(server, err, &exception);
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002546out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return err;
2548}
2549
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002550static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2551 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2552{
2553 struct rpc_auth *auth;
2554 int ret;
2555
2556 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002557 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002558 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002559 goto out;
2560 }
2561 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002562out:
2563 return ret;
2564}
2565
Chuck Lever9a744ba2013-03-16 15:56:02 -04002566/*
2567 * Retry pseudoroot lookup with various security flavors. We do this when:
2568 *
2569 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2570 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2571 *
2572 * Returns zero on success, or a negative NFS4ERR value, or a
2573 * negative errno value.
2574 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002575static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002576 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002578 /* Per 3530bis 15.33.5 */
2579 static const rpc_authflavor_t flav_array[] = {
2580 RPC_AUTH_GSS_KRB5P,
2581 RPC_AUTH_GSS_KRB5I,
2582 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002583 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002584 RPC_AUTH_NULL,
2585 };
2586 int status = -EPERM;
2587 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Chuck Lever9a744ba2013-03-16 15:56:02 -04002589 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002590 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002591 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002592 continue;
2593 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002594 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002595
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002596 /*
2597 * -EACCESS could mean that the user doesn't have correct permissions
2598 * to access the mount. It could also mean that we tried to mount
2599 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2600 * existing mount programs don't handle -EACCES very well so it should
2601 * be mapped to -EPERM instead.
2602 */
2603 if (status == -EACCES)
2604 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002605 return status;
2606}
2607
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002608static int nfs4_do_find_root_sec(struct nfs_server *server,
2609 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2610{
2611 int mv = server->nfs_client->cl_minorversion;
2612 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2613}
2614
2615/**
2616 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2617 * @server: initialized nfs_server handle
2618 * @fhandle: we fill in the pseudo-fs root file handle
2619 * @info: we fill in an FSINFO struct
2620 *
2621 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002622 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002623int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2624 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002625{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002626 int status;
2627
2628 status = nfs4_lookup_root(server, fhandle, info);
2629 if ((status == -NFS4ERR_WRONGSEC) &&
2630 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2631 status = nfs4_do_find_root_sec(server, fhandle, info);
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (status == 0)
2634 status = nfs4_server_capabilities(server, fhandle);
2635 if (status == 0)
2636 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002637
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002638 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
Bryan Schumakerbae36242012-05-10 15:07:31 -04002641static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2642 struct nfs_fsinfo *info)
2643{
2644 int error;
2645 struct nfs_fattr *fattr = info->fattr;
2646
2647 error = nfs4_server_capabilities(server, mntfh);
2648 if (error < 0) {
2649 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2650 return error;
2651 }
2652
2653 error = nfs4_proc_getattr(server, mntfh, fattr);
2654 if (error < 0) {
2655 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2656 return error;
2657 }
2658
2659 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2660 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2661 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2662
2663 return error;
2664}
2665
Manoj Naik6b97fd32006-06-09 09:34:29 -04002666/*
2667 * Get locations and (maybe) other attributes of a referral.
2668 * Note that we'll actually follow the referral later when
2669 * we detect fsid mismatch in inode revalidation
2670 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002671static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2672 const struct qstr *name, struct nfs_fattr *fattr,
2673 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002674{
2675 int status = -ENOMEM;
2676 struct page *page = NULL;
2677 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002678
2679 page = alloc_page(GFP_KERNEL);
2680 if (page == NULL)
2681 goto out;
2682 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2683 if (locations == NULL)
2684 goto out;
2685
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002686 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002687 if (status != 0)
2688 goto out;
2689 /* Make sure server returned a different fsid for the referral */
2690 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002691 dprintk("%s: server did not return a different fsid for"
2692 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002693 status = -EIO;
2694 goto out;
2695 }
Andy Adamson533eb462011-06-13 18:25:56 -04002696 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2697 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002698
Andy Adamson533eb462011-06-13 18:25:56 -04002699 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002700 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002701 memset(fhandle, 0, sizeof(struct nfs_fh));
2702out:
2703 if (page)
2704 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002705 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002706 return status;
2707}
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2710{
2711 struct nfs4_getattr_arg args = {
2712 .fh = fhandle,
2713 .bitmask = server->attr_bitmask,
2714 };
2715 struct nfs4_getattr_res res = {
2716 .fattr = fattr,
2717 .server = server,
2718 };
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2721 .rpc_argp = &args,
2722 .rpc_resp = &res,
2723 };
2724
Trond Myklebust0e574af2005-10-27 22:12:38 -04002725 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002726 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2730{
2731 struct nfs4_exception exception = { };
2732 int err;
2733 do {
2734 err = nfs4_handle_exception(server,
2735 _nfs4_proc_getattr(server, fhandle, fattr),
2736 &exception);
2737 } while (exception.retry);
2738 return err;
2739}
2740
2741/*
2742 * The file is not closed if it is opened due to the a request to change
2743 * the size of the file. The open call will not be needed once the
2744 * VFS layer lookup-intents are implemented.
2745 *
2746 * Close is called when the inode is destroyed.
2747 * If we haven't opened the file for O_WRONLY, we
2748 * need to in the size_change case to obtain a stateid.
2749 *
2750 * Got race?
2751 * Because OPEN is always done by name in nfsv4, it is
2752 * possible that we opened a different file by the same
2753 * name. We can recognize this race condition, but we
2754 * can't do anything about it besides returning an error.
2755 *
2756 * This will be fixed with VFS changes (lookup-intent).
2757 */
2758static int
2759nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2760 struct iattr *sattr)
2761{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002762 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002763 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002764 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 int status;
2766
Benny Halevy8a1636c2010-07-14 15:43:57 -04002767 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002768 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002769
Trond Myklebust0e574af2005-10-27 22:12:38 -04002770 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Andy Adamson26699402012-05-30 16:12:24 -04002772 /* Deal with open(O_TRUNC) */
2773 if (sattr->ia_valid & ATTR_OPEN)
2774 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2775
2776 /* Optimization: if the end result is no change, don't RPC */
2777 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2778 return 0;
2779
Trond Myklebustd5308382005-11-04 15:33:38 -05002780 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002781 if (sattr->ia_valid & ATTR_FILE) {
2782 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002783
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002784 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002785 if (ctx) {
2786 cred = ctx->cred;
2787 state = ctx->state;
2788 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002789 }
2790
2791 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002792 if (status == 0)
2793 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return status;
2795}
2796
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002797static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2798 const struct qstr *name, struct nfs_fh *fhandle,
2799 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002800{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002801 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002802 int status;
2803 struct nfs4_lookup_arg args = {
2804 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002805 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002806 .name = name,
2807 };
2808 struct nfs4_lookup_res res = {
2809 .server = server,
2810 .fattr = fattr,
2811 .fh = fhandle,
2812 };
2813 struct rpc_message msg = {
2814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2815 .rpc_argp = &args,
2816 .rpc_resp = &res,
2817 };
2818
2819 nfs_fattr_init(fattr);
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002822 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dprintk("NFS reply lookup: %d\n", status);
2824 return status;
2825}
2826
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002827static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002828{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002829 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002830 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002831 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2832 fattr->nlink = 2;
2833}
2834
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002835static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2836 struct qstr *name, struct nfs_fh *fhandle,
2837 struct nfs_fattr *fattr)
2838{
2839 struct nfs4_exception exception = { };
2840 struct rpc_clnt *client = *clnt;
2841 int err;
2842 do {
2843 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2844 switch (err) {
2845 case -NFS4ERR_BADNAME:
2846 err = -ENOENT;
2847 goto out;
2848 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002849 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002850 goto out;
2851 case -NFS4ERR_WRONGSEC:
2852 err = -EPERM;
2853 if (client != *clnt)
2854 goto out;
2855
2856 client = nfs4_create_sec_client(client, dir, name);
2857 if (IS_ERR(client))
2858 return PTR_ERR(client);
2859
2860 exception.retry = 1;
2861 break;
2862 default:
2863 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2864 }
2865 } while (exception.retry);
2866
2867out:
2868 if (err == 0)
2869 *clnt = client;
2870 else if (client != *clnt)
2871 rpc_shutdown_client(client);
2872
2873 return err;
2874}
2875
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002876static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002877 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002879 int status;
2880 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002881
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002882 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2883 if (client != NFS_CLIENT(dir)) {
2884 rpc_shutdown_client(client);
2885 nfs_fixup_secinfo_attributes(fattr);
2886 }
2887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002890struct rpc_clnt *
2891nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2892 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2893{
2894 int status;
2895 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2896
2897 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2898 if (status < 0) {
2899 rpc_shutdown_client(client);
2900 return ERR_PTR(status);
2901 }
2902 return client;
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2906{
Trond Myklebust76b32992007-08-10 17:45:11 -04002907 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct nfs4_accessargs args = {
2909 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002910 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002912 struct nfs4_accessres res = {
2913 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2917 .rpc_argp = &args,
2918 .rpc_resp = &res,
2919 .rpc_cred = entry->cred,
2920 };
2921 int mode = entry->mask;
2922 int status;
2923
2924 /*
2925 * Determine which access bits we want to ask for...
2926 */
2927 if (mode & MAY_READ)
2928 args.access |= NFS4_ACCESS_READ;
2929 if (S_ISDIR(inode->i_mode)) {
2930 if (mode & MAY_WRITE)
2931 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2932 if (mode & MAY_EXEC)
2933 args.access |= NFS4_ACCESS_LOOKUP;
2934 } else {
2935 if (mode & MAY_WRITE)
2936 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2937 if (mode & MAY_EXEC)
2938 args.access |= NFS4_ACCESS_EXECUTE;
2939 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002940
2941 res.fattr = nfs_alloc_fattr();
2942 if (res.fattr == NULL)
2943 return -ENOMEM;
2944
Bryan Schumaker7c513052011-03-24 17:12:24 +00002945 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002947 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002948 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002950 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return status;
2952}
2953
2954static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2955{
2956 struct nfs4_exception exception = { };
2957 int err;
2958 do {
2959 err = nfs4_handle_exception(NFS_SERVER(inode),
2960 _nfs4_proc_access(inode, entry),
2961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966/*
2967 * TODO: For the time being, we don't try to get any attributes
2968 * along with any of the zero-copy operations READ, READDIR,
2969 * READLINK, WRITE.
2970 *
2971 * In the case of the first three, we want to put the GETATTR
2972 * after the read-type operation -- this is because it is hard
2973 * to predict the length of a GETATTR response in v4, and thus
2974 * align the READ data correctly. This means that the GETATTR
2975 * may end up partially falling into the page cache, and we should
2976 * shift it into the 'tail' of the xdr_buf before processing.
2977 * To do this efficiently, we need to know the total length
2978 * of data received, which doesn't seem to be available outside
2979 * of the RPC layer.
2980 *
2981 * In the case of WRITE, we also want to put the GETATTR after
2982 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002983 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 *
2985 * Both of these changes to the XDR layer would in fact be quite
2986 * minor, but I decided to leave them for a subsequent patch.
2987 */
2988static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2989 unsigned int pgbase, unsigned int pglen)
2990{
2991 struct nfs4_readlink args = {
2992 .fh = NFS_FH(inode),
2993 .pgbase = pgbase,
2994 .pglen = pglen,
2995 .pages = &page,
2996 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002997 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3000 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003001 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
3003
Bryan Schumaker7c513052011-03-24 17:12:24 +00003004 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 -07003005}
3006
3007static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3008 unsigned int pgbase, unsigned int pglen)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(inode),
3014 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003021 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023static int
3024nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003025 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003027 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int status = 0;
3030
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003031 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3032 if (IS_ERR(ctx))
3033 return PTR_ERR(ctx);
3034
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003035 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003036 state = nfs4_do_open(dir, dentry, ctx->mode,
3037 flags, sattr, ctx->cred,
3038 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003039 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (IS_ERR(state)) {
3041 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003044 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003045 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003046 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003048 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return status;
3050}
3051
3052static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3053{
Trond Myklebust16e42952005-10-27 22:12:44 -04003054 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003055 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003057 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003059 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003060 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3064 .rpc_argp = &args,
3065 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003067 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003068
Bryan Schumaker7c513052011-03-24 17:12:24 +00003069 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003070 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003071 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return status;
3073}
3074
3075static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3076{
3077 struct nfs4_exception exception = { };
3078 int err;
3079 do {
3080 err = nfs4_handle_exception(NFS_SERVER(dir),
3081 _nfs4_proc_remove(dir, name),
3082 &exception);
3083 } while (exception.retry);
3084 return err;
3085}
3086
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003087static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003089 struct nfs_server *server = NFS_SERVER(dir);
3090 struct nfs_removeargs *args = msg->rpc_argp;
3091 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003093 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003095 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003098static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3099{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003100 nfs4_setup_sequence(NFS_SERVER(data->dir),
3101 &data->args.seq_args,
3102 &data->res.seq_res,
3103 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003106static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003108 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3109
Trond Myklebust14516c32010-07-31 14:29:06 -04003110 if (!nfs4_sequence_done(task, &res->seq_res))
3111 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003112 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003113 return 0;
3114 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
Jeff Laytond3d41522010-09-17 17:31:57 -04003118static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3119{
3120 struct nfs_server *server = NFS_SERVER(dir);
3121 struct nfs_renameargs *arg = msg->rpc_argp;
3122 struct nfs_renameres *res = msg->rpc_resp;
3123
3124 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003125 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003126 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003127}
3128
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003129static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3130{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003131 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3132 &data->args.seq_args,
3133 &data->res.seq_res,
3134 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003135}
3136
3137static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3138 struct inode *new_dir)
3139{
3140 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3141
3142 if (!nfs4_sequence_done(task, &res->seq_res))
3143 return 0;
3144 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3145 return 0;
3146
3147 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003148 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003149 return 1;
3150}
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3153 struct inode *new_dir, struct qstr *new_name)
3154{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003155 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003156 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 .old_dir = NFS_FH(old_dir),
3158 .new_dir = NFS_FH(new_dir),
3159 .old_name = old_name,
3160 .new_name = new_name,
3161 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003162 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003163 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct rpc_message msg = {
3166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3167 .rpc_argp = &arg,
3168 .rpc_resp = &res,
3169 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003170 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Bryan Schumaker7c513052011-03-24 17:12:24 +00003172 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (!status) {
3174 update_changeattr(old_dir, &res.old_cinfo);
3175 update_changeattr(new_dir, &res.new_cinfo);
3176 }
3177 return status;
3178}
3179
3180static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3181 struct inode *new_dir, struct qstr *new_name)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
3185 do {
3186 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3187 _nfs4_proc_rename(old_dir, old_name,
3188 new_dir, new_name),
3189 &exception);
3190 } while (exception.retry);
3191 return err;
3192}
3193
3194static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3195{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003196 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 struct nfs4_link_arg arg = {
3198 .fh = NFS_FH(inode),
3199 .dir_fh = NFS_FH(dir),
3200 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003201 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003203 struct nfs4_link_res res = {
3204 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003205 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 struct rpc_message msg = {
3207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3208 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003209 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003211 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Trond Myklebust136f2622010-04-16 16:22:49 -04003213 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003214 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003215 goto out;
3216
Bryan Schumaker7c513052011-03-24 17:12:24 +00003217 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003218 if (!status) {
3219 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003220 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003221 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003222out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003223 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return status;
3225}
3226
3227static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3228{
3229 struct nfs4_exception exception = { };
3230 int err;
3231 do {
3232 err = nfs4_handle_exception(NFS_SERVER(inode),
3233 _nfs4_proc_link(inode, dir, name),
3234 &exception);
3235 } while (exception.retry);
3236 return err;
3237}
3238
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239struct nfs4_createdata {
3240 struct rpc_message msg;
3241 struct nfs4_create_arg arg;
3242 struct nfs4_create_res res;
3243 struct nfs_fh fh;
3244 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003245};
3246
3247static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3248 struct qstr *name, struct iattr *sattr, u32 ftype)
3249{
3250 struct nfs4_createdata *data;
3251
3252 data = kzalloc(sizeof(*data), GFP_KERNEL);
3253 if (data != NULL) {
3254 struct nfs_server *server = NFS_SERVER(dir);
3255
3256 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3257 data->msg.rpc_argp = &data->arg;
3258 data->msg.rpc_resp = &data->res;
3259 data->arg.dir_fh = NFS_FH(dir);
3260 data->arg.server = server;
3261 data->arg.name = name;
3262 data->arg.attrs = sattr;
3263 data->arg.ftype = ftype;
3264 data->arg.bitmask = server->attr_bitmask;
3265 data->res.server = server;
3266 data->res.fh = &data->fh;
3267 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003269 }
3270 return data;
3271}
3272
3273static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3274{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003275 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003276 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003277 if (status == 0) {
3278 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003279 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3280 }
3281 return status;
3282}
3283
3284static void nfs4_free_createdata(struct nfs4_createdata *data)
3285{
3286 kfree(data);
3287}
3288
Chuck Lever4f390c12006-08-22 20:06:22 -04003289static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003290 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003292 struct nfs4_createdata *data;
3293 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Chuck Lever94a6d752006-08-22 20:06:23 -04003295 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003297
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003298 status = -ENOMEM;
3299 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3300 if (data == NULL)
3301 goto out;
3302
3303 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3304 data->arg.u.symlink.pages = &page;
3305 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003307 status = nfs4_do_create(dir, dentry, data);
3308
3309 nfs4_free_createdata(data);
3310out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return status;
3312}
3313
Chuck Lever4f390c12006-08-22 20:06:22 -04003314static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003315 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 struct nfs4_exception exception = { };
3318 int err;
3319 do {
3320 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003321 _nfs4_proc_symlink(dir, dentry, page,
3322 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 &exception);
3324 } while (exception.retry);
3325 return err;
3326}
3327
3328static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3329 struct iattr *sattr)
3330{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003331 struct nfs4_createdata *data;
3332 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003334 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3335 if (data == NULL)
3336 goto out;
3337
3338 status = nfs4_do_create(dir, dentry, data);
3339
3340 nfs4_free_createdata(data);
3341out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return status;
3343}
3344
3345static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3346 struct iattr *sattr)
3347{
3348 struct nfs4_exception exception = { };
3349 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003350
3351 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 do {
3353 err = nfs4_handle_exception(NFS_SERVER(dir),
3354 _nfs4_proc_mkdir(dir, dentry, sattr),
3355 &exception);
3356 } while (exception.retry);
3357 return err;
3358}
3359
3360static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003361 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct inode *dir = dentry->d_inode;
3364 struct nfs4_readdir_arg args = {
3365 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003366 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 .pgbase = 0,
3368 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003369 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003370 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 };
3372 struct nfs4_readdir_res res;
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3375 .rpc_argp = &args,
3376 .rpc_resp = &res,
3377 .rpc_cred = cred,
3378 };
3379 int status;
3380
Harvey Harrison3110ff82008-05-02 13:42:44 -07003381 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003382 dentry->d_parent->d_name.name,
3383 dentry->d_name.name,
3384 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003385 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003387 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 -05003388 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003389 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003390 status += args.pgbase;
3391 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003392
3393 nfs_invalidate_atime(dir);
3394
Harvey Harrison3110ff82008-05-02 13:42:44 -07003395 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return status;
3397}
3398
3399static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003400 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404 do {
3405 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3406 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003407 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 &exception);
3409 } while (exception.retry);
3410 return err;
3411}
3412
3413static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3414 struct iattr *sattr, dev_t rdev)
3415{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003416 struct nfs4_createdata *data;
3417 int mode = sattr->ia_mode;
3418 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003420 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3421 if (data == NULL)
3422 goto out;
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003425 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003427 data->arg.ftype = NF4BLK;
3428 data->arg.u.device.specdata1 = MAJOR(rdev);
3429 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
3431 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003432 data->arg.ftype = NF4CHR;
3433 data->arg.u.device.specdata1 = MAJOR(rdev);
3434 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003435 } else if (!S_ISSOCK(mode)) {
3436 status = -EINVAL;
3437 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003440 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003441out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003442 nfs4_free_createdata(data);
3443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 return status;
3445}
3446
3447static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3448 struct iattr *sattr, dev_t rdev)
3449{
3450 struct nfs4_exception exception = { };
3451 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003452
3453 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 do {
3455 err = nfs4_handle_exception(NFS_SERVER(dir),
3456 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3457 &exception);
3458 } while (exception.retry);
3459 return err;
3460}
3461
3462static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3463 struct nfs_fsstat *fsstat)
3464{
3465 struct nfs4_statfs_arg args = {
3466 .fh = fhandle,
3467 .bitmask = server->attr_bitmask,
3468 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003469 struct nfs4_statfs_res res = {
3470 .fsstat = fsstat,
3471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 struct rpc_message msg = {
3473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3474 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003475 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 };
3477
Trond Myklebust0e574af2005-10-27 22:12:38 -04003478 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003479 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
3482static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3483{
3484 struct nfs4_exception exception = { };
3485 int err;
3486 do {
3487 err = nfs4_handle_exception(server,
3488 _nfs4_proc_statfs(server, fhandle, fsstat),
3489 &exception);
3490 } while (exception.retry);
3491 return err;
3492}
3493
3494static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3495 struct nfs_fsinfo *fsinfo)
3496{
3497 struct nfs4_fsinfo_arg args = {
3498 .fh = fhandle,
3499 .bitmask = server->attr_bitmask,
3500 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003501 struct nfs4_fsinfo_res res = {
3502 .fsinfo = fsinfo,
3503 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 struct rpc_message msg = {
3505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3506 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003507 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 };
3509
Bryan Schumaker7c513052011-03-24 17:12:24 +00003510 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3514{
3515 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003516 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 int err;
3518
3519 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003520 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3521 if (err == 0) {
3522 struct nfs_client *clp = server->nfs_client;
3523
3524 spin_lock(&clp->cl_lock);
3525 clp->cl_lease_time = fsinfo->lease_time * HZ;
3526 clp->cl_last_renewal = now;
3527 spin_unlock(&clp->cl_lock);
3528 break;
3529 }
3530 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 } while (exception.retry);
3532 return err;
3533}
3534
3535static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3536{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003537 int error;
3538
Trond Myklebust0e574af2005-10-27 22:12:38 -04003539 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003540 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003541 if (error == 0) {
3542 /* block layout checks this! */
3543 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003544 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003545 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003546
3547 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
3550static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3551 struct nfs_pathconf *pathconf)
3552{
3553 struct nfs4_pathconf_arg args = {
3554 .fh = fhandle,
3555 .bitmask = server->attr_bitmask,
3556 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003557 struct nfs4_pathconf_res res = {
3558 .pathconf = pathconf,
3559 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 struct rpc_message msg = {
3561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3562 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003563 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 };
3565
3566 /* None of the pathconf attributes are mandatory to implement */
3567 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3568 memset(pathconf, 0, sizeof(*pathconf));
3569 return 0;
3570 }
3571
Trond Myklebust0e574af2005-10-27 22:12:38 -04003572 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003573 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
3576static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3577 struct nfs_pathconf *pathconf)
3578{
3579 struct nfs4_exception exception = { };
3580 int err;
3581
3582 do {
3583 err = nfs4_handle_exception(server,
3584 _nfs4_proc_pathconf(server, fhandle, pathconf),
3585 &exception);
3586 } while (exception.retry);
3587 return err;
3588}
3589
Trond Myklebust5521abf2013-03-16 20:54:34 -04003590int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003591 const struct nfs_open_context *ctx,
3592 const struct nfs_lock_context *l_ctx,
3593 fmode_t fmode)
3594{
3595 const struct nfs_lockowner *lockowner = NULL;
3596
3597 if (l_ctx != NULL)
3598 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003599 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003600}
3601EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3602
Trond Myklebust5521abf2013-03-16 20:54:34 -04003603static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3604 const struct nfs_open_context *ctx,
3605 const struct nfs_lock_context *l_ctx,
3606 fmode_t fmode)
3607{
3608 nfs4_stateid current_stateid;
3609
3610 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3611 return false;
3612 return nfs4_stateid_match(stateid, &current_stateid);
3613}
3614
3615static bool nfs4_error_stateid_expired(int err)
3616{
3617 switch (err) {
3618 case -NFS4ERR_DELEG_REVOKED:
3619 case -NFS4ERR_ADMIN_REVOKED:
3620 case -NFS4ERR_BAD_STATEID:
3621 case -NFS4ERR_STALE_STATEID:
3622 case -NFS4ERR_OLD_STATEID:
3623 case -NFS4ERR_OPENMODE:
3624 case -NFS4ERR_EXPIRED:
3625 return true;
3626 }
3627 return false;
3628}
3629
Benny Halevyd20581a2011-05-22 19:52:03 +03003630void __nfs4_read_done_cb(struct nfs_read_data *data)
3631{
Fred Isamancd841602012-04-20 14:47:44 -04003632 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003633}
3634
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003635static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Fred Isamancd841602012-04-20 14:47:44 -04003637 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003639 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003640 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003641 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003643
Benny Halevyd20581a2011-05-22 19:52:03 +03003644 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003646 renew_lease(server, data->timestamp);
3647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
Trond Myklebust5521abf2013-03-16 20:54:34 -04003650static bool nfs4_read_stateid_changed(struct rpc_task *task,
3651 struct nfs_readargs *args)
3652{
3653
3654 if (!nfs4_error_stateid_expired(task->tk_status) ||
3655 nfs4_stateid_is_current(&args->stateid,
3656 args->context,
3657 args->lock_context,
3658 FMODE_READ))
3659 return false;
3660 rpc_restart_call_prepare(task);
3661 return true;
3662}
3663
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003664static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3665{
3666
3667 dprintk("--> %s\n", __func__);
3668
3669 if (!nfs4_sequence_done(task, &data->res.seq_res))
3670 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003671 if (nfs4_read_stateid_changed(task, &data->args))
3672 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003673 return data->read_done_cb ? data->read_done_cb(task, data) :
3674 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003675}
3676
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003677static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003680 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003681 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003682 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003685static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3686{
Trond Myklebust9b206142013-03-17 15:52:00 -04003687 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003688 &data->args.seq_args,
3689 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003690 task))
3691 return;
3692 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3693 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
Fred Isamanb029bc92011-03-03 15:13:42 +00003696static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Fred Isamancd841602012-04-20 14:47:44 -04003698 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003700 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003701 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003702 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003704 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003706 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003707 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
Trond Myklebust5521abf2013-03-16 20:54:34 -04003711static bool nfs4_write_stateid_changed(struct rpc_task *task,
3712 struct nfs_writeargs *args)
3713{
3714
3715 if (!nfs4_error_stateid_expired(task->tk_status) ||
3716 nfs4_stateid_is_current(&args->stateid,
3717 args->context,
3718 args->lock_context,
3719 FMODE_WRITE))
3720 return false;
3721 rpc_restart_call_prepare(task);
3722 return true;
3723}
3724
Fred Isamanb029bc92011-03-03 15:13:42 +00003725static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3726{
3727 if (!nfs4_sequence_done(task, &data->res.seq_res))
3728 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003729 if (nfs4_write_stateid_changed(task, &data->args))
3730 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003731 return data->write_done_cb ? data->write_done_cb(task, data) :
3732 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003733}
3734
Trond Myklebust5a37f852012-04-28 14:55:16 -04003735static
3736bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003737{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003738 const struct nfs_pgio_header *hdr = data->header;
3739
3740 /* Don't request attributes for pNFS or O_DIRECT writes */
3741 if (data->ds_clp != NULL || hdr->dreq != NULL)
3742 return false;
3743 /* Otherwise, request attributes if and only if we don't hold
3744 * a delegation
3745 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003746 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003747}
Fred Isamana69aef12011-03-03 15:13:47 +00003748
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003749static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Fred Isamancd841602012-04-20 14:47:44 -04003751 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003752
Trond Myklebust5a37f852012-04-28 14:55:16 -04003753 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003754 data->args.bitmask = NULL;
3755 data->res.fattr = NULL;
3756 } else
3757 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003758
Fred Isamanb029bc92011-03-03 15:13:42 +00003759 if (!data->write_done_cb)
3760 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003761 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 data->timestamp = jiffies;
3763
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003764 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003765 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003768static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3769{
Trond Myklebust9b206142013-03-17 15:52:00 -04003770 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003771 &data->args.seq_args,
3772 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003773 task))
3774 return;
3775 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3776 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Fred Isaman0b7c0152012-04-20 14:47:39 -04003779static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3780{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003781 nfs4_setup_sequence(NFS_SERVER(data->inode),
3782 &data->args.seq_args,
3783 &data->res.seq_res,
3784 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
3786
Fred Isaman0b7c0152012-04-20 14:47:39 -04003787static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003790
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003791 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003792 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003793 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Fred Isaman0b7c0152012-04-20 14:47:39 -04003798static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003799{
3800 if (!nfs4_sequence_done(task, &data->res.seq_res))
3801 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003802 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003803}
3804
Fred Isaman0b7c0152012-04-20 14:47:39 -04003805static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003807 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003808
Fred Isaman0b7c0152012-04-20 14:47:39 -04003809 if (data->commit_done_cb == NULL)
3810 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003811 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003812 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003813 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003816struct nfs4_renewdata {
3817 struct nfs_client *client;
3818 unsigned long timestamp;
3819};
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821/*
3822 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3823 * standalone procedure for queueing an asynchronous RENEW.
3824 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003825static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003826{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003827 struct nfs4_renewdata *data = calldata;
3828 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003829
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003830 if (atomic_read(&clp->cl_count) > 1)
3831 nfs4_schedule_state_renewal(clp);
3832 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003833 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003834}
3835
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003836static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003838 struct nfs4_renewdata *data = calldata;
3839 struct nfs_client *clp = data->client;
3840 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003843 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003844 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3845 return;
3846 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003847 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003848 return;
3849 }
3850 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003852 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003855static const struct rpc_call_ops nfs4_renew_ops = {
3856 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003857 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003858};
3859
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003860static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
3862 struct rpc_message msg = {
3863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3864 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003865 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003867 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003869 if (renew_flags == 0)
3870 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003871 if (!atomic_inc_not_zero(&clp->cl_count))
3872 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003873 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003874 if (data == NULL)
3875 return -ENOMEM;
3876 data->client = clp;
3877 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003878 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003879 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003882static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3886 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003887 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 };
3889 unsigned long now = jiffies;
3890 int status;
3891
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003892 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 if (status < 0)
3894 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003895 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return 0;
3897}
3898
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003899static inline int nfs4_server_supports_acls(struct nfs_server *server)
3900{
3901 return (server->caps & NFS_CAP_ACLS)
3902 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3903 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3904}
3905
Trond Myklebust21f498c2012-08-24 10:59:25 -04003906/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3907 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003908 * the stack.
3909 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003910#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003911
Neil Hormane9e3d722011-03-04 19:26:03 -05003912static int buf_to_pages_noslab(const void *buf, size_t buflen,
3913 struct page **pages, unsigned int *pgbase)
3914{
3915 struct page *newpage, **spages;
3916 int rc = 0;
3917 size_t len;
3918 spages = pages;
3919
3920 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003921 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003922 newpage = alloc_page(GFP_KERNEL);
3923
3924 if (newpage == NULL)
3925 goto unwind;
3926 memcpy(page_address(newpage), buf, len);
3927 buf += len;
3928 buflen -= len;
3929 *pages++ = newpage;
3930 rc++;
3931 } while (buflen != 0);
3932
3933 return rc;
3934
3935unwind:
3936 for(; rc > 0; rc--)
3937 __free_page(spages[rc-1]);
3938 return -ENOMEM;
3939}
3940
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003941struct nfs4_cached_acl {
3942 int cached;
3943 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003944 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003945};
3946
3947static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003948{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003949 struct nfs_inode *nfsi = NFS_I(inode);
3950
3951 spin_lock(&inode->i_lock);
3952 kfree(nfsi->nfs4_acl);
3953 nfsi->nfs4_acl = acl;
3954 spin_unlock(&inode->i_lock);
3955}
3956
3957static void nfs4_zap_acl_attr(struct inode *inode)
3958{
3959 nfs4_set_cached_acl(inode, NULL);
3960}
3961
3962static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3963{
3964 struct nfs_inode *nfsi = NFS_I(inode);
3965 struct nfs4_cached_acl *acl;
3966 int ret = -ENOENT;
3967
3968 spin_lock(&inode->i_lock);
3969 acl = nfsi->nfs4_acl;
3970 if (acl == NULL)
3971 goto out;
3972 if (buf == NULL) /* user is just asking for length */
3973 goto out_len;
3974 if (acl->cached == 0)
3975 goto out;
3976 ret = -ERANGE; /* see getxattr(2) man page */
3977 if (acl->len > buflen)
3978 goto out;
3979 memcpy(buf, acl->data, acl->len);
3980out_len:
3981 ret = acl->len;
3982out:
3983 spin_unlock(&inode->i_lock);
3984 return ret;
3985}
3986
Sachin Prabhu5794d212012-04-17 14:36:40 +01003987static 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 +00003988{
3989 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003990 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003991
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003992 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003993 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003994 if (acl == NULL)
3995 goto out;
3996 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003997 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003998 } else {
3999 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4000 if (acl == NULL)
4001 goto out;
4002 acl->cached = 0;
4003 }
4004 acl->len = acl_len;
4005out:
4006 nfs4_set_cached_acl(inode, acl);
4007}
4008
Andy Adamsonbf118a32011-12-07 11:55:27 -05004009/*
4010 * The getxattr API returns the required buffer length when called with a
4011 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4012 * the required buf. On a NULL buf, we send a page of data to the server
4013 * guessing that the ACL request can be serviced by a page. If so, we cache
4014 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4015 * the cache. If not so, we throw away the page, and cache the required
4016 * length. The next getxattr call will then produce another round trip to
4017 * the server, this time with the input buf of the required size.
4018 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004019static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004020{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004021 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004022 struct nfs_getaclargs args = {
4023 .fh = NFS_FH(inode),
4024 .acl_pages = pages,
4025 .acl_len = buflen,
4026 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004027 struct nfs_getaclres res = {
4028 .acl_len = buflen,
4029 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004030 struct rpc_message msg = {
4031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4032 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004033 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004034 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004035 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4036 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004037
Andy Adamsonbf118a32011-12-07 11:55:27 -05004038 /* As long as we're doing a round trip to the server anyway,
4039 * let's be prepared for a page of acl data. */
4040 if (npages == 0)
4041 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004042 if (npages > ARRAY_SIZE(pages))
4043 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004044
Andy Adamsonbf118a32011-12-07 11:55:27 -05004045 for (i = 0; i < npages; i++) {
4046 pages[i] = alloc_page(GFP_KERNEL);
4047 if (!pages[i])
4048 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004049 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004050
4051 /* for decoding across pages */
4052 res.acl_scratch = alloc_page(GFP_KERNEL);
4053 if (!res.acl_scratch)
4054 goto out_free;
4055
Andy Adamsonbf118a32011-12-07 11:55:27 -05004056 args.acl_len = npages * PAGE_SIZE;
4057 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004058
Peng Taode040be2012-01-10 22:42:47 +08004059 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004060 __func__, buf, buflen, npages, args.acl_len);
4061 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4062 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004063 if (ret)
4064 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004065
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004066 /* Handle the case where the passed-in buffer is too short */
4067 if (res.acl_flags & NFS4_ACL_TRUNC) {
4068 /* Did the user only issue a request for the acl length? */
4069 if (buf == NULL)
4070 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004071 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004072 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004073 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004074 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004075 if (buf) {
4076 if (res.acl_len > buflen) {
4077 ret = -ERANGE;
4078 goto out_free;
4079 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004080 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004081 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004082out_ok:
4083 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004084out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004085 for (i = 0; i < npages; i++)
4086 if (pages[i])
4087 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004088 if (res.acl_scratch)
4089 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004090 return ret;
4091}
4092
Trond Myklebust16b42892006-08-24 12:27:15 -04004093static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4094{
4095 struct nfs4_exception exception = { };
4096 ssize_t ret;
4097 do {
4098 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4099 if (ret >= 0)
4100 break;
4101 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4102 } while (exception.retry);
4103 return ret;
4104}
4105
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004106static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4107{
4108 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004109 int ret;
4110
4111 if (!nfs4_server_supports_acls(server))
4112 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004113 ret = nfs_revalidate_inode(server, inode);
4114 if (ret < 0)
4115 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004116 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4117 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004118 ret = nfs4_read_cached_acl(inode, buf, buflen);
4119 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004120 /* -ENOENT is returned if there is no ACL or if there is an ACL
4121 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004122 return ret;
4123 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004124}
4125
Trond Myklebust16b42892006-08-24 12:27:15 -04004126static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004127{
4128 struct nfs_server *server = NFS_SERVER(inode);
4129 struct page *pages[NFS4ACL_MAXPAGES];
4130 struct nfs_setaclargs arg = {
4131 .fh = NFS_FH(inode),
4132 .acl_pages = pages,
4133 .acl_len = buflen,
4134 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004135 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004136 struct rpc_message msg = {
4137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4138 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004139 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004140 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004141 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004142 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004143
4144 if (!nfs4_server_supports_acls(server))
4145 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004146 if (npages > ARRAY_SIZE(pages))
4147 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004148 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4149 if (i < 0)
4150 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004151 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004152 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004153
4154 /*
4155 * Free each page after tx, so the only ref left is
4156 * held by the network stack
4157 */
4158 for (; i > 0; i--)
4159 put_page(pages[i-1]);
4160
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004161 /*
4162 * Acl update can result in inode attribute update.
4163 * so mark the attribute cache invalid.
4164 */
4165 spin_lock(&inode->i_lock);
4166 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4167 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004168 nfs_access_zap_cache(inode);
4169 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004170 return ret;
4171}
4172
Trond Myklebust16b42892006-08-24 12:27:15 -04004173static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4174{
4175 struct nfs4_exception exception = { };
4176 int err;
4177 do {
4178 err = nfs4_handle_exception(NFS_SERVER(inode),
4179 __nfs4_proc_set_acl(inode, buf, buflen),
4180 &exception);
4181 } while (exception.retry);
4182 return err;
4183}
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004186nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004188 struct nfs_client *clp = server->nfs_client;
4189
4190 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return 0;
4192 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004193 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004194 case -NFS4ERR_ADMIN_REVOKED:
4195 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004196 if (state == NULL)
4197 break;
4198 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004199 case -NFS4ERR_OPENMODE:
4200 if (state == NULL)
4201 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004202 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4203 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004204 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004205 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004206 if (state != NULL) {
4207 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4208 goto stateid_invalid;
4209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004211 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004212 nfs4_schedule_lease_recovery(clp);
4213 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004214#if defined(CONFIG_NFS_V4_1)
4215 case -NFS4ERR_BADSESSION:
4216 case -NFS4ERR_BADSLOT:
4217 case -NFS4ERR_BAD_HIGH_SLOT:
4218 case -NFS4ERR_DEADSESSION:
4219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4220 case -NFS4ERR_SEQ_FALSE_RETRY:
4221 case -NFS4ERR_SEQ_MISORDERED:
4222 dprintk("%s ERROR %d, Reset session\n", __func__,
4223 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004224 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004225 task->tk_status = 0;
4226 return -EAGAIN;
4227#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004229 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004230 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4232 task->tk_status = 0;
4233 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004234 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 case -NFS4ERR_OLD_STATEID:
4236 task->tk_status = 0;
4237 return -EAGAIN;
4238 }
4239 task->tk_status = nfs4_map_errors(task->tk_status);
4240 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004241stateid_invalid:
4242 task->tk_status = -EIO;
4243 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004244wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004245 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004246 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4247 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4248 task->tk_status = 0;
4249 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Chuck Leverf0920752012-05-21 22:45:41 -04004252static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4253 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004254{
4255 __be32 verf[2];
4256
Chuck Lever2c820d92012-05-21 22:45:33 -04004257 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4258 /* An impossible timestamp guarantees this value
4259 * will never match a generated boot time. */
4260 verf[0] = 0;
4261 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4262 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004263 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4264 verf[0] = (__be32)nn->boot_time.tv_sec;
4265 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004266 }
Chuck Levercd937102012-03-02 17:14:31 -05004267 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4268}
4269
Chuck Levere984a552012-09-14 17:24:21 -04004270static unsigned int
4271nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4272 char *buf, size_t len)
4273{
4274 unsigned int result;
4275
4276 rcu_read_lock();
4277 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4278 clp->cl_ipaddr,
4279 rpc_peeraddr2str(clp->cl_rpcclient,
4280 RPC_DISPLAY_ADDR),
4281 rpc_peeraddr2str(clp->cl_rpcclient,
4282 RPC_DISPLAY_PROTO));
4283 rcu_read_unlock();
4284 return result;
4285}
4286
4287static unsigned int
4288nfs4_init_uniform_client_string(const struct nfs_client *clp,
4289 char *buf, size_t len)
4290{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004291 char *nodename = clp->cl_rpcclient->cl_nodename;
4292
4293 if (nfs4_client_id_uniquifier[0] != '\0')
4294 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004295 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4296 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004297 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004298}
4299
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004300/**
4301 * nfs4_proc_setclientid - Negotiate client ID
4302 * @clp: state data structure
4303 * @program: RPC program for NFSv4 callback service
4304 * @port: IP port number for NFS4 callback service
4305 * @cred: RPC credential to use for this call
4306 * @res: where to place the result
4307 *
4308 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4309 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004310int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4311 unsigned short port, struct rpc_cred *cred,
4312 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 nfs4_verifier sc_verifier;
4315 struct nfs4_setclientid setclientid = {
4316 .sc_verifier = &sc_verifier,
4317 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004318 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 };
4320 struct rpc_message msg = {
4321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4322 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004323 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004324 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004326 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Chuck Leverde734832012-07-11 16:30:50 -04004328 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004329 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004330 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4331 setclientid.sc_name_len =
4332 nfs4_init_uniform_client_string(clp,
4333 setclientid.sc_name,
4334 sizeof(setclientid.sc_name));
4335 else
4336 setclientid.sc_name_len =
4337 nfs4_init_nonuniform_client_string(clp,
4338 setclientid.sc_name,
4339 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004340 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004341 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004342 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004343 sizeof(setclientid.sc_netid),
4344 rpc_peeraddr2str(clp->cl_rpcclient,
4345 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004346 rcu_read_unlock();
4347 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004348 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 clp->cl_ipaddr, port >> 8, port & 255);
4350
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004351 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4352 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4353 setclientid.sc_name_len, setclientid.sc_name);
4354 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4355 dprintk("NFS reply setclientid: %d\n", status);
4356 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004359/**
4360 * nfs4_proc_setclientid_confirm - Confirm client ID
4361 * @clp: state data structure
4362 * @res: result of a previous SETCLIENTID
4363 * @cred: RPC credential to use for this call
4364 *
4365 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4366 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004367int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004368 struct nfs4_setclientid_res *arg,
4369 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004373 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004374 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 int status;
4377
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004378 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4379 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4380 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004381 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004382 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return status;
4384}
4385
Trond Myklebustfe650402006-01-03 09:55:18 +01004386struct nfs4_delegreturndata {
4387 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004388 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004389 struct nfs_fh fh;
4390 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004391 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004392 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004393 int rpc_status;
4394};
4395
Trond Myklebustfe650402006-01-03 09:55:18 +01004396static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4397{
4398 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004399
Trond Myklebust14516c32010-07-31 14:29:06 -04004400 if (!nfs4_sequence_done(task, &data->res.seq_res))
4401 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004402
Ricardo Labiaga79708862009-12-07 09:23:21 -05004403 switch (task->tk_status) {
4404 case -NFS4ERR_STALE_STATEID:
4405 case -NFS4ERR_EXPIRED:
4406 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004407 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004408 break;
4409 default:
4410 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4411 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004412 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004413 return;
4414 }
4415 }
4416 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004417}
4418
4419static void nfs4_delegreturn_release(void *calldata)
4420{
Trond Myklebustfe650402006-01-03 09:55:18 +01004421 kfree(calldata);
4422}
4423
Andy Adamson938e1012009-04-01 09:22:28 -04004424#if defined(CONFIG_NFS_V4_1)
4425static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4426{
4427 struct nfs4_delegreturndata *d_data;
4428
4429 d_data = (struct nfs4_delegreturndata *)data;
4430
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004431 nfs4_setup_sequence(d_data->res.server,
4432 &d_data->args.seq_args,
4433 &d_data->res.seq_res,
4434 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004435}
4436#endif /* CONFIG_NFS_V4_1 */
4437
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004438static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004439#if defined(CONFIG_NFS_V4_1)
4440 .rpc_call_prepare = nfs4_delegreturn_prepare,
4441#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004442 .rpc_call_done = nfs4_delegreturn_done,
4443 .rpc_release = nfs4_delegreturn_release,
4444};
4445
Trond Myklebuste6f81072008-01-24 18:14:34 -05004446static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004447{
4448 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004449 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004450 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4453 .rpc_cred = cred,
4454 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004455 struct rpc_task_setup task_setup_data = {
4456 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004457 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004458 .callback_ops = &nfs4_delegreturn_ops,
4459 .flags = RPC_TASK_ASYNC,
4460 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004461 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004462
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004463 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004464 if (data == NULL)
4465 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004466 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004467 data->args.fhandle = &data->fh;
4468 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004469 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004470 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004471 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004472 data->res.fattr = &data->fattr;
4473 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004474 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004475 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004476 data->rpc_status = 0;
4477
Trond Myklebustc970aa82007-07-14 15:39:59 -04004478 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004479 msg.rpc_argp = &data->args;
4480 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004481 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004482 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004483 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004484 if (!issync)
4485 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004486 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004487 if (status != 0)
4488 goto out;
4489 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004490 if (status == 0)
4491 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4492 else
4493 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004494out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004495 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004496 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497}
4498
Trond Myklebuste6f81072008-01-24 18:14:34 -05004499int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
4501 struct nfs_server *server = NFS_SERVER(inode);
4502 struct nfs4_exception exception = { };
4503 int err;
4504 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004505 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 switch (err) {
4507 case -NFS4ERR_STALE_STATEID:
4508 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 case 0:
4510 return 0;
4511 }
4512 err = nfs4_handle_exception(server, err, &exception);
4513 } while (exception.retry);
4514 return err;
4515}
4516
4517#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4518#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4519
4520/*
4521 * sleep, with exponential backoff, and retry the LOCK operation.
4522 */
4523static unsigned long
4524nfs4_set_lock_task_retry(unsigned long timeout)
4525{
Jeff Laytond3103102011-12-01 22:44:39 +01004526 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 timeout <<= 1;
4528 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4529 return NFS4_LOCK_MAXTIMEOUT;
4530 return timeout;
4531}
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4534{
4535 struct inode *inode = state->inode;
4536 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004537 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004538 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004542 struct nfs_lockt_res res = {
4543 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 };
4545 struct rpc_message msg = {
4546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4547 .rpc_argp = &arg,
4548 .rpc_resp = &res,
4549 .rpc_cred = state->owner->so_cred,
4550 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 struct nfs4_lock_state *lsp;
4552 int status;
4553
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004554 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004555 status = nfs4_set_lock_state(state, request);
4556 if (status != 0)
4557 goto out;
4558 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004559 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004560 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004561 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004562 switch (status) {
4563 case 0:
4564 request->fl_type = F_UNLCK;
4565 break;
4566 case -NFS4ERR_DENIED:
4567 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004569 request->fl_ops->fl_release_private(request);
Trond Myklebust55fce162013-10-01 14:24:58 -04004570 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return status;
4573}
4574
4575static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4576{
4577 struct nfs4_exception exception = { };
4578 int err;
4579
4580 do {
4581 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4582 _nfs4_proc_getlk(state, cmd, request),
4583 &exception);
4584 } while (exception.retry);
4585 return err;
4586}
4587
4588static int do_vfs_lock(struct file *file, struct file_lock *fl)
4589{
4590 int res = 0;
4591 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4592 case FL_POSIX:
4593 res = posix_lock_file_wait(file, fl);
4594 break;
4595 case FL_FLOCK:
4596 res = flock_lock_file_wait(file, fl);
4597 break;
4598 default:
4599 BUG();
4600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 return res;
4602}
4603
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004605 struct nfs_locku_args arg;
4606 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004607 struct nfs4_lock_state *lsp;
4608 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004609 struct file_lock fl;
4610 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004611 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004612};
4613
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004614static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4615 struct nfs_open_context *ctx,
4616 struct nfs4_lock_state *lsp,
4617 struct nfs_seqid *seqid)
4618{
4619 struct nfs4_unlockdata *p;
4620 struct inode *inode = lsp->ls_state->inode;
4621
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004622 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004623 if (p == NULL)
4624 return NULL;
4625 p->arg.fh = NFS_FH(inode);
4626 p->arg.fl = &p->fl;
4627 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004628 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004629 p->arg.stateid = &lsp->ls_stateid;
4630 p->lsp = lsp;
4631 atomic_inc(&lsp->ls_count);
4632 /* Ensure we don't close file until we're done freeing locks! */
4633 p->ctx = get_nfs_open_context(ctx);
4634 memcpy(&p->fl, fl, sizeof(p->fl));
4635 p->server = NFS_SERVER(inode);
4636 return p;
4637}
4638
Trond Myklebust06f814a2006-01-03 09:55:07 +01004639static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004640{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004641 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004642 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004643 nfs4_put_lock_state(calldata->lsp);
4644 put_nfs_open_context(calldata->ctx);
4645 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004646}
4647
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004648static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004649{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004650 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004651
Trond Myklebust14516c32010-07-31 14:29:06 -04004652 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4653 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004654 switch (task->tk_status) {
4655 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004656 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4657 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004658 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004659 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004660 case -NFS4ERR_BAD_STATEID:
4661 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004662 case -NFS4ERR_STALE_STATEID:
4663 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004664 break;
4665 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004666 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004667 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004668 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004669 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004670}
4671
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004672static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004673{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004674 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004676 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004677 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004678 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004679 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004680 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004681 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004682 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004683 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004684 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004685 &calldata->res.seq_res,
4686 task) != 0)
4687 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004688 return;
4689out_no_action:
4690 task->tk_action = NULL;
4691out_wait:
4692 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004695static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004696 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004697 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004698 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004699};
4700
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004701static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4702 struct nfs_open_context *ctx,
4703 struct nfs4_lock_state *lsp,
4704 struct nfs_seqid *seqid)
4705{
4706 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4709 .rpc_cred = ctx->cred,
4710 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004711 struct rpc_task_setup task_setup_data = {
4712 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004713 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004714 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004715 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004716 .flags = RPC_TASK_ASYNC,
4717 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004718
Frank Filz137d6ac2007-07-09 15:32:29 -07004719 /* Ensure this is an unlock - when canceling a lock, the
4720 * canceled lock is passed in, and it won't be an unlock.
4721 */
4722 fl->fl_type = F_UNLCK;
4723
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4725 if (data == NULL) {
4726 nfs_free_seqid(seqid);
4727 return ERR_PTR(-ENOMEM);
4728 }
4729
Trond Myklebust9d12b212012-01-17 22:04:25 -05004730 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004731 msg.rpc_argp = &data->arg;
4732 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004733 task_setup_data.callback_data = data;
4734 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735}
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4738{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004739 struct inode *inode = state->inode;
4740 struct nfs4_state_owner *sp = state->owner;
4741 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004742 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004743 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004744 struct rpc_task *task;
4745 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004746 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Trond Myklebust9b073572006-06-29 16:38:34 -04004748 status = nfs4_set_lock_state(state, request);
4749 /* Unlock _before_ we do the RPC call */
4750 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004751 /* Exclude nfs_delegation_claim_locks() */
4752 mutex_lock(&sp->so_delegreturn_mutex);
4753 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004754 down_read(&nfsi->rwsem);
4755 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4756 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004757 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004758 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004759 }
4760 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004761 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004762 if (status != 0)
4763 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004764 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004765 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04004766 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4767 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004768 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004769 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004770 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004771 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004772 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773 status = PTR_ERR(task);
4774 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004775 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004776 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004777 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004778out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004779 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004780 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
4782
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004783struct nfs4_lockdata {
4784 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004785 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004786 struct nfs4_lock_state *lsp;
4787 struct nfs_open_context *ctx;
4788 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004789 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004790 int rpc_status;
4791 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004792 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004793};
4794
4795static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004796 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4797 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004798{
4799 struct nfs4_lockdata *p;
4800 struct inode *inode = lsp->ls_state->inode;
4801 struct nfs_server *server = NFS_SERVER(inode);
4802
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004803 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004804 if (p == NULL)
4805 return NULL;
4806
4807 p->arg.fh = NFS_FH(inode);
4808 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004809 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004810 if (p->arg.open_seqid == NULL)
4811 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004812 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004813 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004814 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004815 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004816 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004817 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004818 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004819 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004820 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004821 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822 atomic_inc(&lsp->ls_count);
4823 p->ctx = get_nfs_open_context(ctx);
4824 memcpy(&p->fl, fl, sizeof(p->fl));
4825 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004826out_free_seqid:
4827 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004828out_free:
4829 kfree(p);
4830 return NULL;
4831}
4832
4833static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4834{
4835 struct nfs4_lockdata *data = calldata;
4836 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Harvey Harrison3110ff82008-05-02 13:42:44 -07004838 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004839 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004840 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 /* Do we need to do an open_to_lock_owner? */
4842 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004843 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004844 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004845 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004846 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004848 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004849 } else
4850 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004851 if (!nfs4_valid_open_stateid(state)) {
4852 data->rpc_status = -EBADF;
4853 task->tk_action = NULL;
4854 goto out_release_open_seqid;
4855 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004856 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004857 if (nfs4_setup_sequence(data->server,
4858 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004859 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004860 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004861 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004862out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004863 nfs_release_seqid(data->arg.open_seqid);
4864out_release_lock_seqid:
4865 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004866out_wait:
4867 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004868 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004869}
4870
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004871static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4872{
4873 struct nfs4_lockdata *data = calldata;
4874
Harvey Harrison3110ff82008-05-02 13:42:44 -07004875 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004876
Trond Myklebust14516c32010-07-31 14:29:06 -04004877 if (!nfs4_sequence_done(task, &data->res.seq_res))
4878 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004879
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004880 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004881 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004882 if (data->rpc_status == 0)
4883 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4884 else
4885 goto out;
4886 }
4887 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004888 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004889 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004890 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004892out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004893 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004894}
4895
4896static void nfs4_lock_release(void *calldata)
4897{
4898 struct nfs4_lockdata *data = calldata;
4899
Harvey Harrison3110ff82008-05-02 13:42:44 -07004900 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004901 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004902 if (data->cancelled != 0) {
4903 struct rpc_task *task;
4904 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4905 data->arg.lock_seqid);
4906 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004907 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004908 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004909 } else
4910 nfs_free_seqid(data->arg.lock_seqid);
4911 nfs4_put_lock_state(data->lsp);
4912 put_nfs_open_context(data->ctx);
4913 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004914 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004915}
4916
4917static const struct rpc_call_ops nfs4_lock_ops = {
4918 .rpc_call_prepare = nfs4_lock_prepare,
4919 .rpc_call_done = nfs4_lock_done,
4920 .rpc_release = nfs4_lock_release,
4921};
4922
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004923static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4924{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004925 switch (error) {
4926 case -NFS4ERR_ADMIN_REVOKED:
4927 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004928 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004929 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004930 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004931 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004932 break;
4933 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004934 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004935 case -NFS4ERR_EXPIRED:
4936 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004937 };
4938}
4939
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004940static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004941{
4942 struct nfs4_lockdata *data;
4943 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004944 struct rpc_message msg = {
4945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4946 .rpc_cred = state->owner->so_cred,
4947 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004948 struct rpc_task_setup task_setup_data = {
4949 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004950 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004951 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004952 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004953 .flags = RPC_TASK_ASYNC,
4954 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004955 int ret;
4956
Harvey Harrison3110ff82008-05-02 13:42:44 -07004957 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004958 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004959 fl->fl_u.nfs4_fl.owner,
4960 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004961 if (data == NULL)
4962 return -ENOMEM;
4963 if (IS_SETLKW(cmd))
4964 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004965 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004966 msg.rpc_argp = &data->arg;
4967 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004968 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004969 if (recovery_type > NFS_LOCK_NEW) {
4970 if (recovery_type == NFS_LOCK_RECLAIM)
4971 data->arg.reclaim = NFS_LOCK_RECLAIM;
4972 nfs4_set_sequence_privileged(&data->arg.seq_args);
4973 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004974 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004975 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004976 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004977 ret = nfs4_wait_for_completion_rpc_task(task);
4978 if (ret == 0) {
4979 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004980 if (ret)
4981 nfs4_handle_setlk_error(data->server, data->lsp,
4982 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004983 } else
4984 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004985 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004986 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988}
4989
4990static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4991{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004992 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004993 struct nfs4_exception exception = {
4994 .inode = state->inode,
4995 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004996 int err;
4997
4998 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004999 /* Cache the lock if possible... */
5000 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5001 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005002 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005003 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005004 break;
5005 nfs4_handle_exception(server, err, &exception);
5006 } while (exception.retry);
5007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
5009
5010static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5011{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005012 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005013 struct nfs4_exception exception = {
5014 .inode = state->inode,
5015 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005016 int err;
5017
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005018 err = nfs4_set_lock_state(state, request);
5019 if (err != 0)
5020 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005021 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005022 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5023 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005024 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005025 switch (err) {
5026 default:
5027 goto out;
5028 case -NFS4ERR_GRACE:
5029 case -NFS4ERR_DELAY:
5030 nfs4_handle_exception(server, err, &exception);
5031 err = 0;
5032 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005033 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005034out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005035 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005038#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005039/**
5040 * nfs41_check_expired_locks - possibly free a lock stateid
5041 *
5042 * @state: NFSv4 state for an inode
5043 *
5044 * Returns NFS_OK if recovery for this stateid is now finished.
5045 * Otherwise a negative NFS4ERR value is returned.
5046 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005047static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005048{
Chuck Levereb64cf92012-07-11 16:30:05 -04005049 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005050 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005051 struct nfs_server *server = NFS_SERVER(state->inode);
5052
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005053 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005054 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005055 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5056 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005057 /* Free the stateid unless the server
5058 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005059 if (status != -NFS4ERR_BAD_STATEID)
5060 nfs41_free_stateid(server,
5061 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005062 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005063 ret = status;
5064 }
5065 }
5066 };
5067
5068 return ret;
5069}
5070
5071static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5072{
5073 int status = NFS_OK;
5074
5075 if (test_bit(LK_STATE_IN_USE, &state->flags))
5076 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005077 if (status != NFS_OK)
5078 status = nfs4_lock_expired(state, request);
5079 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005080}
5081#endif
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5084{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005085 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005086 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005087 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005088 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005089 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005091 if ((fl_flags & FL_POSIX) &&
5092 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5093 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005094 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005095 status = nfs4_set_lock_state(state, request);
5096 if (status != 0)
5097 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005098 request->fl_flags |= FL_ACCESS;
5099 status = do_vfs_lock(request->fl_file, request);
5100 if (status < 0)
5101 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005102 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005103 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005104 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005105 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005106 request->fl_flags = fl_flags & ~FL_SLEEP;
5107 status = do_vfs_lock(request->fl_file, request);
5108 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005109 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005110 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5111 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005112 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005113 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005114 goto out;
5115 down_read(&nfsi->rwsem);
5116 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5117 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005118 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005119 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005120 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005121 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005122 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005123 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5124 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005125out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005126 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005127out:
5128 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 return status;
5130}
5131
5132static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5133{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005134 struct nfs4_exception exception = {
5135 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005136 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 int err;
5139
5140 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005141 err = _nfs4_proc_setlk(state, cmd, request);
5142 if (err == -NFS4ERR_DENIED)
5143 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005145 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 } while (exception.retry);
5147 return err;
5148}
5149
5150static int
5151nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5152{
5153 struct nfs_open_context *ctx;
5154 struct nfs4_state *state;
5155 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5156 int status;
5157
5158 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005159 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 state = ctx->state;
5161
5162 if (request->fl_start < 0 || request->fl_end < 0)
5163 return -EINVAL;
5164
Trond Myklebustd9531262009-07-21 19:22:38 -04005165 if (IS_GETLK(cmd)) {
5166 if (state != NULL)
5167 return nfs4_proc_getlk(state, F_GETLK, request);
5168 return 0;
5169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5172 return -EINVAL;
5173
Trond Myklebustd9531262009-07-21 19:22:38 -04005174 if (request->fl_type == F_UNLCK) {
5175 if (state != NULL)
5176 return nfs4_proc_unlck(state, cmd, request);
5177 return 0;
5178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Trond Myklebustd9531262009-07-21 19:22:38 -04005180 if (state == NULL)
5181 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005182 /*
5183 * Don't rely on the VFS having checked the file open mode,
5184 * since it won't do this for flock() locks.
5185 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005186 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005187 case F_RDLCK:
5188 if (!(filp->f_mode & FMODE_READ))
5189 return -EBADF;
5190 break;
5191 case F_WRLCK:
5192 if (!(filp->f_mode & FMODE_WRITE))
5193 return -EBADF;
5194 }
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 do {
5197 status = nfs4_proc_setlk(state, cmd, request);
5198 if ((status != -EAGAIN) || IS_SETLK(cmd))
5199 break;
5200 timeout = nfs4_set_lock_task_retry(timeout);
5201 status = -ERESTARTSYS;
5202 if (signalled())
5203 break;
5204 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return status;
5206}
5207
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005208int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005209{
5210 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005211 int err;
5212
5213 err = nfs4_set_lock_state(state, fl);
5214 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005215 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005216 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005217 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005218}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005219
Trond Myklebustcf470c32012-03-07 13:49:12 -05005220struct nfs_release_lockowner_data {
5221 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005222 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005223 struct nfs_release_lockowner_args args;
5224};
5225
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005226static void nfs4_release_lockowner_release(void *calldata)
5227{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005228 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005229 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005230 kfree(calldata);
5231}
5232
Trond Myklebust17280172012-03-11 13:11:00 -04005233static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005234 .rpc_release = nfs4_release_lockowner_release,
5235};
5236
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005237static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005238{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005239 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005240 struct rpc_message msg = {
5241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5242 };
5243
5244 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005245 return -EINVAL;
5246 data = kmalloc(sizeof(*data), GFP_NOFS);
5247 if (!data)
5248 return -ENOMEM;
5249 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005250 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005251 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5252 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5253 data->args.lock_owner.s_dev = server->s_dev;
5254 msg.rpc_argp = &data->args;
5255 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5256 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005257}
5258
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005259#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5260
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005261static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5262 const void *buf, size_t buflen,
5263 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005264{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005265 if (strcmp(key, "") != 0)
5266 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005267
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005268 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005269}
5270
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005271static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5272 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005273{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005274 if (strcmp(key, "") != 0)
5275 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005276
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005277 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005278}
5279
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005280static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5281 size_t list_len, const char *name,
5282 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005283{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005284 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005285
J. Bruce Fields096455a2006-03-20 23:23:42 -05005286 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5287 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005288
5289 if (list && len <= list_len)
5290 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005291 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005292}
5293
Andy Adamson533eb462011-06-13 18:25:56 -04005294/*
5295 * nfs_fhget will use either the mounted_on_fileid or the fileid
5296 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005297static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5298{
Andy Adamson533eb462011-06-13 18:25:56 -04005299 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5300 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5301 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005302 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005303 return;
5304
5305 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005306 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005307 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5308 fattr->nlink = 2;
5309}
5310
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005311static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5312 const struct qstr *name,
5313 struct nfs4_fs_locations *fs_locations,
5314 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005315{
5316 struct nfs_server *server = NFS_SERVER(dir);
5317 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005318 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005319 };
5320 struct nfs4_fs_locations_arg args = {
5321 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005322 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005323 .page = page,
5324 .bitmask = bitmask,
5325 };
Benny Halevy22958462009-04-01 09:22:02 -04005326 struct nfs4_fs_locations_res res = {
5327 .fs_locations = fs_locations,
5328 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5331 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005332 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005333 };
5334 int status;
5335
Harvey Harrison3110ff82008-05-02 13:42:44 -07005336 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005337
5338 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5339 * is not supported */
5340 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5341 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5342 else
5343 bitmask[0] |= FATTR4_WORD0_FILEID;
5344
Trond Myklebustc228fd32007-01-13 02:28:11 -05005345 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005346 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005347 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005348 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005349 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005350 return status;
5351}
5352
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005353int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5354 const struct qstr *name,
5355 struct nfs4_fs_locations *fs_locations,
5356 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005357{
5358 struct nfs4_exception exception = { };
5359 int err;
5360 do {
5361 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005362 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005363 &exception);
5364 } while (exception.retry);
5365 return err;
5366}
5367
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005368static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5369{
5370 int status;
5371 struct nfs4_secinfo_arg args = {
5372 .dir_fh = NFS_FH(dir),
5373 .name = name,
5374 };
5375 struct nfs4_secinfo_res res = {
5376 .flavors = flavors,
5377 };
5378 struct rpc_message msg = {
5379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5380 .rpc_argp = &args,
5381 .rpc_resp = &res,
5382 };
5383
5384 dprintk("NFS call secinfo %s\n", name->name);
5385 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5386 dprintk("NFS reply secinfo: %d\n", status);
5387 return status;
5388}
5389
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005390int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5391 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005392{
5393 struct nfs4_exception exception = { };
5394 int err;
5395 do {
5396 err = nfs4_handle_exception(NFS_SERVER(dir),
5397 _nfs4_proc_secinfo(dir, name, flavors),
5398 &exception);
5399 } while (exception.retry);
5400 return err;
5401}
5402
Andy Adamson557134a2009-04-01 09:21:53 -04005403#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005404/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005405 * Check the exchange flags returned by the server for invalid flags, having
5406 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5407 * DS flags set.
5408 */
5409static int nfs4_check_cl_exchange_flags(u32 flags)
5410{
5411 if (flags & ~EXCHGID4_FLAG_MASK_R)
5412 goto out_inval;
5413 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5414 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5415 goto out_inval;
5416 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5417 goto out_inval;
5418 return NFS_OK;
5419out_inval:
5420 return -NFS4ERR_INVAL;
5421}
5422
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005423static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005424nfs41_same_server_scope(struct nfs41_server_scope *a,
5425 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005426{
5427 if (a->server_scope_sz == b->server_scope_sz &&
5428 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5429 return true;
5430
5431 return false;
5432}
5433
Andy Adamson357f54d2010-12-14 10:11:57 -05005434/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005435 * nfs4_proc_bind_conn_to_session()
5436 *
5437 * The 4.1 client currently uses the same TCP connection for the
5438 * fore and backchannel.
5439 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005440int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005441{
5442 int status;
5443 struct nfs41_bind_conn_to_session_res res;
5444 struct rpc_message msg = {
5445 .rpc_proc =
5446 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5447 .rpc_argp = clp,
5448 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005449 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005450 };
5451
5452 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005453
5454 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5455 if (unlikely(res.session == NULL)) {
5456 status = -ENOMEM;
5457 goto out;
5458 }
5459
5460 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5461 if (status == 0) {
5462 if (memcmp(res.session->sess_id.data,
5463 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5464 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5465 status = -EIO;
5466 goto out_session;
5467 }
5468 if (res.dir != NFS4_CDFS4_BOTH) {
5469 dprintk("NFS: %s: Unexpected direction from server\n",
5470 __func__);
5471 status = -EIO;
5472 goto out_session;
5473 }
5474 if (res.use_conn_in_rdma_mode) {
5475 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5476 __func__);
5477 status = -EIO;
5478 goto out_session;
5479 }
5480 }
5481out_session:
5482 kfree(res.session);
5483out:
5484 dprintk("<-- %s status= %d\n", __func__, status);
5485 return status;
5486}
5487
5488/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489 * nfs4_proc_exchange_id()
5490 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005491 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5492 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005493 * Since the clientid has expired, all compounds using sessions
5494 * associated with the stale clientid will be returning
5495 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5496 * be in some phase of session reset.
5497 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005498int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005499{
5500 nfs4_verifier verifier;
5501 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005502 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005503 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005504 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005505 };
5506 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005507 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005508 };
5509 int status;
5510 struct rpc_message msg = {
5511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5512 .rpc_argp = &args,
5513 .rpc_resp = &res,
5514 .rpc_cred = cred,
5515 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005516
Chuck Leverf0920752012-05-21 22:45:41 -04005517 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005518 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5519 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005520 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5521 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5522 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005523
Chuck Leveracdeb692012-05-21 22:46:16 -04005524 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005525 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005526 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005527 status = -ENOMEM;
5528 goto out;
5529 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005530
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005531 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005532 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005533 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005534 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005535 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005536 }
5537
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005538 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005539 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005540 status = -ENOMEM;
5541 goto out_server_scope;
5542 }
5543
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005544 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005545 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005546 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005547
Chuck Lever177313f2012-05-21 22:44:58 -04005548 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005549 clp->cl_clientid = res.clientid;
5550 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5551 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5552 clp->cl_seqid = res.seqid;
5553
Chuck Leveracdeb692012-05-21 22:46:16 -04005554 kfree(clp->cl_serverowner);
5555 clp->cl_serverowner = res.server_owner;
5556 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005557
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005558 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005559 kfree(clp->cl_implid);
5560 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005561
Chuck Lever177313f2012-05-21 22:44:58 -04005562 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005563 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005564 res.server_scope)) {
5565 dprintk("%s: server_scope mismatch detected\n",
5566 __func__);
5567 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005568 kfree(clp->cl_serverscope);
5569 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005570 }
5571
Chuck Lever177313f2012-05-21 22:44:58 -04005572 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005573 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005574 goto out;
5575 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005576 } else
5577 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005578
Chuck Leveracdeb692012-05-21 22:46:16 -04005579out_server_owner:
5580 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005581out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005582 kfree(res.server_scope);
5583out:
Chuck Lever177313f2012-05-21 22:44:58 -04005584 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005585 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005586 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005587 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005588 clp->cl_implid->date.seconds,
5589 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005590 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005591 return status;
5592}
5593
Trond Myklebust66245532012-05-25 17:18:09 -04005594static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5595 struct rpc_cred *cred)
5596{
5597 struct rpc_message msg = {
5598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5599 .rpc_argp = clp,
5600 .rpc_cred = cred,
5601 };
5602 int status;
5603
5604 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5605 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005606 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005607 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5608 return status;
5609}
5610
5611static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5612 struct rpc_cred *cred)
5613{
5614 unsigned int loop;
5615 int ret;
5616
5617 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5618 ret = _nfs4_proc_destroy_clientid(clp, cred);
5619 switch (ret) {
5620 case -NFS4ERR_DELAY:
5621 case -NFS4ERR_CLIENTID_BUSY:
5622 ssleep(1);
5623 break;
5624 default:
5625 return ret;
5626 }
5627 }
5628 return 0;
5629}
5630
5631int nfs4_destroy_clientid(struct nfs_client *clp)
5632{
5633 struct rpc_cred *cred;
5634 int ret = 0;
5635
5636 if (clp->cl_mvops->minor_version < 1)
5637 goto out;
5638 if (clp->cl_exchange_flags == 0)
5639 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005640 if (clp->cl_preserve_clid)
5641 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005642 cred = nfs4_get_exchange_id_cred(clp);
5643 ret = nfs4_proc_destroy_clientid(clp, cred);
5644 if (cred)
5645 put_rpccred(cred);
5646 switch (ret) {
5647 case 0:
5648 case -NFS4ERR_STALE_CLIENTID:
5649 clp->cl_exchange_flags = 0;
5650 }
5651out:
5652 return ret;
5653}
5654
Andy Adamson2050f0c2009-04-01 09:22:30 -04005655struct nfs4_get_lease_time_data {
5656 struct nfs4_get_lease_time_args *args;
5657 struct nfs4_get_lease_time_res *res;
5658 struct nfs_client *clp;
5659};
5660
5661static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5662 void *calldata)
5663{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005664 struct nfs4_get_lease_time_data *data =
5665 (struct nfs4_get_lease_time_data *)calldata;
5666
5667 dprintk("--> %s\n", __func__);
5668 /* just setup sequence, do not trigger session recovery
5669 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005670 nfs41_setup_sequence(data->clp->cl_session,
5671 &data->args->la_seq_args,
5672 &data->res->lr_seq_res,
5673 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005674 dprintk("<-- %s\n", __func__);
5675}
5676
5677/*
5678 * Called from nfs4_state_manager thread for session setup, so don't recover
5679 * from sequence operation or clientid errors.
5680 */
5681static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5682{
5683 struct nfs4_get_lease_time_data *data =
5684 (struct nfs4_get_lease_time_data *)calldata;
5685
5686 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005687 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5688 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005689 switch (task->tk_status) {
5690 case -NFS4ERR_DELAY:
5691 case -NFS4ERR_GRACE:
5692 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5693 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5694 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005695 /* fall through */
5696 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005697 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005698 return;
5699 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005700 dprintk("<-- %s\n", __func__);
5701}
5702
Trond Myklebust17280172012-03-11 13:11:00 -04005703static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005704 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5705 .rpc_call_done = nfs4_get_lease_time_done,
5706};
5707
5708int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5709{
5710 struct rpc_task *task;
5711 struct nfs4_get_lease_time_args args;
5712 struct nfs4_get_lease_time_res res = {
5713 .lr_fsinfo = fsinfo,
5714 };
5715 struct nfs4_get_lease_time_data data = {
5716 .args = &args,
5717 .res = &res,
5718 .clp = clp,
5719 };
5720 struct rpc_message msg = {
5721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5722 .rpc_argp = &args,
5723 .rpc_resp = &res,
5724 };
5725 struct rpc_task_setup task_setup = {
5726 .rpc_client = clp->cl_rpcclient,
5727 .rpc_message = &msg,
5728 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005729 .callback_data = &data,
5730 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005731 };
5732 int status;
5733
Trond Myklebust9d12b212012-01-17 22:04:25 -05005734 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005735 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005736 dprintk("--> %s\n", __func__);
5737 task = rpc_run_task(&task_setup);
5738
5739 if (IS_ERR(task))
5740 status = PTR_ERR(task);
5741 else {
5742 status = task->tk_status;
5743 rpc_put_task(task);
5744 }
5745 dprintk("<-- %s return %d\n", __func__, status);
5746
5747 return status;
5748}
5749
Andy Adamsonfc931582009-04-01 09:22:31 -04005750/*
5751 * Initialize the values to be used by the client in CREATE_SESSION
5752 * If nfs4_init_session set the fore channel request and response sizes,
5753 * use them.
5754 *
5755 * Set the back channel max_resp_sz_cached to zero to force the client to
5756 * always set csa_cachethis to FALSE because the current implementation
5757 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5758 */
5759static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5760{
5761 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005762 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5763 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005764
5765 if (mxrqst_sz == 0)
5766 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5767 if (mxresp_sz == 0)
5768 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5769 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005770 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5771 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005772 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005773 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005774
5775 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005776 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005777 __func__,
5778 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005779 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005780
5781 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005782 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5783 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5784 args->bc_attrs.max_resp_sz_cached = 0;
5785 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5786 args->bc_attrs.max_reqs = 1;
5787
5788 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5789 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5790 __func__,
5791 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5792 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5793 args->bc_attrs.max_reqs);
5794}
5795
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005796static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005797{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005798 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5799 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5800
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005801 if (rcvd->max_resp_sz > sent->max_resp_sz)
5802 return -EINVAL;
5803 /*
5804 * Our requested max_ops is the minimum we need; we're not
5805 * prepared to break up compounds into smaller pieces than that.
5806 * So, no point even trying to continue if the server won't
5807 * cooperate:
5808 */
5809 if (rcvd->max_ops < sent->max_ops)
5810 return -EINVAL;
5811 if (rcvd->max_reqs == 0)
5812 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005813 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5814 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005815 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005816}
5817
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005818static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5819{
5820 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5821 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005822
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005823 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5824 return -EINVAL;
5825 if (rcvd->max_resp_sz < sent->max_resp_sz)
5826 return -EINVAL;
5827 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5828 return -EINVAL;
5829 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005830 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005831 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005832 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005833 return -EINVAL;
5834 return 0;
5835}
Andy Adamson8d353012009-04-01 09:22:32 -04005836
Andy Adamson8d353012009-04-01 09:22:32 -04005837static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5838 struct nfs4_session *session)
5839{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005840 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005841
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005842 ret = nfs4_verify_fore_channel_attrs(args, session);
5843 if (ret)
5844 return ret;
5845 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005846}
5847
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005848static int _nfs4_proc_create_session(struct nfs_client *clp,
5849 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005850{
5851 struct nfs4_session *session = clp->cl_session;
5852 struct nfs41_create_session_args args = {
5853 .client = clp,
5854 .cb_program = NFS4_CALLBACK,
5855 };
5856 struct nfs41_create_session_res res = {
5857 .client = clp,
5858 };
5859 struct rpc_message msg = {
5860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5861 .rpc_argp = &args,
5862 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005863 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005864 };
5865 int status;
5866
5867 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005868 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005869
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005870 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005871
Trond Myklebust43095d32012-11-20 11:13:12 -05005872 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005873 /* Verify the session's negotiated channel_attrs values */
5874 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005875 /* Increment the clientid slot sequence id */
5876 clp->cl_seqid++;
5877 }
5878
5879 return status;
5880}
5881
5882/*
5883 * Issues a CREATE_SESSION operation to the server.
5884 * It is the responsibility of the caller to verify the session is
5885 * expired before calling this routine.
5886 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005887int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005888{
5889 int status;
5890 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005891 struct nfs4_session *session = clp->cl_session;
5892
5893 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5894
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005895 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005896 if (status)
5897 goto out;
5898
Andy Adamsonaacd5532011-11-09 13:58:21 -05005899 /* Init or reset the session slot tables */
5900 status = nfs4_setup_session_slot_tables(session);
5901 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005902 if (status)
5903 goto out;
5904
5905 ptr = (unsigned *)&session->sess_id.data[0];
5906 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5907 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005908out:
5909 dprintk("<-- %s\n", __func__);
5910 return status;
5911}
5912
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005913/*
5914 * Issue the over-the-wire RPC DESTROY_SESSION.
5915 * The caller must serialize access to this routine.
5916 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005917int nfs4_proc_destroy_session(struct nfs4_session *session,
5918 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005919{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005920 struct rpc_message msg = {
5921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5922 .rpc_argp = session,
5923 .rpc_cred = cred,
5924 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005925 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005926
5927 dprintk("--> nfs4_proc_destroy_session\n");
5928
5929 /* session is still being setup */
5930 if (session->clp->cl_cons_state != NFS_CS_READY)
5931 return status;
5932
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005933 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005934
5935 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005936 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005937 "Session has been destroyed regardless...\n", status);
5938
5939 dprintk("<-- nfs4_proc_destroy_session\n");
5940 return status;
5941}
5942
Trond Myklebust7b38c362012-05-23 13:23:31 -04005943/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005944 * Renew the cl_session lease.
5945 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005946struct nfs4_sequence_data {
5947 struct nfs_client *clp;
5948 struct nfs4_sequence_args args;
5949 struct nfs4_sequence_res res;
5950};
5951
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005952static void nfs41_sequence_release(void *data)
5953{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005954 struct nfs4_sequence_data *calldata = data;
5955 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005956
Alexandros Batsakis71358402010-02-05 03:45:05 -08005957 if (atomic_read(&clp->cl_count) > 1)
5958 nfs4_schedule_state_renewal(clp);
5959 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005960 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005961}
5962
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005963static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5964{
5965 switch(task->tk_status) {
5966 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005967 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5968 return -EAGAIN;
5969 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005970 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005971 }
5972 return 0;
5973}
5974
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005975static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005976{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005977 struct nfs4_sequence_data *calldata = data;
5978 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979
Trond Myklebust14516c32010-07-31 14:29:06 -04005980 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5981 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982
5983 if (task->tk_status < 0) {
5984 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005985 if (atomic_read(&clp->cl_count) == 1)
5986 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005987
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005988 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5989 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005990 return;
5991 }
5992 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005993 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005994out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005995 dprintk("<-- %s\n", __func__);
5996}
5997
5998static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5999{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006000 struct nfs4_sequence_data *calldata = data;
6001 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006002 struct nfs4_sequence_args *args;
6003 struct nfs4_sequence_res *res;
6004
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006005 args = task->tk_msg.rpc_argp;
6006 res = task->tk_msg.rpc_resp;
6007
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006008 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006009}
6010
6011static const struct rpc_call_ops nfs41_sequence_ops = {
6012 .rpc_call_done = nfs41_sequence_call_done,
6013 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006014 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006015};
6016
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006017static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6018 struct rpc_cred *cred,
6019 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006020{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006021 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006022 struct rpc_message msg = {
6023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6024 .rpc_cred = cred,
6025 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006026 struct rpc_task_setup task_setup_data = {
6027 .rpc_client = clp->cl_rpcclient,
6028 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006029 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006030 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006031 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006032
Alexandros Batsakis71358402010-02-05 03:45:05 -08006033 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006034 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006035 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006036 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006037 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006038 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006039 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006040 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006041 if (is_privileged)
6042 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006043 msg.rpc_argp = &calldata->args;
6044 msg.rpc_resp = &calldata->res;
6045 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006046 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006047
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006048 return rpc_run_task(&task_setup_data);
6049}
6050
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006051static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006052{
6053 struct rpc_task *task;
6054 int ret = 0;
6055
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006056 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6057 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006058 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006059 if (IS_ERR(task))
6060 ret = PTR_ERR(task);
6061 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006062 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006063 dprintk("<-- %s status=%d\n", __func__, ret);
6064 return ret;
6065}
6066
6067static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6068{
6069 struct rpc_task *task;
6070 int ret;
6071
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006072 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006073 if (IS_ERR(task)) {
6074 ret = PTR_ERR(task);
6075 goto out;
6076 }
6077 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006078 if (!ret) {
6079 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6080
6081 if (task->tk_status == 0)
6082 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006083 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006084 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006085 rpc_put_task(task);
6086out:
6087 dprintk("<-- %s status=%d\n", __func__, ret);
6088 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006089}
6090
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006091struct nfs4_reclaim_complete_data {
6092 struct nfs_client *clp;
6093 struct nfs41_reclaim_complete_args arg;
6094 struct nfs41_reclaim_complete_res res;
6095};
6096
6097static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6098{
6099 struct nfs4_reclaim_complete_data *calldata = data;
6100
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006101 nfs41_setup_sequence(calldata->clp->cl_session,
6102 &calldata->arg.seq_args,
6103 &calldata->res.seq_res,
6104 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105}
6106
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006107static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6108{
6109 switch(task->tk_status) {
6110 case 0:
6111 case -NFS4ERR_COMPLETE_ALREADY:
6112 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6113 break;
6114 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006115 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006116 /* fall through */
6117 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006118 return -EAGAIN;
6119 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006120 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006121 }
6122 return 0;
6123}
6124
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006125static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6126{
6127 struct nfs4_reclaim_complete_data *calldata = data;
6128 struct nfs_client *clp = calldata->clp;
6129 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6130
6131 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006132 if (!nfs41_sequence_done(task, res))
6133 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006134
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006135 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6136 rpc_restart_call_prepare(task);
6137 return;
6138 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006139 dprintk("<-- %s\n", __func__);
6140}
6141
6142static void nfs4_free_reclaim_complete_data(void *data)
6143{
6144 struct nfs4_reclaim_complete_data *calldata = data;
6145
6146 kfree(calldata);
6147}
6148
6149static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6150 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6151 .rpc_call_done = nfs4_reclaim_complete_done,
6152 .rpc_release = nfs4_free_reclaim_complete_data,
6153};
6154
6155/*
6156 * Issue a global reclaim complete.
6157 */
6158static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6159{
6160 struct nfs4_reclaim_complete_data *calldata;
6161 struct rpc_task *task;
6162 struct rpc_message msg = {
6163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6164 };
6165 struct rpc_task_setup task_setup_data = {
6166 .rpc_client = clp->cl_rpcclient,
6167 .rpc_message = &msg,
6168 .callback_ops = &nfs4_reclaim_complete_call_ops,
6169 .flags = RPC_TASK_ASYNC,
6170 };
6171 int status = -ENOMEM;
6172
6173 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006174 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006175 if (calldata == NULL)
6176 goto out;
6177 calldata->clp = clp;
6178 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006179
Trond Myklebust9d12b212012-01-17 22:04:25 -05006180 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006181 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006182 msg.rpc_argp = &calldata->arg;
6183 msg.rpc_resp = &calldata->res;
6184 task_setup_data.callback_data = calldata;
6185 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006186 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006187 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006188 goto out;
6189 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006190 status = nfs4_wait_for_completion_rpc_task(task);
6191 if (status == 0)
6192 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006193 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006194 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006195out:
6196 dprintk("<-- %s status=%d\n", __func__, status);
6197 return status;
6198}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006199
6200static void
6201nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6202{
6203 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006204 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006205 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206
6207 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006208 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6209 * right now covering the LAYOUTGET we are about to send.
6210 * However, that is not so catastrophic, and there seems
6211 * to be no way to prevent it completely.
6212 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006213 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006214 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006215 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006216 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6217 NFS_I(lgp->args.inode)->layout,
6218 lgp->args.ctx->state)) {
6219 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006220 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006221}
6222
6223static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6224{
6225 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006226 struct inode *inode = lgp->args.inode;
6227 struct nfs_server *server = NFS_SERVER(inode);
6228 struct pnfs_layout_hdr *lo;
6229 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006230 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006231
6232 dprintk("--> %s\n", __func__);
6233
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006234 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006235 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006236
6237 switch (task->tk_status) {
6238 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006239 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006240 case -NFS4ERR_LAYOUTTRYLATER:
6241 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006242 timeo = rpc_get_timeout(task->tk_client);
6243 giveup = lgp->args.timestamp + timeo;
6244 if (time_after(giveup, jiffies))
6245 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006246 break;
6247 case -NFS4ERR_EXPIRED:
6248 case -NFS4ERR_BAD_STATEID:
6249 spin_lock(&inode->i_lock);
6250 lo = NFS_I(inode)->layout;
6251 if (!lo || list_empty(&lo->plh_segs)) {
6252 spin_unlock(&inode->i_lock);
6253 /* If the open stateid was bad, then recover it. */
6254 state = lgp->args.ctx->state;
6255 } else {
6256 LIST_HEAD(head);
6257
6258 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6259 spin_unlock(&inode->i_lock);
6260 /* Mark the bad layout state as invalid, then
6261 * retry using the open stateid. */
6262 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006263 }
6264 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006265 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6266 rpc_restart_call_prepare(task);
6267out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006268 dprintk("<-- %s\n", __func__);
6269}
6270
Idan Kedar85541162012-08-02 11:47:10 +03006271static size_t max_response_pages(struct nfs_server *server)
6272{
6273 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6274 return nfs_page_array_len(0, max_resp_sz);
6275}
6276
6277static void nfs4_free_pages(struct page **pages, size_t size)
6278{
6279 int i;
6280
6281 if (!pages)
6282 return;
6283
6284 for (i = 0; i < size; i++) {
6285 if (!pages[i])
6286 break;
6287 __free_page(pages[i]);
6288 }
6289 kfree(pages);
6290}
6291
6292static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6293{
6294 struct page **pages;
6295 int i;
6296
6297 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6298 if (!pages) {
6299 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6300 return NULL;
6301 }
6302
6303 for (i = 0; i < size; i++) {
6304 pages[i] = alloc_page(gfp_flags);
6305 if (!pages[i]) {
6306 dprintk("%s: failed to allocate page\n", __func__);
6307 nfs4_free_pages(pages, size);
6308 return NULL;
6309 }
6310 }
6311
6312 return pages;
6313}
6314
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006315static void nfs4_layoutget_release(void *calldata)
6316{
6317 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006318 struct inode *inode = lgp->args.inode;
6319 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006320 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321
6322 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006323 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006324 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006325 put_nfs_open_context(lgp->args.ctx);
6326 kfree(calldata);
6327 dprintk("<-- %s\n", __func__);
6328}
6329
6330static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6331 .rpc_call_prepare = nfs4_layoutget_prepare,
6332 .rpc_call_done = nfs4_layoutget_done,
6333 .rpc_release = nfs4_layoutget_release,
6334};
6335
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006336struct pnfs_layout_segment *
6337nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006338{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006339 struct inode *inode = lgp->args.inode;
6340 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006341 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006342 struct rpc_task *task;
6343 struct rpc_message msg = {
6344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6345 .rpc_argp = &lgp->args,
6346 .rpc_resp = &lgp->res,
6347 };
6348 struct rpc_task_setup task_setup_data = {
6349 .rpc_client = server->client,
6350 .rpc_message = &msg,
6351 .callback_ops = &nfs4_layoutget_call_ops,
6352 .callback_data = lgp,
6353 .flags = RPC_TASK_ASYNC,
6354 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006355 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006356 int status = 0;
6357
6358 dprintk("--> %s\n", __func__);
6359
Idan Kedar85541162012-08-02 11:47:10 +03006360 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6361 if (!lgp->args.layout.pages) {
6362 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006363 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006364 }
6365 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006366 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006367
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006368 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006369 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006370 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006371
6372 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6373 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6374
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006375 task = rpc_run_task(&task_setup_data);
6376 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006377 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006378 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006379 if (status == 0)
6380 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006381 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6382 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006383 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006384 rpc_put_task(task);
6385 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006386 if (status)
6387 return ERR_PTR(status);
6388 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006389}
6390
Benny Halevycbe82602011-05-22 19:52:37 +03006391static void
6392nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6393{
6394 struct nfs4_layoutreturn *lrp = calldata;
6395
6396 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006397 nfs41_setup_sequence(lrp->clp->cl_session,
6398 &lrp->args.seq_args,
6399 &lrp->res.seq_res,
6400 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006401}
6402
6403static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6404{
6405 struct nfs4_layoutreturn *lrp = calldata;
6406 struct nfs_server *server;
6407
6408 dprintk("--> %s\n", __func__);
6409
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006410 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006411 return;
6412
6413 server = NFS_SERVER(lrp->args.inode);
6414 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006415 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006416 return;
6417 }
Benny Halevycbe82602011-05-22 19:52:37 +03006418 dprintk("<-- %s\n", __func__);
6419}
6420
6421static void nfs4_layoutreturn_release(void *calldata)
6422{
6423 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006424 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006425
6426 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006427 spin_lock(&lo->plh_inode->i_lock);
6428 if (lrp->res.lrs_present)
6429 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6430 lo->plh_block_lgets--;
6431 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006432 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006433 kfree(calldata);
6434 dprintk("<-- %s\n", __func__);
6435}
6436
6437static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6438 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6439 .rpc_call_done = nfs4_layoutreturn_done,
6440 .rpc_release = nfs4_layoutreturn_release,
6441};
6442
6443int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6444{
6445 struct rpc_task *task;
6446 struct rpc_message msg = {
6447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6448 .rpc_argp = &lrp->args,
6449 .rpc_resp = &lrp->res,
6450 };
6451 struct rpc_task_setup task_setup_data = {
6452 .rpc_client = lrp->clp->cl_rpcclient,
6453 .rpc_message = &msg,
6454 .callback_ops = &nfs4_layoutreturn_call_ops,
6455 .callback_data = lrp,
6456 };
6457 int status;
6458
6459 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006460 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006461 task = rpc_run_task(&task_setup_data);
6462 if (IS_ERR(task))
6463 return PTR_ERR(task);
6464 status = task->tk_status;
6465 dprintk("<-- %s status=%d\n", __func__, status);
6466 rpc_put_task(task);
6467 return status;
6468}
6469
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006470/*
6471 * Retrieve the list of Data Server devices from the MDS.
6472 */
6473static int _nfs4_getdevicelist(struct nfs_server *server,
6474 const struct nfs_fh *fh,
6475 struct pnfs_devicelist *devlist)
6476{
6477 struct nfs4_getdevicelist_args args = {
6478 .fh = fh,
6479 .layoutclass = server->pnfs_curr_ld->id,
6480 };
6481 struct nfs4_getdevicelist_res res = {
6482 .devlist = devlist,
6483 };
6484 struct rpc_message msg = {
6485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6486 .rpc_argp = &args,
6487 .rpc_resp = &res,
6488 };
6489 int status;
6490
6491 dprintk("--> %s\n", __func__);
6492 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6493 &res.seq_res, 0);
6494 dprintk("<-- %s status=%d\n", __func__, status);
6495 return status;
6496}
6497
6498int nfs4_proc_getdevicelist(struct nfs_server *server,
6499 const struct nfs_fh *fh,
6500 struct pnfs_devicelist *devlist)
6501{
6502 struct nfs4_exception exception = { };
6503 int err;
6504
6505 do {
6506 err = nfs4_handle_exception(server,
6507 _nfs4_getdevicelist(server, fh, devlist),
6508 &exception);
6509 } while (exception.retry);
6510
6511 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6512 err, devlist->num_devs);
6513
6514 return err;
6515}
6516EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6517
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006518static int
6519_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6520{
6521 struct nfs4_getdeviceinfo_args args = {
6522 .pdev = pdev,
6523 };
6524 struct nfs4_getdeviceinfo_res res = {
6525 .pdev = pdev,
6526 };
6527 struct rpc_message msg = {
6528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6529 .rpc_argp = &args,
6530 .rpc_resp = &res,
6531 };
6532 int status;
6533
6534 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006535 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006536 dprintk("<-- %s status=%d\n", __func__, status);
6537
6538 return status;
6539}
6540
6541int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6542{
6543 struct nfs4_exception exception = { };
6544 int err;
6545
6546 do {
6547 err = nfs4_handle_exception(server,
6548 _nfs4_proc_getdeviceinfo(server, pdev),
6549 &exception);
6550 } while (exception.retry);
6551 return err;
6552}
6553EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6554
Andy Adamson863a3c62011-03-23 13:27:54 +00006555static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6556{
6557 struct nfs4_layoutcommit_data *data = calldata;
6558 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006559 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006560
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006561 nfs41_setup_sequence(session,
6562 &data->args.seq_args,
6563 &data->res.seq_res,
6564 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006565}
6566
6567static void
6568nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6569{
6570 struct nfs4_layoutcommit_data *data = calldata;
6571 struct nfs_server *server = NFS_SERVER(data->args.inode);
6572
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006573 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006574 return;
6575
6576 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006577 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6578 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6579 case -NFS4ERR_BADLAYOUT: /* no layout */
6580 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006581 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006582 break;
6583 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006584 nfs_post_op_update_inode_force_wcc(data->args.inode,
6585 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006586 break;
6587 default:
6588 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6589 rpc_restart_call_prepare(task);
6590 return;
6591 }
6592 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006593}
6594
6595static void nfs4_layoutcommit_release(void *calldata)
6596{
6597 struct nfs4_layoutcommit_data *data = calldata;
6598
Andy Adamsondb29c082011-07-30 20:52:38 -04006599 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006600 put_rpccred(data->cred);
6601 kfree(data);
6602}
6603
6604static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6605 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6606 .rpc_call_done = nfs4_layoutcommit_done,
6607 .rpc_release = nfs4_layoutcommit_release,
6608};
6609
6610int
Andy Adamsonef311532011-03-12 02:58:10 -05006611nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006612{
6613 struct rpc_message msg = {
6614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6615 .rpc_argp = &data->args,
6616 .rpc_resp = &data->res,
6617 .rpc_cred = data->cred,
6618 };
6619 struct rpc_task_setup task_setup_data = {
6620 .task = &data->task,
6621 .rpc_client = NFS_CLIENT(data->args.inode),
6622 .rpc_message = &msg,
6623 .callback_ops = &nfs4_layoutcommit_ops,
6624 .callback_data = data,
6625 .flags = RPC_TASK_ASYNC,
6626 };
6627 struct rpc_task *task;
6628 int status = 0;
6629
6630 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6631 "lbw: %llu inode %lu\n",
6632 data->task.tk_pid, sync,
6633 data->args.lastbytewritten,
6634 data->args.inode->i_ino);
6635
Trond Myklebust9d12b212012-01-17 22:04:25 -05006636 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006637 task = rpc_run_task(&task_setup_data);
6638 if (IS_ERR(task))
6639 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006640 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006641 goto out;
6642 status = nfs4_wait_for_completion_rpc_task(task);
6643 if (status != 0)
6644 goto out;
6645 status = task->tk_status;
6646out:
6647 dprintk("%s: status %d\n", __func__, status);
6648 rpc_put_task(task);
6649 return status;
6650}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006651
6652static int
6653_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6654 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6655{
6656 struct nfs41_secinfo_no_name_args args = {
6657 .style = SECINFO_STYLE_CURRENT_FH,
6658 };
6659 struct nfs4_secinfo_res res = {
6660 .flavors = flavors,
6661 };
6662 struct rpc_message msg = {
6663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6664 .rpc_argp = &args,
6665 .rpc_resp = &res,
6666 };
6667 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6668}
6669
6670static int
6671nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6672 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6673{
6674 struct nfs4_exception exception = { };
6675 int err;
6676 do {
6677 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6678 switch (err) {
6679 case 0:
6680 case -NFS4ERR_WRONGSEC:
6681 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006682 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006683 default:
6684 err = nfs4_handle_exception(server, err, &exception);
6685 }
6686 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006687out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006688 return err;
6689}
6690
6691static int
6692nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6693 struct nfs_fsinfo *info)
6694{
6695 int err;
6696 struct page *page;
6697 rpc_authflavor_t flavor;
6698 struct nfs4_secinfo_flavors *flavors;
6699
6700 page = alloc_page(GFP_KERNEL);
6701 if (!page) {
6702 err = -ENOMEM;
6703 goto out;
6704 }
6705
6706 flavors = page_address(page);
6707 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6708
6709 /*
6710 * Fall back on "guess and check" method if
6711 * the server doesn't support SECINFO_NO_NAME
6712 */
6713 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6714 err = nfs4_find_root_sec(server, fhandle, info);
6715 goto out_freepage;
6716 }
6717 if (err)
6718 goto out_freepage;
6719
6720 flavor = nfs_find_best_sec(flavors);
6721 if (err == 0)
6722 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6723
6724out_freepage:
6725 put_page(page);
6726 if (err == -EACCES)
6727 return -EPERM;
6728out:
6729 return err;
6730}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006731
6732static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006733{
6734 int status;
6735 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006736 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006737 };
6738 struct nfs41_test_stateid_res res;
6739 struct rpc_message msg = {
6740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6741 .rpc_argp = &args,
6742 .rpc_resp = &res,
6743 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006744
Chuck Lever38527b12012-07-11 16:30:23 -04006745 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006746 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006747 nfs4_set_sequence_privileged(&args.seq_args);
6748 status = nfs4_call_sync_sequence(server->client, server, &msg,
6749 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006750 if (status != NFS_OK) {
6751 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006752 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006753 }
6754 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006755 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006756}
6757
Chuck Lever38527b12012-07-11 16:30:23 -04006758/**
6759 * nfs41_test_stateid - perform a TEST_STATEID operation
6760 *
6761 * @server: server / transport on which to perform the operation
6762 * @stateid: state ID to test
6763 *
6764 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6765 * Otherwise a negative NFS4ERR value is returned if the operation
6766 * failed or the state ID is not currently valid.
6767 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006768static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006769{
6770 struct nfs4_exception exception = { };
6771 int err;
6772 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006773 err = _nfs41_test_stateid(server, stateid);
6774 if (err != -NFS4ERR_DELAY)
6775 break;
6776 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006777 } while (exception.retry);
6778 return err;
6779}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006780
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006781struct nfs_free_stateid_data {
6782 struct nfs_server *server;
6783 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006784 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006785};
6786
6787static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6788{
6789 struct nfs_free_stateid_data *data = calldata;
6790 nfs41_setup_sequence(nfs4_get_session(data->server),
6791 &data->args.seq_args,
6792 &data->res.seq_res,
6793 task);
6794}
6795
6796static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6797{
6798 struct nfs_free_stateid_data *data = calldata;
6799
6800 nfs41_sequence_done(task, &data->res.seq_res);
6801
6802 switch (task->tk_status) {
6803 case -NFS4ERR_DELAY:
6804 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6805 rpc_restart_call_prepare(task);
6806 }
6807}
6808
6809static void nfs41_free_stateid_release(void *calldata)
6810{
6811 kfree(calldata);
6812}
6813
6814const struct rpc_call_ops nfs41_free_stateid_ops = {
6815 .rpc_call_prepare = nfs41_free_stateid_prepare,
6816 .rpc_call_done = nfs41_free_stateid_done,
6817 .rpc_release = nfs41_free_stateid_release,
6818};
6819
6820static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6821 nfs4_stateid *stateid,
6822 bool privileged)
6823{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006824 struct rpc_message msg = {
6825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006826 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006827 struct rpc_task_setup task_setup = {
6828 .rpc_client = server->client,
6829 .rpc_message = &msg,
6830 .callback_ops = &nfs41_free_stateid_ops,
6831 .flags = RPC_TASK_ASYNC,
6832 };
6833 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006834
Chuck Lever38527b12012-07-11 16:30:23 -04006835 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006836 data = kmalloc(sizeof(*data), GFP_NOFS);
6837 if (!data)
6838 return ERR_PTR(-ENOMEM);
6839 data->server = server;
6840 nfs4_stateid_copy(&data->args.stateid, stateid);
6841
6842 task_setup.callback_data = data;
6843
6844 msg.rpc_argp = &data->args;
6845 msg.rpc_resp = &data->res;
6846 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6847 if (privileged)
6848 nfs4_set_sequence_privileged(&data->args.seq_args);
6849
6850 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006851}
6852
Chuck Lever38527b12012-07-11 16:30:23 -04006853/**
6854 * nfs41_free_stateid - perform a FREE_STATEID operation
6855 *
6856 * @server: server / transport on which to perform the operation
6857 * @stateid: state ID to release
6858 *
6859 * Returns NFS_OK if the server freed "stateid". Otherwise a
6860 * negative NFS4ERR value is returned.
6861 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006862static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006863{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04006864 struct rpc_task *task;
6865 int ret;
6866
6867 task = _nfs41_free_stateid(server, stateid, true);
6868 if (IS_ERR(task))
6869 return PTR_ERR(task);
6870 ret = rpc_wait_for_completion_task(task);
6871 if (!ret)
6872 ret = task->tk_status;
6873 rpc_put_task(task);
6874 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006875}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006876
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006877static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6878{
6879 struct rpc_task *task;
6880
6881 task = _nfs41_free_stateid(server, &lsp->ls_stateid, false);
6882 nfs4_free_lock_state(server, lsp);
6883 if (IS_ERR(task))
6884 return PTR_ERR(task);
6885 rpc_put_task(task);
6886 return 0;
6887}
6888
Trond Myklebust36281ca2012-03-04 18:13:56 -05006889static bool nfs41_match_stateid(const nfs4_stateid *s1,
6890 const nfs4_stateid *s2)
6891{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006892 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006893 return false;
6894
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006895 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006896 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006897 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006898 return true;
6899
6900 return false;
6901}
6902
Andy Adamson557134a2009-04-01 09:21:53 -04006903#endif /* CONFIG_NFS_V4_1 */
6904
Trond Myklebust36281ca2012-03-04 18:13:56 -05006905static bool nfs4_match_stateid(const nfs4_stateid *s1,
6906 const nfs4_stateid *s2)
6907{
Trond Myklebustf597c532012-03-04 18:13:56 -05006908 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006909}
6910
6911
Trond Myklebust17280172012-03-11 13:11:00 -04006912static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006913 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006914 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 .recover_open = nfs4_open_reclaim,
6916 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006917 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006918 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006919 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920};
6921
Andy Adamson591d71c2009-04-01 09:22:47 -04006922#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006923static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006924 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6925 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6926 .recover_open = nfs4_open_reclaim,
6927 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006928 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006929 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006930 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006931 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006932};
6933#endif /* CONFIG_NFS_V4_1 */
6934
Trond Myklebust17280172012-03-11 13:11:00 -04006935static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006936 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006937 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 .recover_open = nfs4_open_expired,
6939 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006940 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006941 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942};
6943
Andy Adamson591d71c2009-04-01 09:22:47 -04006944#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006945static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006946 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6947 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006948 .recover_open = nfs41_open_expired,
6949 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006950 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006951 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006952};
6953#endif /* CONFIG_NFS_V4_1 */
6954
Trond Myklebust17280172012-03-11 13:11:00 -04006955static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006956 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006957 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006958 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006959};
6960
6961#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006962static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006963 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006964 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006965 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006966};
6967#endif
6968
Trond Myklebust97dc1352010-06-16 09:52:26 -04006969static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6970 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006971 .init_caps = NFS_CAP_READDIRPLUS
6972 | NFS_CAP_ATOMIC_OPEN
6973 | NFS_CAP_CHANGE_ATTR
6974 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006975 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006976 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006977 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006978 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006979 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6980 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6981 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006982};
6983
6984#if defined(CONFIG_NFS_V4_1)
6985static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6986 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006987 .init_caps = NFS_CAP_READDIRPLUS
6988 | NFS_CAP_ATOMIC_OPEN
6989 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006990 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006991 | NFS_CAP_STATEID_NFSV41
6992 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006993 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006994 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006995 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04006996 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006997 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6998 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6999 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007000};
7001#endif
7002
Trond Myklebust97dc1352010-06-16 09:52:26 -04007003const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7004 [0] = &nfs_v4_0_minor_ops,
7005#if defined(CONFIG_NFS_V4_1)
7006 [1] = &nfs_v4_1_minor_ops,
7007#endif
7008};
7009
Bryan Schumaker73a79702012-07-16 16:39:12 -04007010const struct inode_operations nfs4_dir_inode_operations = {
7011 .create = nfs_create,
7012 .lookup = nfs_lookup,
7013 .atomic_open = nfs_atomic_open,
7014 .link = nfs_link,
7015 .unlink = nfs_unlink,
7016 .symlink = nfs_symlink,
7017 .mkdir = nfs_mkdir,
7018 .rmdir = nfs_rmdir,
7019 .mknod = nfs_mknod,
7020 .rename = nfs_rename,
7021 .permission = nfs_permission,
7022 .getattr = nfs_getattr,
7023 .setattr = nfs_setattr,
7024 .getxattr = generic_getxattr,
7025 .setxattr = generic_setxattr,
7026 .listxattr = generic_listxattr,
7027 .removexattr = generic_removexattr,
7028};
7029
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007030static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007031 .permission = nfs_permission,
7032 .getattr = nfs_getattr,
7033 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007034 .getxattr = generic_getxattr,
7035 .setxattr = generic_setxattr,
7036 .listxattr = generic_listxattr,
7037 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007038};
7039
David Howells509de812006-08-22 20:06:11 -04007040const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 .version = 4, /* protocol version */
7042 .dentry_ops = &nfs4_dentry_operations,
7043 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007044 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007045 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007047 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007048 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 .getattr = nfs4_proc_getattr,
7050 .setattr = nfs4_proc_setattr,
7051 .lookup = nfs4_proc_lookup,
7052 .access = nfs4_proc_access,
7053 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 .create = nfs4_proc_create,
7055 .remove = nfs4_proc_remove,
7056 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007057 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 .unlink_done = nfs4_proc_unlink_done,
7059 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007060 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007061 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007062 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 .link = nfs4_proc_link,
7064 .symlink = nfs4_proc_symlink,
7065 .mkdir = nfs4_proc_mkdir,
7066 .rmdir = nfs4_proc_remove,
7067 .readdir = nfs4_proc_readdir,
7068 .mknod = nfs4_proc_mknod,
7069 .statfs = nfs4_proc_statfs,
7070 .fsinfo = nfs4_proc_fsinfo,
7071 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007072 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 .decode_dirent = nfs4_decode_dirent,
7074 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007075 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007076 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007077 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007079 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007080 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007081 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007083 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007084 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007086 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007087 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007088 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007089 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007090 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007091 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007092 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007093 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007094 .create_server = nfs4_create_server,
7095 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096};
7097
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007098static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7099 .prefix = XATTR_NAME_NFSV4_ACL,
7100 .list = nfs4_xattr_list_nfs4_acl,
7101 .get = nfs4_xattr_get_nfs4_acl,
7102 .set = nfs4_xattr_set_nfs4_acl,
7103};
7104
7105const struct xattr_handler *nfs4_xattr_handlers[] = {
7106 &nfs4_xattr_nfs4_acl_handler,
7107 NULL
7108};
7109
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110/*
7111 * Local variables:
7112 * c-basic-offset: 8
7113 * End:
7114 */