blob: ea50807b65d9cae29ec4e77c24f0c42f69e2e55e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050096 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400108 case -NFS4ERR_ACCESS:
109 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700114 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400117 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * This is our standard bitmap for GETATTR requests.
122 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400123const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138};
139
Trond Myklebust1549210f2012-06-05 09:16:47 -0400140static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
156};
157
Andy Adamsone23008e2012-10-02 21:07:32 -0400158static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
162};
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
171};
172
Trond Myklebust4ce79712005-06-22 17:16:21 +0000173const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
176 0
177};
178
Fred Isamandae100c2011-07-30 20:52:37 -0400179const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700183 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Manoj Naik830b8e32006-06-09 09:34:25 -0400188const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
205};
206
Al Virobc4785c2006-10-19 23:28:51 -0700207static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct nfs4_readdir_arg *readdir)
209{
Al Viro0dbb4c62006-10-19 23:28:49 -0700210 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000213 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
216 }
217
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
222
223 /*
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
229 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800230 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800258 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Trond Myklebust65de8722008-12-23 15:21:44 -0500261static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
262{
263 int res = 0;
264
265 might_sleep();
266
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100271 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
276}
277
278/* This is the error handling routine for processes that are allowed
279 * to sleep.
280 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800281static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500282{
283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500284 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500285 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 int ret = errorcode;
287
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500292 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400294 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500295 exception->retry = 1;
296 return 0;
297 }
298 if (state == NULL)
299 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500304 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400307 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
308 nfs_remove_bad_delegation(inode);
309 exception->retry = 1;
310 break;
311 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500312 if (state == NULL)
313 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400314 ret = nfs4_schedule_stateid_recovery(server, state);
315 if (ret < 0)
316 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400319 if (state != NULL) {
320 ret = nfs4_schedule_stateid_recovery(server, state);
321 if (ret < 0)
322 break;
323 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500325 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500326 nfs4_schedule_lease_recovery(clp);
327 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500328#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400329 case -NFS4ERR_BADSESSION:
330 case -NFS4ERR_BADSLOT:
331 case -NFS4ERR_BAD_HIGH_SLOT:
332 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
333 case -NFS4ERR_DEADSESSION:
334 case -NFS4ERR_SEQ_FALSE_RETRY:
335 case -NFS4ERR_SEQ_MISORDERED:
336 dprintk("%s ERROR: %d Reset session\n", __func__,
337 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400338 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400339 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500340#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500342 if (exception->timeout > HZ) {
343 /* We have retried a decent amount, time to
344 * fail
345 */
346 ret = -EBUSY;
347 break;
348 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500349 case -NFS4ERR_GRACE:
350 case -NFS4ERR_DELAY:
351 ret = nfs4_delay(server->client, &exception->timeout);
352 if (ret != 0)
353 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400354 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 case -NFS4ERR_OLD_STATEID:
356 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800357 break;
358 case -NFS4ERR_BADOWNER:
359 /* The following works around a Linux server bug! */
360 case -NFS4ERR_BADNAME:
361 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
362 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
363 exception->retry = 1;
364 printk(KERN_WARNING "NFS: v4 server %s "
365 "does not accept raw "
366 "uid/gids. "
367 "Reenabling the idmapper.\n",
368 server->nfs_client->cl_hostname);
369 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500370 }
371 /* We failed to handle the error */
372 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500373wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500374 ret = nfs4_wait_clnt_recover(clp);
375 if (ret == 0)
376 exception->retry = 1;
377 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378}
379
380
Trond Myklebust452e9352010-07-31 14:29:06 -0400381static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_lock(&clp->cl_lock);
384 if (time_before(clp->cl_last_renewal,timestamp))
385 clp->cl_last_renewal = timestamp;
386 spin_unlock(&clp->cl_lock);
387}
388
Trond Myklebust452e9352010-07-31 14:29:06 -0400389static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
390{
391 do_renew_lease(server->nfs_client, timestamp);
392}
393
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400394#if defined(CONFIG_NFS_V4_1)
395
Trond Myklebustd185a332010-06-16 09:52:25 -0400396static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400397{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500398 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400399 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500400 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400401
Benny Halevydfb4f3092010-09-24 09:17:01 -0400402 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400403 /* just wake up the next guy waiting since
404 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500405 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500406 return;
Andy Adamson13615872009-04-01 09:22:17 -0400407 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500408 tbl = res->sr_slot->table;
409 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500410
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500411 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500412 /* Be nice to the server: try to ensure that the last transmitted
413 * value for highest_user_slotid <= target_highest_slotid
414 */
415 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
416 send_new_highest_used_slotid = true;
417
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500418 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
419 send_new_highest_used_slotid = false;
420 goto out_unlock;
421 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500422 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500423
424 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
425 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500426out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400428 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500429 if (send_new_highest_used_slotid)
430 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400431}
432
Trond Myklebust14516c32010-07-31 14:29:06 -0400433static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500435 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500436 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500438 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500439 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Trond Myklebustac20d162012-12-15 15:36:07 -0500448 if (slot->interrupted) {
449 slot->interrupted = 0;
450 interrupted = true;
451 }
452
Andy Adamson691daf32009-12-04 15:55:39 -0500453 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 switch (res->sr_status) {
455 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500457 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500458 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500459 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500460 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500461 if (res->sr_status_flags != 0)
462 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500463 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500465 case 1:
466 /*
467 * sr_status remains 1 if an RPC level error occurred.
468 * The server may or may not have processed the sequence
469 * operation..
470 * Mark the slot as having hosted an interrupted RPC call.
471 */
472 slot->interrupted = 1;
473 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400474 case -NFS4ERR_DELAY:
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
477 * of RFC5661.
478 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500479 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500481 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500482 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500484 case -NFS4ERR_BADSLOT:
485 /*
486 * The slot id we used was probably retired. Try again
487 * using a different slot id.
488 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500489 goto retry_nowait;
490 case -NFS4ERR_SEQ_MISORDERED:
491 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500492 * Was the last operation on this sequence interrupted?
493 * If so, retry after bumping the sequence number.
494 */
495 if (interrupted) {
496 ++slot->seq_nr;
497 goto retry_nowait;
498 }
499 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500500 * Could this slot have been previously retired?
501 * If so, then the server may be expecting seq_nr = 1!
502 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500503 if (slot->seq_nr != 1) {
504 slot->seq_nr = 1;
505 goto retry_nowait;
506 }
507 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500508 case -NFS4ERR_SEQ_FALSE_RETRY:
509 ++slot->seq_nr;
510 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 default:
512 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500513 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514 }
515out:
516 /* The session may be reset by one of the error handlers. */
517 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400518 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500519 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500520retry_nowait:
521 if (rpc_restart_call_prepare(task)) {
522 task->tk_status = 0;
523 ret = 0;
524 }
525 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400526out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400527 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 goto out;
529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
530 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400531}
532
Trond Myklebust14516c32010-07-31 14:29:06 -0400533static int nfs4_sequence_done(struct rpc_task *task,
534 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400535{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500536 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 return 1;
538 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400539}
540
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
543{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500544 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400546 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500547 if (cache_reply)
548 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500549 res->sr_slot = NULL;
550}
551
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400552static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
553{
554 args->sa_privileged = 1;
555}
556
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000557int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct rpc_task *task)
561{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400562 struct nfs4_slot *slot;
563 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564
565 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400567 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400568 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 tbl = &session->fc_slot_table;
571
Trond Myklebust69d206b2012-11-22 13:21:02 -0500572 task->tk_timeout = 0;
573
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400575 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400576 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500577 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400579 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400580 }
581
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500582 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500583 if (IS_ERR(slot)) {
584 /* If out of memory, try again in 1/4 second */
585 if (slot == ERR_PTR(-ENOMEM))
586 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400588 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500592 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500594 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
595 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
Benny Halevydfb4f3092010-09-24 09:17:01 -0400597 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500598 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400599 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 /*
601 * sr_status is only set in decode_sequence, and so will remain
602 * set to 1 if an rpc level failure occurs.
603 */
604 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400605out_success:
606 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400608out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400609 /* Privileged tasks are queued with top priority */
610 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400611 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
612 NULL, RPC_PRIORITY_PRIVILEGED);
613 else
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400615 spin_unlock(&tbl->slot_tbl_lock);
616 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000618EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619
Trond Myklebust035168a2010-06-16 09:52:26 -0400620int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 struct rpc_task *task)
624{
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 int ret = 0;
627
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400628 if (session == NULL) {
629 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400631 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500633 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400634 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638out:
639 dprintk("<-- %s status=%d\n", __func__, ret);
640 return ret;
641}
642
643struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647};
648
649static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400652 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
655
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400656 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Trond Myklebust17280172012-03-11 13:11:00 -0400666static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400675 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
Trond Myklebustdf896452010-06-16 09:52:26 -0400701#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400702static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500703void nfs41_init_sequence(struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res, int cache_reply)
705{
706}
707
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400708static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709{
710}
711
712
Trond Myklebust14516c32010-07-31 14:29:06 -0400713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400715{
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400717}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718#endif /* CONFIG_NFS_V4_1 */
719
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400720static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728}
729
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400730static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400740 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700749 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400750 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000752 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500762 struct nfs4_string owner_name;
763 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100772 int rpc_status;
773 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774};
775
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400776static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
777 int err, struct nfs4_exception *exception)
778{
779 if (err != -EINVAL)
780 return false;
781 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
782 return false;
783 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
784 exception->retry = 1;
785 return true;
786}
787
788static enum open_claim_type4
789nfs4_map_atomic_open_claim(struct nfs_server *server,
790 enum open_claim_type4 claim)
791{
792 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
793 return claim;
794 switch (claim) {
795 default:
796 return claim;
797 case NFS4_OPEN_CLAIM_FH:
798 return NFS4_OPEN_CLAIM_NULL;
799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
800 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
801 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
802 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
803 }
804}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400805
806static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807{
808 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400812 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400820 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400821 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct inode *dir = parent->d_inode;
825 struct nfs_server *server = NFS_SERVER(dir);
826 struct nfs4_opendata *p;
827
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p == NULL)
830 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400831 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 if (p->o_arg.seqid == NULL)
833 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400834 nfs_sb_active(dentry->d_sb);
835 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 p->dir = parent;
837 p->owner = sp;
838 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700841 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
842 * will return permission denied for all bits until close */
843 if (!(flags & O_EXCL)) {
844 /* ask server to check for all possible rights as results
845 * are cached */
846 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
847 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700848 }
David Howells7539bba2006-08-22 20:06:09 -0400849 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400850 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
851 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 p->o_arg.server = server;
854 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400855 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400856 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
857 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400858 case NFS4_OPEN_CLAIM_NULL:
859 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
860 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
861 p->o_arg.fh = NFS_FH(dir);
862 break;
863 case NFS4_OPEN_CLAIM_PREVIOUS:
864 case NFS4_OPEN_CLAIM_FH:
865 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
866 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
867 p->o_arg.fh = NFS_FH(dentry->d_inode);
868 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500869 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500870 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400871
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500874
875 verf[0] = jiffies;
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400883 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 return p;
886err_free:
887 kfree(p);
888err:
889 dput(parent);
890 return NULL;
891}
892
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898
899 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 nfs4_put_state_owner(p->owner);
903 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 dput(p->dentry);
905 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500906 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400907 kfree(p);
908}
909
910static void nfs4_opendata_put(struct nfs4_opendata *p)
911{
912 if (p != NULL)
913 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914}
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
917{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918 int ret;
919
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925{
926 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927
Trond Myklebust536e43d2012-01-17 22:04:26 -0500928 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 goto out;
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 break;
939 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 return ret;
945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500949 if (delegation == NULL)
950 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500955 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400981 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 case FMODE_READ:
984 set_bit(NFS_O_RDONLY_STATE, &state->flags);
985 break;
986 case FMODE_WRITE:
987 set_bit(NFS_O_WRONLY_STATE, &state->flags);
988 break;
989 case FMODE_READ|FMODE_WRITE:
990 set_bit(NFS_O_RDWR_STATE, &state->flags);
991 }
992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400995{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400996 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400999}
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001003 /*
1004 * Protect the call to nfs4_state_set_mode_locked and
1005 * serialise the stateid update
1006 */
1007 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001008 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001009 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001010 set_bit(NFS_DELEGATED_STATE, &state->flags);
1011 }
1012 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_sequnlock(&state->seqlock);
1015 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001017 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001021{
1022 struct nfs_inode *nfsi = NFS_I(state->inode);
1023 struct nfs_delegation *deleg_cur;
1024 int ret = 0;
1025
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001027
1028 rcu_read_lock();
1029 deleg_cur = rcu_dereference(nfsi->delegation);
1030 if (deleg_cur == NULL)
1031 goto no_delegation;
1032
1033 spin_lock(&deleg_cur->lock);
1034 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001035 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
1039 if (delegation == NULL)
1040 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001041 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001042 goto no_delegation_unlock;
1043
Trond Myklebustb7391f42008-12-23 15:21:52 -05001044 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001046 ret = 1;
1047no_delegation_unlock:
1048 spin_unlock(&deleg_cur->lock);
1049no_delegation:
1050 rcu_read_unlock();
1051
1052 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001054 ret = 1;
1055 }
1056
1057 return ret;
1058}
1059
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062{
1063 struct nfs_delegation *delegation;
1064
1065 rcu_read_lock();
1066 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 rcu_read_unlock();
1069 return;
1070 }
1071 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001072 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073}
1074
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076{
1077 struct nfs4_state *state = opendata->state;
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001080 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 nfs4_stateid stateid;
1083 int ret = -EAGAIN;
1084
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 if (can_open_cached(state, fmode, open_mode)) {
1089 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 goto out_return_state;
1092 }
1093 spin_unlock(&state->owner->so_lock);
1094 }
Trond Myklebust34310432008-12-23 15:21:38 -05001095 rcu_read_lock();
1096 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001097 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001100 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001102 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001104 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 if (ret != 0)
1106 goto out;
1107 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001108
1109 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113out:
1114 return ERR_PTR(ret);
1115out_return_state:
1116 atomic_inc(&state->count);
1117 return state;
1118}
1119
Andy Adamsone23008e2012-10-02 21:07:32 -04001120static void
1121nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1122{
1123 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1124 struct nfs_delegation *delegation;
1125 int delegation_flags = 0;
1126
1127 rcu_read_lock();
1128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1129 if (delegation)
1130 delegation_flags = delegation->flags;
1131 rcu_read_unlock();
1132 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1133 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1134 "returning a delegation for "
1135 "OPEN(CLAIM_DELEGATE_CUR)\n",
1136 clp->cl_hostname);
1137 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1138 nfs_inode_set_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 else
1142 nfs_inode_reclaim_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145}
1146
1147/*
1148 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1149 * and update the nfs4_state.
1150 */
1151static struct nfs4_state *
1152_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1153{
1154 struct inode *inode = data->state->inode;
1155 struct nfs4_state *state = data->state;
1156 int ret;
1157
1158 if (!data->rpc_done) {
1159 ret = data->rpc_status;
1160 goto err;
1161 }
1162
1163 ret = -ESTALE;
1164 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1165 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1166 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1167 goto err;
1168
1169 ret = -ENOMEM;
1170 state = nfs4_get_open_state(inode, data->owner);
1171 if (state == NULL)
1172 goto err;
1173
1174 ret = nfs_refresh_inode(inode, &data->f_attr);
1175 if (ret)
1176 goto err;
1177
1178 if (data->o_res.delegation_type != 0)
1179 nfs4_opendata_check_deleg(data, state);
1180 update_open_stateid(state, &data->o_res.stateid, NULL,
1181 data->o_arg.fmode);
1182
1183 return state;
1184err:
1185 return ERR_PTR(ret);
1186
1187}
1188
1189static struct nfs4_state *
1190_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001191{
1192 struct inode *inode;
1193 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001194 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001195
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001197 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001198 goto out;
1199 }
1200
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001202 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001203 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001205 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001206 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001207 goto err;
1208 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209 state = nfs4_get_open_state(inode, data->owner);
1210 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001212 if (data->o_res.delegation_type != 0)
1213 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001214 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001217out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001218 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220err_put_inode:
1221 iput(inode);
1222err:
1223 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001224}
1225
Andy Adamsone23008e2012-10-02 21:07:32 -04001226static struct nfs4_state *
1227nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1228{
1229 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1230 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1231 return _nfs4_opendata_to_nfs4_state(data);
1232}
1233
Trond Myklebust864472e2006-01-03 09:55:15 +01001234static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1235{
1236 struct nfs_inode *nfsi = NFS_I(state->inode);
1237 struct nfs_open_context *ctx;
1238
1239 spin_lock(&state->inode->i_lock);
1240 list_for_each_entry(ctx, &nfsi->open_files, list) {
1241 if (ctx->state != state)
1242 continue;
1243 get_nfs_open_context(ctx);
1244 spin_unlock(&state->inode->i_lock);
1245 return ctx;
1246 }
1247 spin_unlock(&state->inode->i_lock);
1248 return ERR_PTR(-ENOENT);
1249}
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1252 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253{
1254 struct nfs4_opendata *opendata;
1255
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001256 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1257 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001258 if (opendata == NULL)
1259 return ERR_PTR(-ENOMEM);
1260 opendata->state = state;
1261 atomic_inc(&state->count);
1262 return opendata;
1263}
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001266{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 int ret;
1269
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 opendata->o_arg.open_flags = 0;
1271 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001272 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1273 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1274 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001275 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 if (ret != 0)
1277 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001278 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001279 if (IS_ERR(newstate))
1280 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001281 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001282 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 return 0;
1284}
1285
1286static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1287{
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 int ret;
1290
1291 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001293 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 smp_rmb();
1295 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001296 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001297 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 if (ret != 0)
1299 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001300 if (newstate != state)
1301 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 }
1303 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001304 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001305 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 if (ret != 0)
1307 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 if (newstate != state)
1309 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 }
1311 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001312 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001313 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 if (ret != 0)
1315 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 if (newstate != state)
1317 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001318 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001319 /*
1320 * We may have performed cached opens for all three recoveries.
1321 * Check if we need to update the current stateid.
1322 */
1323 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001324 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001325 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001326 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001327 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001328 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001329 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001330 return 0;
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * OPEN_RECLAIM:
1335 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001337static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001339 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001340 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int status;
1343
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001344 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1345 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001346 if (IS_ERR(opendata))
1347 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001348 rcu_read_lock();
1349 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001350 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001351 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001352 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 opendata->o_arg.u.delegation_type = delegation_type;
1354 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001355 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return status;
1357}
1358
Trond Myklebust539cd032007-06-05 11:46:42 -04001359static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct nfs_server *server = NFS_SERVER(state->inode);
1362 struct nfs4_exception exception = { };
1363 int err;
1364 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001365 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001366 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1367 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001368 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001369 break;
1370 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } while (exception.retry);
1372 return err;
1373}
1374
Trond Myklebust864472e2006-01-03 09:55:15 +01001375static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1376{
1377 struct nfs_open_context *ctx;
1378 int ret;
1379
1380 ctx = nfs4_state_find_open_context(state);
1381 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001382 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001383 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001384 put_nfs_open_context(ctx);
1385 return ret;
1386}
1387
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001388static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001390 switch (err) {
1391 default:
1392 printk(KERN_ERR "NFS: %s: unhandled error "
1393 "%d.\n", __func__, err);
1394 case 0:
1395 case -ENOENT:
1396 case -ESTALE:
1397 break;
1398 case -NFS4ERR_BADSESSION:
1399 case -NFS4ERR_BADSLOT:
1400 case -NFS4ERR_BAD_HIGH_SLOT:
1401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1402 case -NFS4ERR_DEADSESSION:
1403 set_bit(NFS_DELEGATED_STATE, &state->flags);
1404 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1405 return -EAGAIN;
1406 case -NFS4ERR_STALE_CLIENTID:
1407 case -NFS4ERR_STALE_STATEID:
1408 set_bit(NFS_DELEGATED_STATE, &state->flags);
1409 case -NFS4ERR_EXPIRED:
1410 /* Don't recall a delegation if it was lost */
1411 nfs4_schedule_lease_recovery(server->nfs_client);
1412 return -EAGAIN;
1413 case -NFS4ERR_DELEG_REVOKED:
1414 case -NFS4ERR_ADMIN_REVOKED:
1415 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001416 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001417 nfs_inode_find_state_and_recover(state->inode,
1418 stateid);
1419 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001420 return 0;
1421 case -NFS4ERR_DELAY:
1422 case -NFS4ERR_GRACE:
1423 set_bit(NFS_DELEGATED_STATE, &state->flags);
1424 ssleep(1);
1425 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001426 case -ENOMEM:
1427 case -NFS4ERR_DENIED:
1428 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1429 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return err;
1432}
1433
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001434int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1435{
1436 struct nfs_server *server = NFS_SERVER(state->inode);
1437 struct nfs4_opendata *opendata;
1438 int err;
1439
1440 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1441 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1442 if (IS_ERR(opendata))
1443 return PTR_ERR(opendata);
1444 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1445 err = nfs4_open_recover(opendata, state);
1446 nfs4_opendata_put(opendata);
1447 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1448}
1449
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001450static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1451{
1452 struct nfs4_opendata *data = calldata;
1453
1454 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001455 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001457 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001459 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001460 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001461}
1462
1463static void nfs4_open_confirm_release(void *calldata)
1464{
1465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state *state = NULL;
1467
1468 /* If this request hasn't been cancelled, do nothing */
1469 if (data->cancelled == 0)
1470 goto out_free;
1471 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001472 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001474 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001475 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001476 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001478 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001479}
1480
1481static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001482 .rpc_call_done = nfs4_open_confirm_done,
1483 .rpc_release = nfs4_open_confirm_release,
1484};
1485
1486/*
1487 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1488 */
1489static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1490{
1491 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1492 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001493 struct rpc_message msg = {
1494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1495 .rpc_argp = &data->c_arg,
1496 .rpc_resp = &data->c_res,
1497 .rpc_cred = data->owner->so_cred,
1498 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 struct rpc_task_setup task_setup_data = {
1500 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001502 .callback_ops = &nfs4_open_confirm_ops,
1503 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001504 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001505 .flags = RPC_TASK_ASYNC,
1506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int status;
1508
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001509 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001510 data->rpc_done = 0;
1511 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001512 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001513 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001514 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001515 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001516 status = nfs4_wait_for_completion_rpc_task(task);
1517 if (status != 0) {
1518 data->cancelled = 1;
1519 smp_wmb();
1520 } else
1521 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001522 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return status;
1524}
1525
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528 struct nfs4_opendata *data = calldata;
1529 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001530 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001531
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001533 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001534 /*
1535 * Check if we still need to send an OPEN call, or if we can use
1536 * a delegation instead.
1537 */
1538 if (data->state != NULL) {
1539 struct nfs_delegation *delegation;
1540
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001542 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_lock();
1544 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001545 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001546 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001547 can_open_delegated(delegation, data->o_arg.fmode))
1548 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001549 rcu_read_unlock();
1550 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001551 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001552 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001553 switch (data->o_arg.claim) {
1554 case NFS4_OPEN_CLAIM_PREVIOUS:
1555 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1556 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001557 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001558 case NFS4_OPEN_CLAIM_FH:
1559 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001560 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1561 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001562 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001563 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001564 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001565 &data->o_res.seq_res,
1566 task) != 0)
1567 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001568
1569 /* Set the create mode (note dependency on the session type) */
1570 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1571 if (data->o_arg.open_flags & O_EXCL) {
1572 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1573 if (nfs4_has_persistent_session(clp))
1574 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1575 else if (clp->cl_mvops->minor_version > 0)
1576 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1577 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001578 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001579unlock_no_action:
1580 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001581out_no_action:
1582 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001583out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001584 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001585}
1586
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587static void nfs4_open_done(struct rpc_task *task, void *calldata)
1588{
1589 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001592
Trond Myklebust14516c32010-07-31 14:29:06 -04001593 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1594 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001597 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1598 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001599 case S_IFREG:
1600 break;
1601 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001603 break;
1604 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001606 break;
1607 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001609 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001610 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001611 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001612 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1613 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001614 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001615 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001617
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001618static void nfs4_open_release(void *calldata)
1619{
1620 struct nfs4_opendata *data = calldata;
1621 struct nfs4_state *state = NULL;
1622
1623 /* If this request hasn't been cancelled, do nothing */
1624 if (data->cancelled == 0)
1625 goto out_free;
1626 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001627 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628 goto out_free;
1629 /* In case we need an open_confirm, no cleanup! */
1630 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1631 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001633 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001634 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001635out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001636 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637}
1638
1639static const struct rpc_call_ops nfs4_open_ops = {
1640 .rpc_call_prepare = nfs4_open_prepare,
1641 .rpc_call_done = nfs4_open_done,
1642 .rpc_release = nfs4_open_release,
1643};
1644
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001645static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646{
1647 struct inode *dir = data->dir->d_inode;
1648 struct nfs_server *server = NFS_SERVER(dir);
1649 struct nfs_openargs *o_arg = &data->o_arg;
1650 struct nfs_openres *o_res = &data->o_res;
1651 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001652 struct rpc_message msg = {
1653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1654 .rpc_argp = o_arg,
1655 .rpc_resp = o_res,
1656 .rpc_cred = data->owner->so_cred,
1657 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001658 struct rpc_task_setup task_setup_data = {
1659 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001660 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001661 .callback_ops = &nfs4_open_ops,
1662 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001663 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001664 .flags = RPC_TASK_ASYNC,
1665 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 int status;
1667
Trond Myklebust9d12b212012-01-17 22:04:25 -05001668 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001669 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001670 data->rpc_done = 0;
1671 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001672 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001673 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001674 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001675 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001676 if (IS_ERR(task))
1677 return PTR_ERR(task);
1678 status = nfs4_wait_for_completion_rpc_task(task);
1679 if (status != 0) {
1680 data->cancelled = 1;
1681 smp_wmb();
1682 } else
1683 status = data->rpc_status;
1684 rpc_put_task(task);
1685
1686 return status;
1687}
1688
1689static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1690{
1691 struct inode *dir = data->dir->d_inode;
1692 struct nfs_openres *o_res = &data->o_res;
1693 int status;
1694
1695 status = nfs4_run_open_task(data, 1);
1696 if (status != 0 || !data->rpc_done)
1697 return status;
1698
Trond Myklebust6926afd2012-01-07 13:22:46 -05001699 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1700
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001701 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1702 status = _nfs4_proc_open_confirm(data);
1703 if (status != 0)
1704 return status;
1705 }
1706
1707 return status;
1708}
1709
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001710static int nfs4_opendata_access(struct rpc_cred *cred,
1711 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001712 struct nfs4_state *state, fmode_t fmode,
1713 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001714{
1715 struct nfs_access_entry cache;
1716 u32 mask;
1717
1718 /* access call failed or for some reason the server doesn't
1719 * support any access modes -- defer access call until later */
1720 if (opendata->o_res.access_supported == 0)
1721 return 0;
1722
1723 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001724 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001725 * write mode bits, but POSIX allows the creating process to write.
1726 * use openflags to check for exec, because fmode won't
1727 * always have FMODE_EXEC set when file open for exec. */
1728 if (openflags & __FMODE_EXEC) {
1729 /* ONLY check for exec rights */
1730 mask = MAY_EXEC;
1731 } else if (fmode & FMODE_READ)
1732 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001733
1734 cache.cred = cred;
1735 cache.jiffies = jiffies;
1736 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1737 nfs_access_add_cache(state->inode, &cache);
1738
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001739 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001740 return 0;
1741
1742 /* even though OPEN succeeded, access is denied. Close the file */
1743 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001744 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001745}
1746
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001747/*
1748 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1749 */
1750static int _nfs4_proc_open(struct nfs4_opendata *data)
1751{
1752 struct inode *dir = data->dir->d_inode;
1753 struct nfs_server *server = NFS_SERVER(dir);
1754 struct nfs_openargs *o_arg = &data->o_arg;
1755 struct nfs_openres *o_res = &data->o_res;
1756 int status;
1757
1758 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001759 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001761 if (status != 0) {
1762 if (status == -NFS4ERR_BADNAME &&
1763 !(o_arg->open_flags & O_CREAT))
1764 return -ENOENT;
1765 return status;
1766 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767
Trond Myklebust6926afd2012-01-07 13:22:46 -05001768 nfs_fattr_map_and_free_names(server, &data->f_attr);
1769
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001770 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001771 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001772 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1773 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001775 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001780 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Andy Adamsond83217c2011-03-01 01:34:17 +00001784static int nfs4_recover_expired_lease(struct nfs_server *server)
1785{
1786 return nfs4_client_recover_expired_lease(server->nfs_client);
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/*
1790 * OPEN_EXPIRED:
1791 * reclaim state on the server after a network partition.
1792 * Assumes caller holds the appropriate lock
1793 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001794static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001796 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001797 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001799 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001800 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001801 if (IS_ERR(opendata))
1802 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001803 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001804 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001805 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001806 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001807 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001810static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001811{
Trond Myklebust539cd032007-06-05 11:46:42 -04001812 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001813 struct nfs4_exception exception = { };
1814 int err;
1815
1816 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001817 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001818 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1819 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001820 switch (err) {
1821 default:
1822 goto out;
1823 case -NFS4ERR_GRACE:
1824 case -NFS4ERR_DELAY:
1825 nfs4_handle_exception(server, err, &exception);
1826 err = 0;
1827 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001828 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001829out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001830 return err;
1831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001836 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 ctx = nfs4_state_find_open_context(state);
1839 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001840 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001841 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001842 put_nfs_open_context(ctx);
1843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001846#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001847static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001848{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001849 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001850 nfs4_stateid *stateid = &state->stateid;
1851 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001852
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001853 /* If a state reset has been done, test_stateid is unneeded */
1854 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1855 return;
1856
1857 status = nfs41_test_stateid(server, stateid);
1858 if (status != NFS_OK) {
1859 /* Free the stateid unless the server explicitly
1860 * informs us the stateid is unrecognized. */
1861 if (status != -NFS4ERR_BAD_STATEID)
1862 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001863 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001864
Bryan Schumaker69388672012-09-26 15:25:52 -04001865 write_seqlock(&state->seqlock);
1866 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1867 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001868 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1869 }
1870}
1871
1872/**
1873 * nfs41_check_open_stateid - possibly free an open stateid
1874 *
1875 * @state: NFSv4 state for an inode
1876 *
1877 * Returns NFS_OK if recovery for this stateid is now finished.
1878 * Otherwise a negative NFS4ERR value is returned.
1879 */
1880static int nfs41_check_open_stateid(struct nfs4_state *state)
1881{
1882 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001883 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001884 int status;
1885
1886 /* If a state reset has been done, test_stateid is unneeded */
1887 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1888 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1889 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1890 return -NFS4ERR_BAD_STATEID;
1891
1892 status = nfs41_test_stateid(server, stateid);
1893 if (status != NFS_OK) {
1894 /* Free the stateid unless the server explicitly
1895 * informs us the stateid is unrecognized. */
1896 if (status != -NFS4ERR_BAD_STATEID)
1897 nfs41_free_stateid(server, stateid);
1898
1899 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1900 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1901 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001902 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001903 }
1904 return status;
1905}
1906
1907static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1908{
Chuck Levereb64cf92012-07-11 16:30:05 -04001909 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001910
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001911 nfs41_clear_delegation_stateid(state);
1912 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001913 if (status != NFS_OK)
1914 status = nfs4_open_expired(sp, state);
1915 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001916}
1917#endif
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001920 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1921 * fields corresponding to attributes that were used to store the verifier.
1922 * Make sure we clobber those fields in the later setattr call
1923 */
1924static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1925{
1926 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1927 !(sattr->ia_valid & ATTR_ATIME_SET))
1928 sattr->ia_valid |= ATTR_ATIME;
1929
1930 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1931 !(sattr->ia_valid & ATTR_MTIME_SET))
1932 sattr->ia_valid |= ATTR_MTIME;
1933}
1934
Trond Myklebustc21443c2013-02-07 14:26:21 -05001935static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1936 fmode_t fmode,
1937 int flags,
1938 struct nfs4_state **res)
1939{
1940 struct nfs4_state_owner *sp = opendata->owner;
1941 struct nfs_server *server = sp->so_server;
1942 struct nfs4_state *state;
1943 unsigned int seq;
1944 int ret;
1945
1946 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1947
1948 ret = _nfs4_proc_open(opendata);
1949 if (ret != 0)
1950 goto out;
1951
1952 state = nfs4_opendata_to_nfs4_state(opendata);
1953 ret = PTR_ERR(state);
1954 if (IS_ERR(state))
1955 goto out;
1956 if (server->caps & NFS_CAP_POSIX_LOCK)
1957 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1958
1959 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1960 if (ret != 0)
1961 goto out;
1962
Trond Myklebust91876b12013-03-28 14:01:33 -04001963 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001964 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001965 *res = state;
1966out:
1967 return ret;
1968}
1969
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001970/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001971 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 */
Andy Adamson82be4172012-05-23 05:02:35 -04001973static int _nfs4_do_open(struct inode *dir,
1974 struct dentry *dentry,
1975 fmode_t fmode,
1976 int flags,
1977 struct iattr *sattr,
1978 struct rpc_cred *cred,
1979 struct nfs4_state **res,
1980 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct nfs4_state_owner *sp;
1983 struct nfs4_state *state = NULL;
1984 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001985 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001986 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001987 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001991 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1992 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1994 goto out_err;
1995 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001996 status = nfs4_recover_expired_lease(server);
1997 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001998 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001999 if (dentry->d_inode != NULL)
2000 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002001 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 if (dentry->d_inode)
2003 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002004 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002005 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002006 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002007 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Andy Adamson82be4172012-05-23 05:02:35 -04002009 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2010 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2011 if (!opendata->f_attr.mdsthreshold)
2012 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002013 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002014 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002015 if (dentry->d_inode != NULL)
2016 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002017
Trond Myklebustc21443c2013-02-07 14:26:21 -05002018 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002019 if (status != 0)
2020 goto err_opendata_put;
2021
Trond Myklebust549b19c2013-04-16 18:42:34 -04002022 if ((opendata->o_arg.open_flags & O_EXCL) &&
2023 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002024 nfs4_exclusive_attrset(opendata, sattr);
2025
2026 nfs_fattr_init(opendata->o_res.f_attr);
2027 status = nfs4_do_setattr(state->inode, cred,
2028 opendata->o_res.f_attr, sattr,
2029 state);
2030 if (status == 0)
2031 nfs_setattr_update_inode(state->inode, sattr);
2032 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2033 }
Andy Adamson82be4172012-05-23 05:02:35 -04002034
2035 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2036 *ctx_th = opendata->f_attr.mdsthreshold;
2037 else
2038 kfree(opendata->f_attr.mdsthreshold);
2039 opendata->f_attr.mdsthreshold = NULL;
2040
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002041 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *res = state;
2044 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002045err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002046 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002047 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002048err_put_state_owner:
2049 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 *res = NULL;
2052 return status;
2053}
2054
2055
Andy Adamson82be4172012-05-23 05:02:35 -04002056static struct nfs4_state *nfs4_do_open(struct inode *dir,
2057 struct dentry *dentry,
2058 fmode_t fmode,
2059 int flags,
2060 struct iattr *sattr,
2061 struct rpc_cred *cred,
2062 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002064 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct nfs4_exception exception = { };
2066 struct nfs4_state *res;
2067 int status;
2068
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002069 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002071 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2072 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (status == 0)
2074 break;
2075 /* NOTE: BAD_SEQID means the server and client disagree about the
2076 * book-keeping w.r.t. state-changing operations
2077 * (OPEN/CLOSE/LOCK/LOCKU...)
2078 * It is actually a sign of a bug on the client or on the server.
2079 *
2080 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002081 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * have unhashed the old state_owner for us, and that we can
2083 * therefore safely retry using a new one. We should still warn
2084 * the user though...
2085 */
2086 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002087 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002088 " returned a bad sequence-id error!\n",
2089 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 exception.retry = 1;
2091 continue;
2092 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002093 /*
2094 * BAD_STATEID on OPEN means that the server cancelled our
2095 * state before it received the OPEN_CONFIRM.
2096 * Recover by retrying the request as per the discussion
2097 * on Page 181 of RFC3530.
2098 */
2099 if (status == -NFS4ERR_BAD_STATEID) {
2100 exception.retry = 1;
2101 continue;
2102 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002103 if (status == -EAGAIN) {
2104 /* We must have found a delegation */
2105 exception.retry = 1;
2106 continue;
2107 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002108 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2109 continue;
2110 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 status, &exception));
2112 } while (exception.retry);
2113 return res;
2114}
2115
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002116static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2117 struct nfs_fattr *fattr, struct iattr *sattr,
2118 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002120 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002122 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 .iap = sattr,
2124 .server = server,
2125 .bitmask = server->attr_bitmask,
2126 };
2127 struct nfs_setattrres res = {
2128 .fattr = fattr,
2129 .server = server,
2130 };
2131 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2133 .rpc_argp = &arg,
2134 .rpc_resp = &res,
2135 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002137 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002138 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Trond Myklebust0e574af2005-10-27 22:12:38 -04002140 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Trond Myklebust5d422302013-03-14 16:57:48 -04002142 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002143 struct nfs_lockowner lockowner = {
2144 .l_owner = current->files,
2145 .l_pid = current->tgid,
2146 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002147 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002148 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002149 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2150 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002151 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002152 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002153 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Bryan Schumaker7c513052011-03-24 17:12:24 +00002155 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002156 if (status == 0 && state != NULL)
2157 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002158 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002161static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2162 struct nfs_fattr *fattr, struct iattr *sattr,
2163 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002165 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002166 struct nfs4_exception exception = {
2167 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002168 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 int err;
2171 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002172 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2173 switch (err) {
2174 case -NFS4ERR_OPENMODE:
2175 if (state && !(state->state & FMODE_WRITE)) {
2176 err = -EBADF;
2177 if (sattr->ia_valid & ATTR_OPEN)
2178 err = -EACCES;
2179 goto out;
2180 }
2181 }
2182 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002184out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return err;
2186}
2187
2188struct nfs4_closedata {
2189 struct inode *inode;
2190 struct nfs4_state *state;
2191 struct nfs_closeargs arg;
2192 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002193 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002194 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002195 bool roc;
2196 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197};
2198
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002199static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002200{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002201 struct nfs4_closedata *calldata = data;
2202 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002203 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002204
Fred Isamanf7e89172011-01-06 11:36:32 +00002205 if (calldata->roc)
2206 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002207 nfs4_put_open_state(calldata->state);
2208 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002209 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002210 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002211 kfree(calldata);
2212}
2213
Trond Myklebust88069f72009-12-08 08:33:16 -05002214static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2215 fmode_t fmode)
2216{
2217 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002218 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002219 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2220 case FMODE_WRITE:
2221 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2222 break;
2223 case FMODE_READ:
2224 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2225 break;
2226 case 0:
2227 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2228 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2229 clear_bit(NFS_OPEN_STATE, &state->flags);
2230 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002231 spin_unlock(&state->owner->so_lock);
2232}
2233
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002234static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002236 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 struct nfs_server *server = NFS_SERVER(calldata->inode);
2239
Chuck Levera3ca5652012-03-01 17:00:40 -05002240 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002241 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2242 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /* hmm. we are done with the inode, and in the process of freeing
2244 * the state_owner. we keep this around to process errors
2245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 switch (task->tk_status) {
2247 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002248 if (calldata->roc)
2249 pnfs_roc_set_barrier(state->inode,
2250 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002251 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002252 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002253 nfs4_close_clear_stateid_flags(state,
2254 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
2256 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002257 case -NFS4ERR_OLD_STATEID:
2258 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002260 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002263 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2264 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002266 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002267 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002268 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002271static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002273 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002274 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002275 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002276 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002277
Chuck Levera3ca5652012-03-01 17:00:40 -05002278 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002279 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002280 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002281
Trond Myklebust88069f72009-12-08 08:33:16 -05002282 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2283 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002284 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002285 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002286 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002287 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2289 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2290 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002291 }
2292 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002293 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2294 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2295 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002296 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002297 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002298 if (!nfs4_valid_open_stateid(state))
2299 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002300 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002301
2302 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002303 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002304 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002305 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002306
Fred Isamanf7e89172011-01-06 11:36:32 +00002307 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002308 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002309 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002310 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2311 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002312 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002313 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002314 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002315
Trond Myklebust516a6af2005-10-27 22:12:41 -04002316 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002317 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002318 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002319 &calldata->arg.seq_args,
2320 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002321 task) != 0)
2322 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002323 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002324 return;
2325out_no_action:
2326 task->tk_action = NULL;
2327out_wait:
2328 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002331static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002332 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002333 .rpc_call_done = nfs4_close_done,
2334 .rpc_release = nfs4_free_closedata,
2335};
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337/*
2338 * It is possible for data to be read/written from a mem-mapped file
2339 * after the sys_close call (which hits the vfs layer as a flush).
2340 * This means that we can't safely call nfsv4 close on a file until
2341 * the inode is cleared. This in turn means that we are not good
2342 * NFSv4 citizens - we do not indicate to the server to update the file's
2343 * share state even when we are done with one of the three share
2344 * stateid's in the inode.
2345 *
2346 * NOTE: Caller must be holding the sp->so_owner semaphore!
2347 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002348int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002350 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002352 struct nfs4_state_owner *sp = state->owner;
2353 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002354 struct rpc_message msg = {
2355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2356 .rpc_cred = state->owner->so_cred,
2357 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002358 struct rpc_task_setup task_setup_data = {
2359 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002360 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002361 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002362 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002363 .flags = RPC_TASK_ASYNC,
2364 };
Trond Myklebust95121352005-10-18 14:20:12 -07002365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002367 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002369 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002370 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002371 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002373 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002374 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002376 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002377 if (calldata->arg.seqid == NULL)
2378 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002379 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002380 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002381 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002382 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002383 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002384 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002385 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002386
Trond Myklebust1174dd12010-12-21 10:52:24 -05002387 msg.rpc_argp = &calldata->arg;
2388 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002389 task_setup_data.callback_data = calldata;
2390 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002391 if (IS_ERR(task))
2392 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002393 status = 0;
2394 if (wait)
2395 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002396 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002397 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002398out_free_calldata:
2399 kfree(calldata);
2400out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002401 nfs4_put_open_state(state);
2402 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Trond Myklebust2b484292010-09-17 10:56:51 -04002406static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002407nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct nfs4_state *state;
2410
Trond Myklebust565277f2007-10-15 18:17:53 -04002411 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002412 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2413 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002414 if (IS_ERR(state))
2415 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002416 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002417 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Trond Myklebust1185a552009-12-03 15:54:02 -05002420static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002421{
2422 if (ctx->state == NULL)
2423 return;
2424 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002425 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002426 else
Al Viro643168c2011-06-22 18:20:23 -04002427 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2431{
Benny Halevy43652ad2009-04-01 09:21:54 -04002432 struct nfs4_server_caps_arg args = {
2433 .fhandle = fhandle,
2434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct nfs4_server_caps_res res = {};
2436 struct rpc_message msg = {
2437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002438 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 .rpc_resp = &res,
2440 };
2441 int status;
2442
Bryan Schumaker7c513052011-03-24 17:12:24 +00002443 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (status == 0) {
2445 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002446 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2447 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2448 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2449 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2450 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2452 server->caps |= NFS_CAP_ACLS;
2453 if (res.has_links != 0)
2454 server->caps |= NFS_CAP_HARDLINKS;
2455 if (res.has_symlinks != 0)
2456 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002457 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2458 server->caps |= NFS_CAP_FILEID;
2459 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2460 server->caps |= NFS_CAP_MODE;
2461 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2462 server->caps |= NFS_CAP_NLINK;
2463 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2464 server->caps |= NFS_CAP_OWNER;
2465 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2466 server->caps |= NFS_CAP_OWNER_GROUP;
2467 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2468 server->caps |= NFS_CAP_ATIME;
2469 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2470 server->caps |= NFS_CAP_CTIME;
2471 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2472 server->caps |= NFS_CAP_MTIME;
2473
Trond Myklebusta65318b2009-03-11 14:10:28 -04002474 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2475 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2476 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002478 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return status;
2482}
2483
Trond Myklebust55a97592006-06-09 09:34:19 -04002484int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 struct nfs4_exception exception = { };
2487 int err;
2488 do {
2489 err = nfs4_handle_exception(server,
2490 _nfs4_server_capabilities(server, fhandle),
2491 &exception);
2492 } while (exception.retry);
2493 return err;
2494}
2495
2496static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2497 struct nfs_fsinfo *info)
2498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct nfs4_lookup_root_arg args = {
2500 .bitmask = nfs4_fattr_bitmap,
2501 };
2502 struct nfs4_lookup_res res = {
2503 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002504 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 .fh = fhandle,
2506 };
2507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2509 .rpc_argp = &args,
2510 .rpc_resp = &res,
2511 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002512
Trond Myklebust0e574af2005-10-27 22:12:38 -04002513 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2518 struct nfs_fsinfo *info)
2519{
2520 struct nfs4_exception exception = { };
2521 int err;
2522 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002523 err = _nfs4_lookup_root(server, fhandle, info);
2524 switch (err) {
2525 case 0:
2526 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002527 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002528 default:
2529 err = nfs4_handle_exception(server, err, &exception);
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return err;
2534}
2535
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002536static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2537 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2538{
2539 struct rpc_auth *auth;
2540 int ret;
2541
2542 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002543 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002544 ret = -EIO;
2545 goto out;
2546 }
2547 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002548out:
2549 return ret;
2550}
2551
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002552static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002553 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002555 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002556 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002558 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002559 if (len < 0)
2560 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002561
2562 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002563 /* AUTH_UNIX is the default flavor if none was specified,
2564 * thus has already been tried. */
2565 if (flav_array[i] == RPC_AUTH_UNIX)
2566 continue;
2567
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002568 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002569 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002570 continue;
2571 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002572 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002573 /*
2574 * -EACCESS could mean that the user doesn't have correct permissions
2575 * to access the mount. It could also mean that we tried to mount
2576 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2577 * existing mount programs don't handle -EACCES very well so it should
2578 * be mapped to -EPERM instead.
2579 */
2580 if (status == -EACCES)
2581 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002582 return status;
2583}
2584
2585/*
2586 * get the file handle for the "/" directory on the server
2587 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002588int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2589 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002590{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002591 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002592 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002593 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2594 /*
2595 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2596 * by nfs4_map_errors() as this function exits.
2597 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002598 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (status == 0)
2600 status = nfs4_server_capabilities(server, fhandle);
2601 if (status == 0)
2602 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002603 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Bryan Schumakerbae36242012-05-10 15:07:31 -04002606static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2607 struct nfs_fsinfo *info)
2608{
2609 int error;
2610 struct nfs_fattr *fattr = info->fattr;
2611
2612 error = nfs4_server_capabilities(server, mntfh);
2613 if (error < 0) {
2614 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2615 return error;
2616 }
2617
2618 error = nfs4_proc_getattr(server, mntfh, fattr);
2619 if (error < 0) {
2620 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2621 return error;
2622 }
2623
2624 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2625 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2626 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2627
2628 return error;
2629}
2630
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631/*
2632 * Get locations and (maybe) other attributes of a referral.
2633 * Note that we'll actually follow the referral later when
2634 * we detect fsid mismatch in inode revalidation
2635 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002636static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2637 const struct qstr *name, struct nfs_fattr *fattr,
2638 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002639{
2640 int status = -ENOMEM;
2641 struct page *page = NULL;
2642 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643
2644 page = alloc_page(GFP_KERNEL);
2645 if (page == NULL)
2646 goto out;
2647 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2648 if (locations == NULL)
2649 goto out;
2650
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002651 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002652 if (status != 0)
2653 goto out;
2654 /* Make sure server returned a different fsid for the referral */
2655 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002656 dprintk("%s: server did not return a different fsid for"
2657 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002658 status = -EIO;
2659 goto out;
2660 }
Andy Adamson533eb462011-06-13 18:25:56 -04002661 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2662 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002663
Andy Adamson533eb462011-06-13 18:25:56 -04002664 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002665 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002666 memset(fhandle, 0, sizeof(struct nfs_fh));
2667out:
2668 if (page)
2669 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002670 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002671 return status;
2672}
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675{
2676 struct nfs4_getattr_arg args = {
2677 .fh = fhandle,
2678 .bitmask = server->attr_bitmask,
2679 };
2680 struct nfs4_getattr_res res = {
2681 .fattr = fattr,
2682 .server = server,
2683 };
2684 struct rpc_message msg = {
2685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2686 .rpc_argp = &args,
2687 .rpc_resp = &res,
2688 };
2689
Trond Myklebust0e574af2005-10-27 22:12:38 -04002690 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002691 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
2694static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2695{
2696 struct nfs4_exception exception = { };
2697 int err;
2698 do {
2699 err = nfs4_handle_exception(server,
2700 _nfs4_proc_getattr(server, fhandle, fattr),
2701 &exception);
2702 } while (exception.retry);
2703 return err;
2704}
2705
2706/*
2707 * The file is not closed if it is opened due to the a request to change
2708 * the size of the file. The open call will not be needed once the
2709 * VFS layer lookup-intents are implemented.
2710 *
2711 * Close is called when the inode is destroyed.
2712 * If we haven't opened the file for O_WRONLY, we
2713 * need to in the size_change case to obtain a stateid.
2714 *
2715 * Got race?
2716 * Because OPEN is always done by name in nfsv4, it is
2717 * possible that we opened a different file by the same
2718 * name. We can recognize this race condition, but we
2719 * can't do anything about it besides returning an error.
2720 *
2721 * This will be fixed with VFS changes (lookup-intent).
2722 */
2723static int
2724nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2725 struct iattr *sattr)
2726{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002727 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002728 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002729 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 int status;
2731
Benny Halevy8a1636c2010-07-14 15:43:57 -04002732 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002733 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002734
Trond Myklebust0e574af2005-10-27 22:12:38 -04002735 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Andy Adamson26699402012-05-30 16:12:24 -04002737 /* Deal with open(O_TRUNC) */
2738 if (sattr->ia_valid & ATTR_OPEN)
2739 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2740
2741 /* Optimization: if the end result is no change, don't RPC */
2742 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2743 return 0;
2744
Trond Myklebustd5308382005-11-04 15:33:38 -05002745 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002746 if (sattr->ia_valid & ATTR_FILE) {
2747 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002748
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002749 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002750 if (ctx) {
2751 cred = ctx->cred;
2752 state = ctx->state;
2753 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002754 }
2755
2756 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002757 if (status == 0)
2758 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return status;
2760}
2761
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002762static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2763 const struct qstr *name, struct nfs_fh *fhandle,
2764 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002765{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002766 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002767 int status;
2768 struct nfs4_lookup_arg args = {
2769 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002770 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002771 .name = name,
2772 };
2773 struct nfs4_lookup_res res = {
2774 .server = server,
2775 .fattr = fattr,
2776 .fh = fhandle,
2777 };
2778 struct rpc_message msg = {
2779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2780 .rpc_argp = &args,
2781 .rpc_resp = &res,
2782 };
2783
2784 nfs_fattr_init(fattr);
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002787 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 dprintk("NFS reply lookup: %d\n", status);
2789 return status;
2790}
2791
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002792static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002793{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002794 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002795 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002796 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2797 fattr->nlink = 2;
2798}
2799
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002800static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2801 struct qstr *name, struct nfs_fh *fhandle,
2802 struct nfs_fattr *fattr)
2803{
2804 struct nfs4_exception exception = { };
2805 struct rpc_clnt *client = *clnt;
2806 int err;
2807 do {
2808 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2809 switch (err) {
2810 case -NFS4ERR_BADNAME:
2811 err = -ENOENT;
2812 goto out;
2813 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002814 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002815 goto out;
2816 case -NFS4ERR_WRONGSEC:
2817 err = -EPERM;
2818 if (client != *clnt)
2819 goto out;
2820
2821 client = nfs4_create_sec_client(client, dir, name);
2822 if (IS_ERR(client))
2823 return PTR_ERR(client);
2824
2825 exception.retry = 1;
2826 break;
2827 default:
2828 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2829 }
2830 } while (exception.retry);
2831
2832out:
2833 if (err == 0)
2834 *clnt = client;
2835 else if (client != *clnt)
2836 rpc_shutdown_client(client);
2837
2838 return err;
2839}
2840
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002841static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002842 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002844 int status;
2845 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002846
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002847 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2848 if (client != NFS_CLIENT(dir)) {
2849 rpc_shutdown_client(client);
2850 nfs_fixup_secinfo_attributes(fattr);
2851 }
2852 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002855struct rpc_clnt *
2856nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2857 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2858{
2859 int status;
2860 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2861
2862 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2863 if (status < 0) {
2864 rpc_shutdown_client(client);
2865 return ERR_PTR(status);
2866 }
2867 return client;
2868}
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2871{
Trond Myklebust76b32992007-08-10 17:45:11 -04002872 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct nfs4_accessargs args = {
2874 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002875 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002877 struct nfs4_accessres res = {
2878 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002879 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct rpc_message msg = {
2881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2882 .rpc_argp = &args,
2883 .rpc_resp = &res,
2884 .rpc_cred = entry->cred,
2885 };
2886 int mode = entry->mask;
2887 int status;
2888
2889 /*
2890 * Determine which access bits we want to ask for...
2891 */
2892 if (mode & MAY_READ)
2893 args.access |= NFS4_ACCESS_READ;
2894 if (S_ISDIR(inode->i_mode)) {
2895 if (mode & MAY_WRITE)
2896 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2897 if (mode & MAY_EXEC)
2898 args.access |= NFS4_ACCESS_LOOKUP;
2899 } else {
2900 if (mode & MAY_WRITE)
2901 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2902 if (mode & MAY_EXEC)
2903 args.access |= NFS4_ACCESS_EXECUTE;
2904 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002905
2906 res.fattr = nfs_alloc_fattr();
2907 if (res.fattr == NULL)
2908 return -ENOMEM;
2909
Bryan Schumaker7c513052011-03-24 17:12:24 +00002910 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002912 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002913 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002915 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return status;
2917}
2918
2919static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2920{
2921 struct nfs4_exception exception = { };
2922 int err;
2923 do {
2924 err = nfs4_handle_exception(NFS_SERVER(inode),
2925 _nfs4_proc_access(inode, entry),
2926 &exception);
2927 } while (exception.retry);
2928 return err;
2929}
2930
2931/*
2932 * TODO: For the time being, we don't try to get any attributes
2933 * along with any of the zero-copy operations READ, READDIR,
2934 * READLINK, WRITE.
2935 *
2936 * In the case of the first three, we want to put the GETATTR
2937 * after the read-type operation -- this is because it is hard
2938 * to predict the length of a GETATTR response in v4, and thus
2939 * align the READ data correctly. This means that the GETATTR
2940 * may end up partially falling into the page cache, and we should
2941 * shift it into the 'tail' of the xdr_buf before processing.
2942 * To do this efficiently, we need to know the total length
2943 * of data received, which doesn't seem to be available outside
2944 * of the RPC layer.
2945 *
2946 * In the case of WRITE, we also want to put the GETATTR after
2947 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002948 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 *
2950 * Both of these changes to the XDR layer would in fact be quite
2951 * minor, but I decided to leave them for a subsequent patch.
2952 */
2953static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2954 unsigned int pgbase, unsigned int pglen)
2955{
2956 struct nfs4_readlink args = {
2957 .fh = NFS_FH(inode),
2958 .pgbase = pgbase,
2959 .pglen = pglen,
2960 .pages = &page,
2961 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002962 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 struct rpc_message msg = {
2964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2965 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002966 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 };
2968
Bryan Schumaker7c513052011-03-24 17:12:24 +00002969 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 -07002970}
2971
2972static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2973 unsigned int pgbase, unsigned int pglen)
2974{
2975 struct nfs4_exception exception = { };
2976 int err;
2977 do {
2978 err = nfs4_handle_exception(NFS_SERVER(inode),
2979 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2980 &exception);
2981 } while (exception.retry);
2982 return err;
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002986 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988static int
2989nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002990 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002992 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 int status = 0;
2995
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002996 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2997 if (IS_ERR(ctx))
2998 return PTR_ERR(ctx);
2999
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003000 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003001 state = nfs4_do_open(dir, dentry, ctx->mode,
3002 flags, sattr, ctx->cred,
3003 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003004 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (IS_ERR(state)) {
3006 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003009 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003010 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003011 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003013 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return status;
3015}
3016
3017static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3018{
Trond Myklebust16e42952005-10-27 22:12:44 -04003019 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003020 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003022 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003024 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003025 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3029 .rpc_argp = &args,
3030 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003032 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003033
Bryan Schumaker7c513052011-03-24 17:12:24 +00003034 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003035 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003036 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return status;
3038}
3039
3040static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3041{
3042 struct nfs4_exception exception = { };
3043 int err;
3044 do {
3045 err = nfs4_handle_exception(NFS_SERVER(dir),
3046 _nfs4_proc_remove(dir, name),
3047 &exception);
3048 } while (exception.retry);
3049 return err;
3050}
3051
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003052static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003054 struct nfs_server *server = NFS_SERVER(dir);
3055 struct nfs_removeargs *args = msg->rpc_argp;
3056 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003060 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003063static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3064{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003065 nfs4_setup_sequence(NFS_SERVER(data->dir),
3066 &data->args.seq_args,
3067 &data->res.seq_res,
3068 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003071static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003073 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3074
Trond Myklebust14516c32010-07-31 14:29:06 -04003075 if (!nfs4_sequence_done(task, &res->seq_res))
3076 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003077 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003078 return 0;
3079 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003080 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Jeff Laytond3d41522010-09-17 17:31:57 -04003083static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3084{
3085 struct nfs_server *server = NFS_SERVER(dir);
3086 struct nfs_renameargs *arg = msg->rpc_argp;
3087 struct nfs_renameres *res = msg->rpc_resp;
3088
3089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003090 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003091 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003092}
3093
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003094static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3095{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003096 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3097 &data->args.seq_args,
3098 &data->res.seq_res,
3099 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003100}
3101
3102static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3103 struct inode *new_dir)
3104{
3105 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3106
3107 if (!nfs4_sequence_done(task, &res->seq_res))
3108 return 0;
3109 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3110 return 0;
3111
3112 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003113 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003114 return 1;
3115}
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3118 struct inode *new_dir, struct qstr *new_name)
3119{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003120 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003121 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 .old_dir = NFS_FH(old_dir),
3123 .new_dir = NFS_FH(new_dir),
3124 .old_name = old_name,
3125 .new_name = new_name,
3126 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003127 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003128 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct rpc_message msg = {
3131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3132 .rpc_argp = &arg,
3133 .rpc_resp = &res,
3134 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003135 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Bryan Schumaker7c513052011-03-24 17:12:24 +00003137 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (!status) {
3139 update_changeattr(old_dir, &res.old_cinfo);
3140 update_changeattr(new_dir, &res.new_cinfo);
3141 }
3142 return status;
3143}
3144
3145static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3146 struct inode *new_dir, struct qstr *new_name)
3147{
3148 struct nfs4_exception exception = { };
3149 int err;
3150 do {
3151 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3152 _nfs4_proc_rename(old_dir, old_name,
3153 new_dir, new_name),
3154 &exception);
3155 } while (exception.retry);
3156 return err;
3157}
3158
3159static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3160{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003161 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 struct nfs4_link_arg arg = {
3163 .fh = NFS_FH(inode),
3164 .dir_fh = NFS_FH(dir),
3165 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003166 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003168 struct nfs4_link_res res = {
3169 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3173 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003174 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003176 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Trond Myklebust136f2622010-04-16 16:22:49 -04003178 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003179 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003180 goto out;
3181
Bryan Schumaker7c513052011-03-24 17:12:24 +00003182 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003183 if (!status) {
3184 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003185 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003186 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003187out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003188 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return status;
3190}
3191
3192static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3193{
3194 struct nfs4_exception exception = { };
3195 int err;
3196 do {
3197 err = nfs4_handle_exception(NFS_SERVER(inode),
3198 _nfs4_proc_link(inode, dir, name),
3199 &exception);
3200 } while (exception.retry);
3201 return err;
3202}
3203
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003204struct nfs4_createdata {
3205 struct rpc_message msg;
3206 struct nfs4_create_arg arg;
3207 struct nfs4_create_res res;
3208 struct nfs_fh fh;
3209 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210};
3211
3212static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3213 struct qstr *name, struct iattr *sattr, u32 ftype)
3214{
3215 struct nfs4_createdata *data;
3216
3217 data = kzalloc(sizeof(*data), GFP_KERNEL);
3218 if (data != NULL) {
3219 struct nfs_server *server = NFS_SERVER(dir);
3220
3221 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3222 data->msg.rpc_argp = &data->arg;
3223 data->msg.rpc_resp = &data->res;
3224 data->arg.dir_fh = NFS_FH(dir);
3225 data->arg.server = server;
3226 data->arg.name = name;
3227 data->arg.attrs = sattr;
3228 data->arg.ftype = ftype;
3229 data->arg.bitmask = server->attr_bitmask;
3230 data->res.server = server;
3231 data->res.fh = &data->fh;
3232 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003233 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003234 }
3235 return data;
3236}
3237
3238static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3239{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003240 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003241 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 if (status == 0) {
3243 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3245 }
3246 return status;
3247}
3248
3249static void nfs4_free_createdata(struct nfs4_createdata *data)
3250{
3251 kfree(data);
3252}
3253
Chuck Lever4f390c12006-08-22 20:06:22 -04003254static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003255 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257 struct nfs4_createdata *data;
3258 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Chuck Lever94a6d752006-08-22 20:06:23 -04003260 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003261 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003262
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 status = -ENOMEM;
3264 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3265 if (data == NULL)
3266 goto out;
3267
3268 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3269 data->arg.u.symlink.pages = &page;
3270 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003272 status = nfs4_do_create(dir, dentry, data);
3273
3274 nfs4_free_createdata(data);
3275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return status;
3277}
3278
Chuck Lever4f390c12006-08-22 20:06:22 -04003279static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003280 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
3282 struct nfs4_exception exception = { };
3283 int err;
3284 do {
3285 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003286 _nfs4_proc_symlink(dir, dentry, page,
3287 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 &exception);
3289 } while (exception.retry);
3290 return err;
3291}
3292
3293static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3294 struct iattr *sattr)
3295{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 struct nfs4_createdata *data;
3297 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003299 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3300 if (data == NULL)
3301 goto out;
3302
3303 status = nfs4_do_create(dir, dentry, data);
3304
3305 nfs4_free_createdata(data);
3306out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return status;
3308}
3309
3310static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3311 struct iattr *sattr)
3312{
3313 struct nfs4_exception exception = { };
3314 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003315
3316 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 do {
3318 err = nfs4_handle_exception(NFS_SERVER(dir),
3319 _nfs4_proc_mkdir(dir, dentry, sattr),
3320 &exception);
3321 } while (exception.retry);
3322 return err;
3323}
3324
3325static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003326 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
3328 struct inode *dir = dentry->d_inode;
3329 struct nfs4_readdir_arg args = {
3330 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003331 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 .pgbase = 0,
3333 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003334 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003335 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 };
3337 struct nfs4_readdir_res res;
3338 struct rpc_message msg = {
3339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3340 .rpc_argp = &args,
3341 .rpc_resp = &res,
3342 .rpc_cred = cred,
3343 };
3344 int status;
3345
Harvey Harrison3110ff82008-05-02 13:42:44 -07003346 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003347 dentry->d_parent->d_name.name,
3348 dentry->d_name.name,
3349 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003350 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003352 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 -05003353 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003354 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003355 status += args.pgbase;
3356 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003357
3358 nfs_invalidate_atime(dir);
3359
Harvey Harrison3110ff82008-05-02 13:42:44 -07003360 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return status;
3362}
3363
3364static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003365 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
3367 struct nfs4_exception exception = { };
3368 int err;
3369 do {
3370 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3371 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003372 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 &exception);
3374 } while (exception.retry);
3375 return err;
3376}
3377
3378static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3379 struct iattr *sattr, dev_t rdev)
3380{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003381 struct nfs4_createdata *data;
3382 int mode = sattr->ia_mode;
3383 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003385 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3386 if (data == NULL)
3387 goto out;
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003390 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003392 data->arg.ftype = NF4BLK;
3393 data->arg.u.device.specdata1 = MAJOR(rdev);
3394 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003397 data->arg.ftype = NF4CHR;
3398 data->arg.u.device.specdata1 = MAJOR(rdev);
3399 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003400 } else if (!S_ISSOCK(mode)) {
3401 status = -EINVAL;
3402 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003405 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003406out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003407 nfs4_free_createdata(data);
3408out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return status;
3410}
3411
3412static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3413 struct iattr *sattr, dev_t rdev)
3414{
3415 struct nfs4_exception exception = { };
3416 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003417
3418 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 do {
3420 err = nfs4_handle_exception(NFS_SERVER(dir),
3421 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3422 &exception);
3423 } while (exception.retry);
3424 return err;
3425}
3426
3427static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3428 struct nfs_fsstat *fsstat)
3429{
3430 struct nfs4_statfs_arg args = {
3431 .fh = fhandle,
3432 .bitmask = server->attr_bitmask,
3433 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003434 struct nfs4_statfs_res res = {
3435 .fsstat = fsstat,
3436 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 struct rpc_message msg = {
3438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3439 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003440 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 };
3442
Trond Myklebust0e574af2005-10-27 22:12:38 -04003443 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003444 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
3447static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3448{
3449 struct nfs4_exception exception = { };
3450 int err;
3451 do {
3452 err = nfs4_handle_exception(server,
3453 _nfs4_proc_statfs(server, fhandle, fsstat),
3454 &exception);
3455 } while (exception.retry);
3456 return err;
3457}
3458
3459static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3460 struct nfs_fsinfo *fsinfo)
3461{
3462 struct nfs4_fsinfo_arg args = {
3463 .fh = fhandle,
3464 .bitmask = server->attr_bitmask,
3465 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003466 struct nfs4_fsinfo_res res = {
3467 .fsinfo = fsinfo,
3468 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 struct rpc_message msg = {
3470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3471 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003472 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 };
3474
Bryan Schumaker7c513052011-03-24 17:12:24 +00003475 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
3478static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3479{
3480 struct nfs4_exception exception = { };
3481 int err;
3482
3483 do {
3484 err = nfs4_handle_exception(server,
3485 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3486 &exception);
3487 } while (exception.retry);
3488 return err;
3489}
3490
3491static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3492{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003493 int error;
3494
Trond Myklebust0e574af2005-10-27 22:12:38 -04003495 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003496 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003497 if (error == 0) {
3498 /* block layout checks this! */
3499 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003500 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003501 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003502
3503 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
3506static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3507 struct nfs_pathconf *pathconf)
3508{
3509 struct nfs4_pathconf_arg args = {
3510 .fh = fhandle,
3511 .bitmask = server->attr_bitmask,
3512 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003513 struct nfs4_pathconf_res res = {
3514 .pathconf = pathconf,
3515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 struct rpc_message msg = {
3517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3518 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003519 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 };
3521
3522 /* None of the pathconf attributes are mandatory to implement */
3523 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3524 memset(pathconf, 0, sizeof(*pathconf));
3525 return 0;
3526 }
3527
Trond Myklebust0e574af2005-10-27 22:12:38 -04003528 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003529 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
3532static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3533 struct nfs_pathconf *pathconf)
3534{
3535 struct nfs4_exception exception = { };
3536 int err;
3537
3538 do {
3539 err = nfs4_handle_exception(server,
3540 _nfs4_proc_pathconf(server, fhandle, pathconf),
3541 &exception);
3542 } while (exception.retry);
3543 return err;
3544}
3545
Trond Myklebust5521abf2013-03-16 20:54:34 -04003546int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003547 const struct nfs_open_context *ctx,
3548 const struct nfs_lock_context *l_ctx,
3549 fmode_t fmode)
3550{
3551 const struct nfs_lockowner *lockowner = NULL;
3552
3553 if (l_ctx != NULL)
3554 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003555 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003556}
3557EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3558
Trond Myklebust5521abf2013-03-16 20:54:34 -04003559static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3560 const struct nfs_open_context *ctx,
3561 const struct nfs_lock_context *l_ctx,
3562 fmode_t fmode)
3563{
3564 nfs4_stateid current_stateid;
3565
3566 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3567 return false;
3568 return nfs4_stateid_match(stateid, &current_stateid);
3569}
3570
3571static bool nfs4_error_stateid_expired(int err)
3572{
3573 switch (err) {
3574 case -NFS4ERR_DELEG_REVOKED:
3575 case -NFS4ERR_ADMIN_REVOKED:
3576 case -NFS4ERR_BAD_STATEID:
3577 case -NFS4ERR_STALE_STATEID:
3578 case -NFS4ERR_OLD_STATEID:
3579 case -NFS4ERR_OPENMODE:
3580 case -NFS4ERR_EXPIRED:
3581 return true;
3582 }
3583 return false;
3584}
3585
Benny Halevyd20581a2011-05-22 19:52:03 +03003586void __nfs4_read_done_cb(struct nfs_read_data *data)
3587{
Fred Isamancd841602012-04-20 14:47:44 -04003588 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003589}
3590
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003591static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Fred Isamancd841602012-04-20 14:47:44 -04003593 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003595 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003596 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003597 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003599
Benny Halevyd20581a2011-05-22 19:52:03 +03003600 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003602 renew_lease(server, data->timestamp);
3603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Trond Myklebust5521abf2013-03-16 20:54:34 -04003606static bool nfs4_read_stateid_changed(struct rpc_task *task,
3607 struct nfs_readargs *args)
3608{
3609
3610 if (!nfs4_error_stateid_expired(task->tk_status) ||
3611 nfs4_stateid_is_current(&args->stateid,
3612 args->context,
3613 args->lock_context,
3614 FMODE_READ))
3615 return false;
3616 rpc_restart_call_prepare(task);
3617 return true;
3618}
3619
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003620static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3621{
3622
3623 dprintk("--> %s\n", __func__);
3624
3625 if (!nfs4_sequence_done(task, &data->res.seq_res))
3626 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003627 if (nfs4_read_stateid_changed(task, &data->args))
3628 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003629 return data->read_done_cb ? data->read_done_cb(task, data) :
3630 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003631}
3632
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003633static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003636 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003637 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003638 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003641static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3642{
Trond Myklebust9b206142013-03-17 15:52:00 -04003643 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003644 &data->args.seq_args,
3645 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003646 task))
3647 return;
3648 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3649 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Fred Isamanb029bc92011-03-03 15:13:42 +00003652static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Fred Isamancd841602012-04-20 14:47:44 -04003654 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003656 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003657 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003658 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003660 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003662 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003663 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
Trond Myklebust5521abf2013-03-16 20:54:34 -04003667static bool nfs4_write_stateid_changed(struct rpc_task *task,
3668 struct nfs_writeargs *args)
3669{
3670
3671 if (!nfs4_error_stateid_expired(task->tk_status) ||
3672 nfs4_stateid_is_current(&args->stateid,
3673 args->context,
3674 args->lock_context,
3675 FMODE_WRITE))
3676 return false;
3677 rpc_restart_call_prepare(task);
3678 return true;
3679}
3680
Fred Isamanb029bc92011-03-03 15:13:42 +00003681static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3682{
3683 if (!nfs4_sequence_done(task, &data->res.seq_res))
3684 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003685 if (nfs4_write_stateid_changed(task, &data->args))
3686 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003687 return data->write_done_cb ? data->write_done_cb(task, data) :
3688 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003689}
3690
Trond Myklebust5a37f852012-04-28 14:55:16 -04003691static
3692bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003693{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003694 const struct nfs_pgio_header *hdr = data->header;
3695
3696 /* Don't request attributes for pNFS or O_DIRECT writes */
3697 if (data->ds_clp != NULL || hdr->dreq != NULL)
3698 return false;
3699 /* Otherwise, request attributes if and only if we don't hold
3700 * a delegation
3701 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003702 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003703}
Fred Isamana69aef12011-03-03 15:13:47 +00003704
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003705static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Fred Isamancd841602012-04-20 14:47:44 -04003707 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003708
Trond Myklebust5a37f852012-04-28 14:55:16 -04003709 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003710 data->args.bitmask = NULL;
3711 data->res.fattr = NULL;
3712 } else
3713 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003714
Fred Isamanb029bc92011-03-03 15:13:42 +00003715 if (!data->write_done_cb)
3716 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003717 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 data->timestamp = jiffies;
3719
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003720 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003721 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003724static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3725{
Trond Myklebust9b206142013-03-17 15:52:00 -04003726 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003727 &data->args.seq_args,
3728 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003729 task))
3730 return;
3731 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3732 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Fred Isaman0b7c0152012-04-20 14:47:39 -04003735static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3736{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003737 nfs4_setup_sequence(NFS_SERVER(data->inode),
3738 &data->args.seq_args,
3739 &data->res.seq_res,
3740 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Fred Isaman0b7c0152012-04-20 14:47:39 -04003743static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003746
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003747 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003748 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003749 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
3753
Fred Isaman0b7c0152012-04-20 14:47:39 -04003754static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003755{
3756 if (!nfs4_sequence_done(task, &data->res.seq_res))
3757 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003758 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003759}
3760
Fred Isaman0b7c0152012-04-20 14:47:39 -04003761static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003763 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003764
Fred Isaman0b7c0152012-04-20 14:47:39 -04003765 if (data->commit_done_cb == NULL)
3766 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003767 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003768 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003769 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003772struct nfs4_renewdata {
3773 struct nfs_client *client;
3774 unsigned long timestamp;
3775};
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777/*
3778 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3779 * standalone procedure for queueing an asynchronous RENEW.
3780 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003781static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003782{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003783 struct nfs4_renewdata *data = calldata;
3784 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003785
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003786 if (atomic_read(&clp->cl_count) > 1)
3787 nfs4_schedule_state_renewal(clp);
3788 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003789 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003790}
3791
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003792static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003794 struct nfs4_renewdata *data = calldata;
3795 struct nfs_client *clp = data->client;
3796 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003799 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003800 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3801 return;
3802 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003803 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003804 return;
3805 }
3806 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003808 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003811static const struct rpc_call_ops nfs4_renew_ops = {
3812 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003813 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003814};
3815
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003816static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 struct rpc_message msg = {
3819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3820 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003821 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003823 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003825 if (renew_flags == 0)
3826 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003827 if (!atomic_inc_not_zero(&clp->cl_count))
3828 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003829 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003830 if (data == NULL)
3831 return -ENOMEM;
3832 data->client = clp;
3833 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003834 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003835 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003838static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 struct rpc_message msg = {
3841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3842 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003843 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 };
3845 unsigned long now = jiffies;
3846 int status;
3847
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003848 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (status < 0)
3850 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003851 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 return 0;
3853}
3854
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003855static inline int nfs4_server_supports_acls(struct nfs_server *server)
3856{
3857 return (server->caps & NFS_CAP_ACLS)
3858 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3859 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3860}
3861
Trond Myklebust21f498c2012-08-24 10:59:25 -04003862/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3863 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003864 * the stack.
3865 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003866#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003867
Neil Hormane9e3d722011-03-04 19:26:03 -05003868static int buf_to_pages_noslab(const void *buf, size_t buflen,
3869 struct page **pages, unsigned int *pgbase)
3870{
3871 struct page *newpage, **spages;
3872 int rc = 0;
3873 size_t len;
3874 spages = pages;
3875
3876 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003877 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003878 newpage = alloc_page(GFP_KERNEL);
3879
3880 if (newpage == NULL)
3881 goto unwind;
3882 memcpy(page_address(newpage), buf, len);
3883 buf += len;
3884 buflen -= len;
3885 *pages++ = newpage;
3886 rc++;
3887 } while (buflen != 0);
3888
3889 return rc;
3890
3891unwind:
3892 for(; rc > 0; rc--)
3893 __free_page(spages[rc-1]);
3894 return -ENOMEM;
3895}
3896
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003897struct nfs4_cached_acl {
3898 int cached;
3899 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003900 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003901};
3902
3903static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003904{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003905 struct nfs_inode *nfsi = NFS_I(inode);
3906
3907 spin_lock(&inode->i_lock);
3908 kfree(nfsi->nfs4_acl);
3909 nfsi->nfs4_acl = acl;
3910 spin_unlock(&inode->i_lock);
3911}
3912
3913static void nfs4_zap_acl_attr(struct inode *inode)
3914{
3915 nfs4_set_cached_acl(inode, NULL);
3916}
3917
3918static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3919{
3920 struct nfs_inode *nfsi = NFS_I(inode);
3921 struct nfs4_cached_acl *acl;
3922 int ret = -ENOENT;
3923
3924 spin_lock(&inode->i_lock);
3925 acl = nfsi->nfs4_acl;
3926 if (acl == NULL)
3927 goto out;
3928 if (buf == NULL) /* user is just asking for length */
3929 goto out_len;
3930 if (acl->cached == 0)
3931 goto out;
3932 ret = -ERANGE; /* see getxattr(2) man page */
3933 if (acl->len > buflen)
3934 goto out;
3935 memcpy(buf, acl->data, acl->len);
3936out_len:
3937 ret = acl->len;
3938out:
3939 spin_unlock(&inode->i_lock);
3940 return ret;
3941}
3942
Sachin Prabhu5794d212012-04-17 14:36:40 +01003943static 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 +00003944{
3945 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003946 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003947
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003948 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003949 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003950 if (acl == NULL)
3951 goto out;
3952 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003953 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003954 } else {
3955 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3956 if (acl == NULL)
3957 goto out;
3958 acl->cached = 0;
3959 }
3960 acl->len = acl_len;
3961out:
3962 nfs4_set_cached_acl(inode, acl);
3963}
3964
Andy Adamsonbf118a32011-12-07 11:55:27 -05003965/*
3966 * The getxattr API returns the required buffer length when called with a
3967 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3968 * the required buf. On a NULL buf, we send a page of data to the server
3969 * guessing that the ACL request can be serviced by a page. If so, we cache
3970 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3971 * the cache. If not so, we throw away the page, and cache the required
3972 * length. The next getxattr call will then produce another round trip to
3973 * the server, this time with the input buf of the required size.
3974 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003975static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003976{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003977 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003978 struct nfs_getaclargs args = {
3979 .fh = NFS_FH(inode),
3980 .acl_pages = pages,
3981 .acl_len = buflen,
3982 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003983 struct nfs_getaclres res = {
3984 .acl_len = buflen,
3985 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003986 struct rpc_message msg = {
3987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3988 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003989 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003990 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003991 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3992 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003993
Andy Adamsonbf118a32011-12-07 11:55:27 -05003994 /* As long as we're doing a round trip to the server anyway,
3995 * let's be prepared for a page of acl data. */
3996 if (npages == 0)
3997 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003998 if (npages > ARRAY_SIZE(pages))
3999 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004000
Andy Adamsonbf118a32011-12-07 11:55:27 -05004001 for (i = 0; i < npages; i++) {
4002 pages[i] = alloc_page(GFP_KERNEL);
4003 if (!pages[i])
4004 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004005 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004006
4007 /* for decoding across pages */
4008 res.acl_scratch = alloc_page(GFP_KERNEL);
4009 if (!res.acl_scratch)
4010 goto out_free;
4011
Andy Adamsonbf118a32011-12-07 11:55:27 -05004012 args.acl_len = npages * PAGE_SIZE;
4013 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004014
Peng Taode040be2012-01-10 22:42:47 +08004015 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004016 __func__, buf, buflen, npages, args.acl_len);
4017 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4018 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004019 if (ret)
4020 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004021
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004022 /* Handle the case where the passed-in buffer is too short */
4023 if (res.acl_flags & NFS4_ACL_TRUNC) {
4024 /* Did the user only issue a request for the acl length? */
4025 if (buf == NULL)
4026 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004027 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004028 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004029 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004030 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004031 if (buf) {
4032 if (res.acl_len > buflen) {
4033 ret = -ERANGE;
4034 goto out_free;
4035 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004036 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004037 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004038out_ok:
4039 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004040out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004041 for (i = 0; i < npages; i++)
4042 if (pages[i])
4043 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004044 if (res.acl_scratch)
4045 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004046 return ret;
4047}
4048
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004049static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4050{
4051 struct nfs4_exception exception = { };
4052 ssize_t ret;
4053 do {
4054 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4055 if (ret >= 0)
4056 break;
4057 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4058 } while (exception.retry);
4059 return ret;
4060}
4061
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004062static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4063{
4064 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004065 int ret;
4066
4067 if (!nfs4_server_supports_acls(server))
4068 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004069 ret = nfs_revalidate_inode(server, inode);
4070 if (ret < 0)
4071 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004072 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4073 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004074 ret = nfs4_read_cached_acl(inode, buf, buflen);
4075 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004076 /* -ENOENT is returned if there is no ACL or if there is an ACL
4077 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004078 return ret;
4079 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004080}
4081
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004082static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004083{
4084 struct nfs_server *server = NFS_SERVER(inode);
4085 struct page *pages[NFS4ACL_MAXPAGES];
4086 struct nfs_setaclargs arg = {
4087 .fh = NFS_FH(inode),
4088 .acl_pages = pages,
4089 .acl_len = buflen,
4090 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004091 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4094 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004095 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004096 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004097 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004098 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004099
4100 if (!nfs4_server_supports_acls(server))
4101 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004102 if (npages > ARRAY_SIZE(pages))
4103 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004104 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4105 if (i < 0)
4106 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004107 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004108 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004109
4110 /*
4111 * Free each page after tx, so the only ref left is
4112 * held by the network stack
4113 */
4114 for (; i > 0; i--)
4115 put_page(pages[i-1]);
4116
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004117 /*
4118 * Acl update can result in inode attribute update.
4119 * so mark the attribute cache invalid.
4120 */
4121 spin_lock(&inode->i_lock);
4122 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4123 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004124 nfs_access_zap_cache(inode);
4125 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004126 return ret;
4127}
4128
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004129static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4130{
4131 struct nfs4_exception exception = { };
4132 int err;
4133 do {
4134 err = nfs4_handle_exception(NFS_SERVER(inode),
4135 __nfs4_proc_set_acl(inode, buf, buflen),
4136 &exception);
4137 } while (exception.retry);
4138 return err;
4139}
4140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004142nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004144 struct nfs_client *clp = server->nfs_client;
4145
4146 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return 0;
4148 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004149 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004150 case -NFS4ERR_ADMIN_REVOKED:
4151 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004152 if (state == NULL)
4153 break;
4154 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004155 case -NFS4ERR_OPENMODE:
4156 if (state == NULL)
4157 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004158 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4159 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004160 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004161 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004162 if (state != NULL) {
4163 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4164 goto stateid_invalid;
4165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004167 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004168 nfs4_schedule_lease_recovery(clp);
4169 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004170#if defined(CONFIG_NFS_V4_1)
4171 case -NFS4ERR_BADSESSION:
4172 case -NFS4ERR_BADSLOT:
4173 case -NFS4ERR_BAD_HIGH_SLOT:
4174 case -NFS4ERR_DEADSESSION:
4175 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4176 case -NFS4ERR_SEQ_FALSE_RETRY:
4177 case -NFS4ERR_SEQ_MISORDERED:
4178 dprintk("%s ERROR %d, Reset session\n", __func__,
4179 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004180 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004181 task->tk_status = 0;
4182 return -EAGAIN;
4183#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004185 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004186 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4188 task->tk_status = 0;
4189 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004190 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 case -NFS4ERR_OLD_STATEID:
4192 task->tk_status = 0;
4193 return -EAGAIN;
4194 }
4195 task->tk_status = nfs4_map_errors(task->tk_status);
4196 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004197stateid_invalid:
4198 task->tk_status = -EIO;
4199 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004200wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004201 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004202 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4203 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4204 task->tk_status = 0;
4205 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206}
4207
Chuck Leverf0920752012-05-21 22:45:41 -04004208static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4209 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004210{
4211 __be32 verf[2];
4212
Chuck Lever2c820d92012-05-21 22:45:33 -04004213 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4214 /* An impossible timestamp guarantees this value
4215 * will never match a generated boot time. */
4216 verf[0] = 0;
4217 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4218 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004219 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4220 verf[0] = (__be32)nn->boot_time.tv_sec;
4221 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004222 }
Chuck Levercd937102012-03-02 17:14:31 -05004223 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4224}
4225
Chuck Levere984a552012-09-14 17:24:21 -04004226static unsigned int
4227nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4228 char *buf, size_t len)
4229{
4230 unsigned int result;
4231
4232 rcu_read_lock();
4233 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4234 clp->cl_ipaddr,
4235 rpc_peeraddr2str(clp->cl_rpcclient,
4236 RPC_DISPLAY_ADDR),
4237 rpc_peeraddr2str(clp->cl_rpcclient,
4238 RPC_DISPLAY_PROTO));
4239 rcu_read_unlock();
4240 return result;
4241}
4242
4243static unsigned int
4244nfs4_init_uniform_client_string(const struct nfs_client *clp,
4245 char *buf, size_t len)
4246{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004247 char *nodename = clp->cl_rpcclient->cl_nodename;
4248
4249 if (nfs4_client_id_uniquifier[0] != '\0')
4250 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004251 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4252 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004253 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004254}
4255
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004256/**
4257 * nfs4_proc_setclientid - Negotiate client ID
4258 * @clp: state data structure
4259 * @program: RPC program for NFSv4 callback service
4260 * @port: IP port number for NFS4 callback service
4261 * @cred: RPC credential to use for this call
4262 * @res: where to place the result
4263 *
4264 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4265 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004266int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4267 unsigned short port, struct rpc_cred *cred,
4268 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
4270 nfs4_verifier sc_verifier;
4271 struct nfs4_setclientid setclientid = {
4272 .sc_verifier = &sc_verifier,
4273 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004274 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
4276 struct rpc_message msg = {
4277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4278 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004279 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004280 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004282 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Chuck Leverde734832012-07-11 16:30:50 -04004284 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004285 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004286 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4287 setclientid.sc_name_len =
4288 nfs4_init_uniform_client_string(clp,
4289 setclientid.sc_name,
4290 sizeof(setclientid.sc_name));
4291 else
4292 setclientid.sc_name_len =
4293 nfs4_init_nonuniform_client_string(clp,
4294 setclientid.sc_name,
4295 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004296 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004297 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004298 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004299 sizeof(setclientid.sc_netid),
4300 rpc_peeraddr2str(clp->cl_rpcclient,
4301 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004302 rcu_read_unlock();
4303 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004304 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 clp->cl_ipaddr, port >> 8, port & 255);
4306
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004307 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4308 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4309 setclientid.sc_name_len, setclientid.sc_name);
4310 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4311 dprintk("NFS reply setclientid: %d\n", status);
4312 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004315/**
4316 * nfs4_proc_setclientid_confirm - Confirm client ID
4317 * @clp: state data structure
4318 * @res: result of a previous SETCLIENTID
4319 * @cred: RPC credential to use for this call
4320 *
4321 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4322 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004323int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004324 struct nfs4_setclientid_res *arg,
4325 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct nfs_fsinfo fsinfo;
4328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004330 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004332 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 };
4334 unsigned long now;
4335 int status;
4336
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004337 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4338 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4339 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004341 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (status == 0) {
4343 spin_lock(&clp->cl_lock);
4344 clp->cl_lease_time = fsinfo.lease_time * HZ;
4345 clp->cl_last_renewal = now;
4346 spin_unlock(&clp->cl_lock);
4347 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004348 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return status;
4350}
4351
Trond Myklebustfe650402006-01-03 09:55:18 +01004352struct nfs4_delegreturndata {
4353 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004354 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004355 struct nfs_fh fh;
4356 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004357 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004358 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004359 int rpc_status;
4360};
4361
Trond Myklebustfe650402006-01-03 09:55:18 +01004362static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4363{
4364 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004365
Trond Myklebust14516c32010-07-31 14:29:06 -04004366 if (!nfs4_sequence_done(task, &data->res.seq_res))
4367 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004368
Ricardo Labiaga79708862009-12-07 09:23:21 -05004369 switch (task->tk_status) {
4370 case -NFS4ERR_STALE_STATEID:
4371 case -NFS4ERR_EXPIRED:
4372 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004373 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004374 break;
4375 default:
4376 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4377 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004378 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004379 return;
4380 }
4381 }
4382 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004383}
4384
4385static void nfs4_delegreturn_release(void *calldata)
4386{
Trond Myklebustfe650402006-01-03 09:55:18 +01004387 kfree(calldata);
4388}
4389
Andy Adamson938e1012009-04-01 09:22:28 -04004390#if defined(CONFIG_NFS_V4_1)
4391static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4392{
4393 struct nfs4_delegreturndata *d_data;
4394
4395 d_data = (struct nfs4_delegreturndata *)data;
4396
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004397 nfs4_setup_sequence(d_data->res.server,
4398 &d_data->args.seq_args,
4399 &d_data->res.seq_res,
4400 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004401}
4402#endif /* CONFIG_NFS_V4_1 */
4403
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004404static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004405#if defined(CONFIG_NFS_V4_1)
4406 .rpc_call_prepare = nfs4_delegreturn_prepare,
4407#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004408 .rpc_call_done = nfs4_delegreturn_done,
4409 .rpc_release = nfs4_delegreturn_release,
4410};
4411
Trond Myklebuste6f81072008-01-24 18:14:34 -05004412static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004413{
4414 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004415 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004416 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004417 struct rpc_message msg = {
4418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4419 .rpc_cred = cred,
4420 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004421 struct rpc_task_setup task_setup_data = {
4422 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004423 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004424 .callback_ops = &nfs4_delegreturn_ops,
4425 .flags = RPC_TASK_ASYNC,
4426 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004427 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004428
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004429 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004430 if (data == NULL)
4431 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004432 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004433 data->args.fhandle = &data->fh;
4434 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004435 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004436 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004437 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004438 data->res.fattr = &data->fattr;
4439 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004440 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004441 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004442 data->rpc_status = 0;
4443
Trond Myklebustc970aa82007-07-14 15:39:59 -04004444 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004445 msg.rpc_argp = &data->args;
4446 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004447 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004448 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004449 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004450 if (!issync)
4451 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004452 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004453 if (status != 0)
4454 goto out;
4455 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004456 if (status == 0)
4457 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4458 else
4459 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004460out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004461 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
Trond Myklebuste6f81072008-01-24 18:14:34 -05004465int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
4467 struct nfs_server *server = NFS_SERVER(inode);
4468 struct nfs4_exception exception = { };
4469 int err;
4470 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004471 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 switch (err) {
4473 case -NFS4ERR_STALE_STATEID:
4474 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 case 0:
4476 return 0;
4477 }
4478 err = nfs4_handle_exception(server, err, &exception);
4479 } while (exception.retry);
4480 return err;
4481}
4482
4483#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4484#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4485
4486/*
4487 * sleep, with exponential backoff, and retry the LOCK operation.
4488 */
4489static unsigned long
4490nfs4_set_lock_task_retry(unsigned long timeout)
4491{
Jeff Laytond3103102011-12-01 22:44:39 +01004492 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 timeout <<= 1;
4494 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4495 return NFS4_LOCK_MAXTIMEOUT;
4496 return timeout;
4497}
4498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4500{
4501 struct inode *inode = state->inode;
4502 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004503 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004504 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004506 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004508 struct nfs_lockt_res res = {
4509 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 };
4511 struct rpc_message msg = {
4512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4513 .rpc_argp = &arg,
4514 .rpc_resp = &res,
4515 .rpc_cred = state->owner->so_cred,
4516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 struct nfs4_lock_state *lsp;
4518 int status;
4519
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004520 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004521 status = nfs4_set_lock_state(state, request);
4522 if (status != 0)
4523 goto out;
4524 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004525 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004526 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004527 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004528 switch (status) {
4529 case 0:
4530 request->fl_type = F_UNLCK;
4531 break;
4532 case -NFS4ERR_DENIED:
4533 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004535 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return status;
4538}
4539
4540static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4541{
4542 struct nfs4_exception exception = { };
4543 int err;
4544
4545 do {
4546 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4547 _nfs4_proc_getlk(state, cmd, request),
4548 &exception);
4549 } while (exception.retry);
4550 return err;
4551}
4552
4553static int do_vfs_lock(struct file *file, struct file_lock *fl)
4554{
4555 int res = 0;
4556 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4557 case FL_POSIX:
4558 res = posix_lock_file_wait(file, fl);
4559 break;
4560 case FL_FLOCK:
4561 res = flock_lock_file_wait(file, fl);
4562 break;
4563 default:
4564 BUG();
4565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return res;
4567}
4568
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004569struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004570 struct nfs_locku_args arg;
4571 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004572 struct nfs4_lock_state *lsp;
4573 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004574 struct file_lock fl;
4575 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004576 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004577};
4578
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004579static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4580 struct nfs_open_context *ctx,
4581 struct nfs4_lock_state *lsp,
4582 struct nfs_seqid *seqid)
4583{
4584 struct nfs4_unlockdata *p;
4585 struct inode *inode = lsp->ls_state->inode;
4586
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004587 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004588 if (p == NULL)
4589 return NULL;
4590 p->arg.fh = NFS_FH(inode);
4591 p->arg.fl = &p->fl;
4592 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004593 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004594 p->arg.stateid = &lsp->ls_stateid;
4595 p->lsp = lsp;
4596 atomic_inc(&lsp->ls_count);
4597 /* Ensure we don't close file until we're done freeing locks! */
4598 p->ctx = get_nfs_open_context(ctx);
4599 memcpy(&p->fl, fl, sizeof(p->fl));
4600 p->server = NFS_SERVER(inode);
4601 return p;
4602}
4603
Trond Myklebust06f814a2006-01-03 09:55:07 +01004604static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004605{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004606 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004607 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004608 nfs4_put_lock_state(calldata->lsp);
4609 put_nfs_open_context(calldata->ctx);
4610 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611}
4612
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004613static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004614{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004615 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004616
Trond Myklebust14516c32010-07-31 14:29:06 -04004617 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4618 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004619 switch (task->tk_status) {
4620 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004621 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4622 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004623 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004624 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004625 case -NFS4ERR_BAD_STATEID:
4626 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004627 case -NFS4ERR_STALE_STATEID:
4628 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004629 break;
4630 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004631 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004632 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004633 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004634 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004635}
4636
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004637static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004638{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004639 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004641 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004642 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004643 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004644 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004645 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004646 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004647 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004648 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004649 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004650 &calldata->res.seq_res,
4651 task) != 0)
4652 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004653 return;
4654out_no_action:
4655 task->tk_action = NULL;
4656out_wait:
4657 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004660static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004661 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004662 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004663 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004664};
4665
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4667 struct nfs_open_context *ctx,
4668 struct nfs4_lock_state *lsp,
4669 struct nfs_seqid *seqid)
4670{
4671 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4674 .rpc_cred = ctx->cred,
4675 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004676 struct rpc_task_setup task_setup_data = {
4677 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004678 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004679 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004680 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004681 .flags = RPC_TASK_ASYNC,
4682 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004683
Frank Filz137d6ac2007-07-09 15:32:29 -07004684 /* Ensure this is an unlock - when canceling a lock, the
4685 * canceled lock is passed in, and it won't be an unlock.
4686 */
4687 fl->fl_type = F_UNLCK;
4688
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4690 if (data == NULL) {
4691 nfs_free_seqid(seqid);
4692 return ERR_PTR(-ENOMEM);
4693 }
4694
Trond Myklebust9d12b212012-01-17 22:04:25 -05004695 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004696 msg.rpc_argp = &data->arg;
4697 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004698 task_setup_data.callback_data = data;
4699 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004700}
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4703{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004704 struct inode *inode = state->inode;
4705 struct nfs4_state_owner *sp = state->owner;
4706 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004707 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004708 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004709 struct rpc_task *task;
4710 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004711 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Trond Myklebust9b073572006-06-29 16:38:34 -04004713 status = nfs4_set_lock_state(state, request);
4714 /* Unlock _before_ we do the RPC call */
4715 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004716 /* Exclude nfs_delegation_claim_locks() */
4717 mutex_lock(&sp->so_delegreturn_mutex);
4718 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004719 down_read(&nfsi->rwsem);
4720 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4721 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004722 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004723 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004724 }
4725 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004726 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004727 if (status != 0)
4728 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004729 /* Is this a delegated lock? */
4730 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004731 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004732 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004733 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004734 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004735 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004736 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004737 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738 status = PTR_ERR(task);
4739 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004740 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004741 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004742 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004743out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004744 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004745 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748struct nfs4_lockdata {
4749 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004750 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 struct nfs4_lock_state *lsp;
4752 struct nfs_open_context *ctx;
4753 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004754 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 int rpc_status;
4756 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004757 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758};
4759
4760static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004761 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4762 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763{
4764 struct nfs4_lockdata *p;
4765 struct inode *inode = lsp->ls_state->inode;
4766 struct nfs_server *server = NFS_SERVER(inode);
4767
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004768 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004769 if (p == NULL)
4770 return NULL;
4771
4772 p->arg.fh = NFS_FH(inode);
4773 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004774 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004775 if (p->arg.open_seqid == NULL)
4776 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004777 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004778 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004779 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004780 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004781 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004782 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004783 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004784 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004785 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004786 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004787 atomic_inc(&lsp->ls_count);
4788 p->ctx = get_nfs_open_context(ctx);
4789 memcpy(&p->fl, fl, sizeof(p->fl));
4790 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004791out_free_seqid:
4792 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004793out_free:
4794 kfree(p);
4795 return NULL;
4796}
4797
4798static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4799{
4800 struct nfs4_lockdata *data = calldata;
4801 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Harvey Harrison3110ff82008-05-02 13:42:44 -07004803 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004804 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004805 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004806 /* Do we need to do an open_to_lock_owner? */
4807 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004808 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004809 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004810 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004811 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004813 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004814 } else
4815 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004816 if (!nfs4_valid_open_stateid(state)) {
4817 data->rpc_status = -EBADF;
4818 task->tk_action = NULL;
4819 goto out_release_open_seqid;
4820 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004821 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004822 if (nfs4_setup_sequence(data->server,
4823 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004824 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004825 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004826 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004827out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004828 nfs_release_seqid(data->arg.open_seqid);
4829out_release_lock_seqid:
4830 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004831out_wait:
4832 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004833 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004834}
4835
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004836static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4837{
4838 struct nfs4_lockdata *data = calldata;
4839
Harvey Harrison3110ff82008-05-02 13:42:44 -07004840 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841
Trond Myklebust14516c32010-07-31 14:29:06 -04004842 if (!nfs4_sequence_done(task, &data->res.seq_res))
4843 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004844
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004845 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004846 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 if (data->rpc_status == 0)
4848 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4849 else
4850 goto out;
4851 }
4852 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004853 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004854 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004855 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004856 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004857out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004858 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004859}
4860
4861static void nfs4_lock_release(void *calldata)
4862{
4863 struct nfs4_lockdata *data = calldata;
4864
Harvey Harrison3110ff82008-05-02 13:42:44 -07004865 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004866 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004867 if (data->cancelled != 0) {
4868 struct rpc_task *task;
4869 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4870 data->arg.lock_seqid);
4871 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004872 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004873 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004874 } else
4875 nfs_free_seqid(data->arg.lock_seqid);
4876 nfs4_put_lock_state(data->lsp);
4877 put_nfs_open_context(data->ctx);
4878 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004879 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004880}
4881
4882static const struct rpc_call_ops nfs4_lock_ops = {
4883 .rpc_call_prepare = nfs4_lock_prepare,
4884 .rpc_call_done = nfs4_lock_done,
4885 .rpc_release = nfs4_lock_release,
4886};
4887
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004888static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4889{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004890 switch (error) {
4891 case -NFS4ERR_ADMIN_REVOKED:
4892 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004893 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004894 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004895 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004896 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004897 break;
4898 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004899 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004900 case -NFS4ERR_EXPIRED:
4901 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004902 };
4903}
4904
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004905static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004906{
4907 struct nfs4_lockdata *data;
4908 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4911 .rpc_cred = state->owner->so_cred,
4912 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004913 struct rpc_task_setup task_setup_data = {
4914 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004915 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004916 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004917 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004918 .flags = RPC_TASK_ASYNC,
4919 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004920 int ret;
4921
Harvey Harrison3110ff82008-05-02 13:42:44 -07004922 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004923 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004924 fl->fl_u.nfs4_fl.owner,
4925 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926 if (data == NULL)
4927 return -ENOMEM;
4928 if (IS_SETLKW(cmd))
4929 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004930 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004931 msg.rpc_argp = &data->arg;
4932 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004933 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004934 if (recovery_type > NFS_LOCK_NEW) {
4935 if (recovery_type == NFS_LOCK_RECLAIM)
4936 data->arg.reclaim = NFS_LOCK_RECLAIM;
4937 nfs4_set_sequence_privileged(&data->arg.seq_args);
4938 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004939 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004940 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004941 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004942 ret = nfs4_wait_for_completion_rpc_task(task);
4943 if (ret == 0) {
4944 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004945 if (ret)
4946 nfs4_handle_setlk_error(data->server, data->lsp,
4947 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004948 } else
4949 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004950 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004951 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953}
4954
4955static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4956{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004957 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004958 struct nfs4_exception exception = {
4959 .inode = state->inode,
4960 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004961 int err;
4962
4963 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004964 /* Cache the lock if possible... */
4965 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4966 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004967 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004968 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004969 break;
4970 nfs4_handle_exception(server, err, &exception);
4971 } while (exception.retry);
4972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
4975static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4976{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004977 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004978 struct nfs4_exception exception = {
4979 .inode = state->inode,
4980 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004981 int err;
4982
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004983 err = nfs4_set_lock_state(state, request);
4984 if (err != 0)
4985 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004986 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004987 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4988 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004989 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004990 switch (err) {
4991 default:
4992 goto out;
4993 case -NFS4ERR_GRACE:
4994 case -NFS4ERR_DELAY:
4995 nfs4_handle_exception(server, err, &exception);
4996 err = 0;
4997 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004998 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004999out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005003#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005004/**
5005 * nfs41_check_expired_locks - possibly free a lock stateid
5006 *
5007 * @state: NFSv4 state for an inode
5008 *
5009 * Returns NFS_OK if recovery for this stateid is now finished.
5010 * Otherwise a negative NFS4ERR value is returned.
5011 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005012static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005013{
Chuck Levereb64cf92012-07-11 16:30:05 -04005014 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005015 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005016 struct nfs_server *server = NFS_SERVER(state->inode);
5017
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005018 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005019 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005020 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5021 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005022 /* Free the stateid unless the server
5023 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005024 if (status != -NFS4ERR_BAD_STATEID)
5025 nfs41_free_stateid(server,
5026 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005027 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005028 ret = status;
5029 }
5030 }
5031 };
5032
5033 return ret;
5034}
5035
5036static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5037{
5038 int status = NFS_OK;
5039
5040 if (test_bit(LK_STATE_IN_USE, &state->flags))
5041 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005042 if (status != NFS_OK)
5043 status = nfs4_lock_expired(state, request);
5044 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005045}
5046#endif
5047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5049{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005050 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005051 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005052 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005053 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005054 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005056 if ((fl_flags & FL_POSIX) &&
5057 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5058 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005059 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005060 status = nfs4_set_lock_state(state, request);
5061 if (status != 0)
5062 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005063 request->fl_flags |= FL_ACCESS;
5064 status = do_vfs_lock(request->fl_file, request);
5065 if (status < 0)
5066 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005067 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005068 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005069 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005070 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005071 request->fl_flags = fl_flags & ~FL_SLEEP;
5072 status = do_vfs_lock(request->fl_file, request);
5073 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005074 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005075 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5076 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005077 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005078 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005079 goto out;
5080 down_read(&nfsi->rwsem);
5081 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5082 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005083 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005084 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005085 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005086 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005087 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005088 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5089 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005090out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005091 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005092out:
5093 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 return status;
5095}
5096
5097static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5098{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005099 struct nfs4_exception exception = {
5100 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005101 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005102 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int err;
5104
5105 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005106 err = _nfs4_proc_setlk(state, cmd, request);
5107 if (err == -NFS4ERR_DENIED)
5108 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005110 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 } while (exception.retry);
5112 return err;
5113}
5114
5115static int
5116nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5117{
5118 struct nfs_open_context *ctx;
5119 struct nfs4_state *state;
5120 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5121 int status;
5122
5123 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005124 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 state = ctx->state;
5126
5127 if (request->fl_start < 0 || request->fl_end < 0)
5128 return -EINVAL;
5129
Trond Myklebustd9531262009-07-21 19:22:38 -04005130 if (IS_GETLK(cmd)) {
5131 if (state != NULL)
5132 return nfs4_proc_getlk(state, F_GETLK, request);
5133 return 0;
5134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5137 return -EINVAL;
5138
Trond Myklebustd9531262009-07-21 19:22:38 -04005139 if (request->fl_type == F_UNLCK) {
5140 if (state != NULL)
5141 return nfs4_proc_unlck(state, cmd, request);
5142 return 0;
5143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Trond Myklebustd9531262009-07-21 19:22:38 -04005145 if (state == NULL)
5146 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005147 /*
5148 * Don't rely on the VFS having checked the file open mode,
5149 * since it won't do this for flock() locks.
5150 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005151 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005152 case F_RDLCK:
5153 if (!(filp->f_mode & FMODE_READ))
5154 return -EBADF;
5155 break;
5156 case F_WRLCK:
5157 if (!(filp->f_mode & FMODE_WRITE))
5158 return -EBADF;
5159 }
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 do {
5162 status = nfs4_proc_setlk(state, cmd, request);
5163 if ((status != -EAGAIN) || IS_SETLK(cmd))
5164 break;
5165 timeout = nfs4_set_lock_task_retry(timeout);
5166 status = -ERESTARTSYS;
5167 if (signalled())
5168 break;
5169 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 return status;
5171}
5172
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005173int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005174{
5175 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005176 int err;
5177
5178 err = nfs4_set_lock_state(state, fl);
5179 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005180 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005181 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005182 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005183}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005184
Trond Myklebustcf470c32012-03-07 13:49:12 -05005185struct nfs_release_lockowner_data {
5186 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005187 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005188 struct nfs_release_lockowner_args args;
5189};
5190
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005191static void nfs4_release_lockowner_release(void *calldata)
5192{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005193 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005194 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005195 kfree(calldata);
5196}
5197
Trond Myklebust17280172012-03-11 13:11:00 -04005198static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005199 .rpc_release = nfs4_release_lockowner_release,
5200};
5201
Trond Myklebustcf470c32012-03-07 13:49:12 -05005202int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005203{
5204 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005205 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005206 struct rpc_message msg = {
5207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5208 };
5209
5210 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005211 return -EINVAL;
5212 data = kmalloc(sizeof(*data), GFP_NOFS);
5213 if (!data)
5214 return -ENOMEM;
5215 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005216 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005217 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5218 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5219 data->args.lock_owner.s_dev = server->s_dev;
5220 msg.rpc_argp = &data->args;
5221 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5222 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005223}
5224
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005225#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5226
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005227static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5228 const void *buf, size_t buflen,
5229 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005230{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005231 if (strcmp(key, "") != 0)
5232 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005233
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005234 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005235}
5236
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005237static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5238 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005239{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005240 if (strcmp(key, "") != 0)
5241 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005242
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005243 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005244}
5245
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005246static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5247 size_t list_len, const char *name,
5248 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005249{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005250 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005251
J. Bruce Fields096455a2006-03-20 23:23:42 -05005252 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5253 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005254
5255 if (list && len <= list_len)
5256 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005257 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005258}
5259
Andy Adamson533eb462011-06-13 18:25:56 -04005260/*
5261 * nfs_fhget will use either the mounted_on_fileid or the fileid
5262 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005263static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5264{
Andy Adamson533eb462011-06-13 18:25:56 -04005265 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5266 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5267 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005268 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005269 return;
5270
5271 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005272 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005273 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5274 fattr->nlink = 2;
5275}
5276
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005277static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5278 const struct qstr *name,
5279 struct nfs4_fs_locations *fs_locations,
5280 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005281{
5282 struct nfs_server *server = NFS_SERVER(dir);
5283 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005284 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005285 };
5286 struct nfs4_fs_locations_arg args = {
5287 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005288 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005289 .page = page,
5290 .bitmask = bitmask,
5291 };
Benny Halevy22958462009-04-01 09:22:02 -04005292 struct nfs4_fs_locations_res res = {
5293 .fs_locations = fs_locations,
5294 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005295 struct rpc_message msg = {
5296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5297 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005298 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005299 };
5300 int status;
5301
Harvey Harrison3110ff82008-05-02 13:42:44 -07005302 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005303
5304 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5305 * is not supported */
5306 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5307 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5308 else
5309 bitmask[0] |= FATTR4_WORD0_FILEID;
5310
Trond Myklebustc228fd32007-01-13 02:28:11 -05005311 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005312 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005313 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005314 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005315 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005316 return status;
5317}
5318
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005319int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5320 const struct qstr *name,
5321 struct nfs4_fs_locations *fs_locations,
5322 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005323{
5324 struct nfs4_exception exception = { };
5325 int err;
5326 do {
5327 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005328 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005329 &exception);
5330 } while (exception.retry);
5331 return err;
5332}
5333
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005334static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5335{
5336 int status;
5337 struct nfs4_secinfo_arg args = {
5338 .dir_fh = NFS_FH(dir),
5339 .name = name,
5340 };
5341 struct nfs4_secinfo_res res = {
5342 .flavors = flavors,
5343 };
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5346 .rpc_argp = &args,
5347 .rpc_resp = &res,
5348 };
5349
5350 dprintk("NFS call secinfo %s\n", name->name);
5351 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5352 dprintk("NFS reply secinfo: %d\n", status);
5353 return status;
5354}
5355
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005356int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5357 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005358{
5359 struct nfs4_exception exception = { };
5360 int err;
5361 do {
5362 err = nfs4_handle_exception(NFS_SERVER(dir),
5363 _nfs4_proc_secinfo(dir, name, flavors),
5364 &exception);
5365 } while (exception.retry);
5366 return err;
5367}
5368
Andy Adamson557134a2009-04-01 09:21:53 -04005369#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005370/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005371 * Check the exchange flags returned by the server for invalid flags, having
5372 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5373 * DS flags set.
5374 */
5375static int nfs4_check_cl_exchange_flags(u32 flags)
5376{
5377 if (flags & ~EXCHGID4_FLAG_MASK_R)
5378 goto out_inval;
5379 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5380 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5381 goto out_inval;
5382 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5383 goto out_inval;
5384 return NFS_OK;
5385out_inval:
5386 return -NFS4ERR_INVAL;
5387}
5388
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005389static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005390nfs41_same_server_scope(struct nfs41_server_scope *a,
5391 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005392{
5393 if (a->server_scope_sz == b->server_scope_sz &&
5394 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5395 return true;
5396
5397 return false;
5398}
5399
Andy Adamson357f54d2010-12-14 10:11:57 -05005400/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005401 * nfs4_proc_bind_conn_to_session()
5402 *
5403 * The 4.1 client currently uses the same TCP connection for the
5404 * fore and backchannel.
5405 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005406int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005407{
5408 int status;
5409 struct nfs41_bind_conn_to_session_res res;
5410 struct rpc_message msg = {
5411 .rpc_proc =
5412 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5413 .rpc_argp = clp,
5414 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005415 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005416 };
5417
5418 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005419
5420 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5421 if (unlikely(res.session == NULL)) {
5422 status = -ENOMEM;
5423 goto out;
5424 }
5425
5426 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5427 if (status == 0) {
5428 if (memcmp(res.session->sess_id.data,
5429 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5430 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5431 status = -EIO;
5432 goto out_session;
5433 }
5434 if (res.dir != NFS4_CDFS4_BOTH) {
5435 dprintk("NFS: %s: Unexpected direction from server\n",
5436 __func__);
5437 status = -EIO;
5438 goto out_session;
5439 }
5440 if (res.use_conn_in_rdma_mode) {
5441 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5442 __func__);
5443 status = -EIO;
5444 goto out_session;
5445 }
5446 }
5447out_session:
5448 kfree(res.session);
5449out:
5450 dprintk("<-- %s status= %d\n", __func__, status);
5451 return status;
5452}
5453
5454/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005455 * nfs4_proc_exchange_id()
5456 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005457 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5458 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005459 * Since the clientid has expired, all compounds using sessions
5460 * associated with the stale clientid will be returning
5461 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5462 * be in some phase of session reset.
5463 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005464int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005465{
5466 nfs4_verifier verifier;
5467 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005468 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005470 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005471 };
5472 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005473 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005474 };
5475 int status;
5476 struct rpc_message msg = {
5477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5478 .rpc_argp = &args,
5479 .rpc_resp = &res,
5480 .rpc_cred = cred,
5481 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005482
Chuck Leverf0920752012-05-21 22:45:41 -04005483 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005484 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5485 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005486 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5487 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5488 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005489
Chuck Leveracdeb692012-05-21 22:46:16 -04005490 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005491 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005492 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005493 status = -ENOMEM;
5494 goto out;
5495 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005496
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005497 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005498 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005499 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005500 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005501 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005502 }
5503
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005504 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005505 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005506 status = -ENOMEM;
5507 goto out_server_scope;
5508 }
5509
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005510 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005511 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005512 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005513
Chuck Lever177313f2012-05-21 22:44:58 -04005514 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005515 clp->cl_clientid = res.clientid;
5516 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5517 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5518 clp->cl_seqid = res.seqid;
5519
Chuck Leveracdeb692012-05-21 22:46:16 -04005520 kfree(clp->cl_serverowner);
5521 clp->cl_serverowner = res.server_owner;
5522 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005523
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005524 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005525 kfree(clp->cl_implid);
5526 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005527
Chuck Lever177313f2012-05-21 22:44:58 -04005528 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005529 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005530 res.server_scope)) {
5531 dprintk("%s: server_scope mismatch detected\n",
5532 __func__);
5533 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005534 kfree(clp->cl_serverscope);
5535 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005536 }
5537
Chuck Lever177313f2012-05-21 22:44:58 -04005538 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005539 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005540 goto out;
5541 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005542 } else
5543 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005544
Chuck Leveracdeb692012-05-21 22:46:16 -04005545out_server_owner:
5546 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005547out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005548 kfree(res.server_scope);
5549out:
Chuck Lever177313f2012-05-21 22:44:58 -04005550 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005552 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005553 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005554 clp->cl_implid->date.seconds,
5555 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005556 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005557 return status;
5558}
5559
Trond Myklebust66245532012-05-25 17:18:09 -04005560static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5561 struct rpc_cred *cred)
5562{
5563 struct rpc_message msg = {
5564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5565 .rpc_argp = clp,
5566 .rpc_cred = cred,
5567 };
5568 int status;
5569
5570 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5571 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005572 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005573 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5574 return status;
5575}
5576
5577static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5578 struct rpc_cred *cred)
5579{
5580 unsigned int loop;
5581 int ret;
5582
5583 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5584 ret = _nfs4_proc_destroy_clientid(clp, cred);
5585 switch (ret) {
5586 case -NFS4ERR_DELAY:
5587 case -NFS4ERR_CLIENTID_BUSY:
5588 ssleep(1);
5589 break;
5590 default:
5591 return ret;
5592 }
5593 }
5594 return 0;
5595}
5596
5597int nfs4_destroy_clientid(struct nfs_client *clp)
5598{
5599 struct rpc_cred *cred;
5600 int ret = 0;
5601
5602 if (clp->cl_mvops->minor_version < 1)
5603 goto out;
5604 if (clp->cl_exchange_flags == 0)
5605 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005606 if (clp->cl_preserve_clid)
5607 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005608 cred = nfs4_get_exchange_id_cred(clp);
5609 ret = nfs4_proc_destroy_clientid(clp, cred);
5610 if (cred)
5611 put_rpccred(cred);
5612 switch (ret) {
5613 case 0:
5614 case -NFS4ERR_STALE_CLIENTID:
5615 clp->cl_exchange_flags = 0;
5616 }
5617out:
5618 return ret;
5619}
5620
Andy Adamson2050f0c2009-04-01 09:22:30 -04005621struct nfs4_get_lease_time_data {
5622 struct nfs4_get_lease_time_args *args;
5623 struct nfs4_get_lease_time_res *res;
5624 struct nfs_client *clp;
5625};
5626
5627static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5628 void *calldata)
5629{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005630 struct nfs4_get_lease_time_data *data =
5631 (struct nfs4_get_lease_time_data *)calldata;
5632
5633 dprintk("--> %s\n", __func__);
5634 /* just setup sequence, do not trigger session recovery
5635 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005636 nfs41_setup_sequence(data->clp->cl_session,
5637 &data->args->la_seq_args,
5638 &data->res->lr_seq_res,
5639 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005640 dprintk("<-- %s\n", __func__);
5641}
5642
5643/*
5644 * Called from nfs4_state_manager thread for session setup, so don't recover
5645 * from sequence operation or clientid errors.
5646 */
5647static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5648{
5649 struct nfs4_get_lease_time_data *data =
5650 (struct nfs4_get_lease_time_data *)calldata;
5651
5652 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005653 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5654 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005655 switch (task->tk_status) {
5656 case -NFS4ERR_DELAY:
5657 case -NFS4ERR_GRACE:
5658 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5659 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5660 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005661 /* fall through */
5662 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005663 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005664 return;
5665 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005666 dprintk("<-- %s\n", __func__);
5667}
5668
Trond Myklebust17280172012-03-11 13:11:00 -04005669static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005670 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5671 .rpc_call_done = nfs4_get_lease_time_done,
5672};
5673
5674int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5675{
5676 struct rpc_task *task;
5677 struct nfs4_get_lease_time_args args;
5678 struct nfs4_get_lease_time_res res = {
5679 .lr_fsinfo = fsinfo,
5680 };
5681 struct nfs4_get_lease_time_data data = {
5682 .args = &args,
5683 .res = &res,
5684 .clp = clp,
5685 };
5686 struct rpc_message msg = {
5687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5688 .rpc_argp = &args,
5689 .rpc_resp = &res,
5690 };
5691 struct rpc_task_setup task_setup = {
5692 .rpc_client = clp->cl_rpcclient,
5693 .rpc_message = &msg,
5694 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005695 .callback_data = &data,
5696 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005697 };
5698 int status;
5699
Trond Myklebust9d12b212012-01-17 22:04:25 -05005700 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005701 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005702 dprintk("--> %s\n", __func__);
5703 task = rpc_run_task(&task_setup);
5704
5705 if (IS_ERR(task))
5706 status = PTR_ERR(task);
5707 else {
5708 status = task->tk_status;
5709 rpc_put_task(task);
5710 }
5711 dprintk("<-- %s return %d\n", __func__, status);
5712
5713 return status;
5714}
5715
Andy Adamsonfc931582009-04-01 09:22:31 -04005716/*
5717 * Initialize the values to be used by the client in CREATE_SESSION
5718 * If nfs4_init_session set the fore channel request and response sizes,
5719 * use them.
5720 *
5721 * Set the back channel max_resp_sz_cached to zero to force the client to
5722 * always set csa_cachethis to FALSE because the current implementation
5723 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5724 */
5725static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5726{
5727 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005728 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5729 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005730
5731 if (mxrqst_sz == 0)
5732 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5733 if (mxresp_sz == 0)
5734 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5735 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005736 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5737 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005738 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005739 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005740
5741 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005742 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005743 __func__,
5744 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005745 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005746
5747 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005748 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5749 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5750 args->bc_attrs.max_resp_sz_cached = 0;
5751 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5752 args->bc_attrs.max_reqs = 1;
5753
5754 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5755 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5756 __func__,
5757 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5758 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5759 args->bc_attrs.max_reqs);
5760}
5761
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005762static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005763{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005764 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5765 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5766
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005767 if (rcvd->max_resp_sz > sent->max_resp_sz)
5768 return -EINVAL;
5769 /*
5770 * Our requested max_ops is the minimum we need; we're not
5771 * prepared to break up compounds into smaller pieces than that.
5772 * So, no point even trying to continue if the server won't
5773 * cooperate:
5774 */
5775 if (rcvd->max_ops < sent->max_ops)
5776 return -EINVAL;
5777 if (rcvd->max_reqs == 0)
5778 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005779 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5780 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005781 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005782}
5783
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005784static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5785{
5786 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5787 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005788
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005789 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5790 return -EINVAL;
5791 if (rcvd->max_resp_sz < sent->max_resp_sz)
5792 return -EINVAL;
5793 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5794 return -EINVAL;
5795 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005796 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005797 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005798 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005799 return -EINVAL;
5800 return 0;
5801}
Andy Adamson8d353012009-04-01 09:22:32 -04005802
Andy Adamson8d353012009-04-01 09:22:32 -04005803static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5804 struct nfs4_session *session)
5805{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005806 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005807
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005808 ret = nfs4_verify_fore_channel_attrs(args, session);
5809 if (ret)
5810 return ret;
5811 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005812}
5813
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005814static int _nfs4_proc_create_session(struct nfs_client *clp,
5815 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005816{
5817 struct nfs4_session *session = clp->cl_session;
5818 struct nfs41_create_session_args args = {
5819 .client = clp,
5820 .cb_program = NFS4_CALLBACK,
5821 };
5822 struct nfs41_create_session_res res = {
5823 .client = clp,
5824 };
5825 struct rpc_message msg = {
5826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5827 .rpc_argp = &args,
5828 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005829 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005830 };
5831 int status;
5832
5833 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005834 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005835
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005836 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005837
Trond Myklebust43095d32012-11-20 11:13:12 -05005838 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005839 /* Verify the session's negotiated channel_attrs values */
5840 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005841 /* Increment the clientid slot sequence id */
5842 clp->cl_seqid++;
5843 }
5844
5845 return status;
5846}
5847
5848/*
5849 * Issues a CREATE_SESSION operation to the server.
5850 * It is the responsibility of the caller to verify the session is
5851 * expired before calling this routine.
5852 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005853int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005854{
5855 int status;
5856 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005857 struct nfs4_session *session = clp->cl_session;
5858
5859 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5860
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005861 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005862 if (status)
5863 goto out;
5864
Andy Adamsonaacd5532011-11-09 13:58:21 -05005865 /* Init or reset the session slot tables */
5866 status = nfs4_setup_session_slot_tables(session);
5867 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005868 if (status)
5869 goto out;
5870
5871 ptr = (unsigned *)&session->sess_id.data[0];
5872 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5873 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005874out:
5875 dprintk("<-- %s\n", __func__);
5876 return status;
5877}
5878
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005879/*
5880 * Issue the over-the-wire RPC DESTROY_SESSION.
5881 * The caller must serialize access to this routine.
5882 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005883int nfs4_proc_destroy_session(struct nfs4_session *session,
5884 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005885{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005886 struct rpc_message msg = {
5887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5888 .rpc_argp = session,
5889 .rpc_cred = cred,
5890 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005891 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005892
5893 dprintk("--> nfs4_proc_destroy_session\n");
5894
5895 /* session is still being setup */
5896 if (session->clp->cl_cons_state != NFS_CS_READY)
5897 return status;
5898
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005899 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005900
5901 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005902 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005903 "Session has been destroyed regardless...\n", status);
5904
5905 dprintk("<-- nfs4_proc_destroy_session\n");
5906 return status;
5907}
5908
Trond Myklebust7b38c362012-05-23 13:23:31 -04005909/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005910 * Renew the cl_session lease.
5911 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005912struct nfs4_sequence_data {
5913 struct nfs_client *clp;
5914 struct nfs4_sequence_args args;
5915 struct nfs4_sequence_res res;
5916};
5917
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005918static void nfs41_sequence_release(void *data)
5919{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005920 struct nfs4_sequence_data *calldata = data;
5921 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005922
Alexandros Batsakis71358402010-02-05 03:45:05 -08005923 if (atomic_read(&clp->cl_count) > 1)
5924 nfs4_schedule_state_renewal(clp);
5925 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005926 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005927}
5928
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005929static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5930{
5931 switch(task->tk_status) {
5932 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005933 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5934 return -EAGAIN;
5935 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005936 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005937 }
5938 return 0;
5939}
5940
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005941static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005942{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005943 struct nfs4_sequence_data *calldata = data;
5944 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005945
Trond Myklebust14516c32010-07-31 14:29:06 -04005946 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5947 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005948
5949 if (task->tk_status < 0) {
5950 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005951 if (atomic_read(&clp->cl_count) == 1)
5952 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005953
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005954 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5955 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956 return;
5957 }
5958 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005959 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005960out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005961 dprintk("<-- %s\n", __func__);
5962}
5963
5964static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5965{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005966 struct nfs4_sequence_data *calldata = data;
5967 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005968 struct nfs4_sequence_args *args;
5969 struct nfs4_sequence_res *res;
5970
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005971 args = task->tk_msg.rpc_argp;
5972 res = task->tk_msg.rpc_resp;
5973
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005974 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005975}
5976
5977static const struct rpc_call_ops nfs41_sequence_ops = {
5978 .rpc_call_done = nfs41_sequence_call_done,
5979 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005980 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981};
5982
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005983static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5984 struct rpc_cred *cred,
5985 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005986{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005987 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5990 .rpc_cred = cred,
5991 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005992 struct rpc_task_setup task_setup_data = {
5993 .rpc_client = clp->cl_rpcclient,
5994 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005995 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005996 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005998
Alexandros Batsakis71358402010-02-05 03:45:05 -08005999 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006000 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006001 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006002 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006003 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006004 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006005 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006006 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006007 if (is_privileged)
6008 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006009 msg.rpc_argp = &calldata->args;
6010 msg.rpc_resp = &calldata->res;
6011 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006012 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006013
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006014 return rpc_run_task(&task_setup_data);
6015}
6016
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006017static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018{
6019 struct rpc_task *task;
6020 int ret = 0;
6021
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006022 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6023 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006024 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006025 if (IS_ERR(task))
6026 ret = PTR_ERR(task);
6027 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006028 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006029 dprintk("<-- %s status=%d\n", __func__, ret);
6030 return ret;
6031}
6032
6033static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6034{
6035 struct rpc_task *task;
6036 int ret;
6037
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006038 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006039 if (IS_ERR(task)) {
6040 ret = PTR_ERR(task);
6041 goto out;
6042 }
6043 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006044 if (!ret) {
6045 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6046
6047 if (task->tk_status == 0)
6048 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006049 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006050 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006051 rpc_put_task(task);
6052out:
6053 dprintk("<-- %s status=%d\n", __func__, ret);
6054 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006055}
6056
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006057struct nfs4_reclaim_complete_data {
6058 struct nfs_client *clp;
6059 struct nfs41_reclaim_complete_args arg;
6060 struct nfs41_reclaim_complete_res res;
6061};
6062
6063static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6064{
6065 struct nfs4_reclaim_complete_data *calldata = data;
6066
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006067 nfs41_setup_sequence(calldata->clp->cl_session,
6068 &calldata->arg.seq_args,
6069 &calldata->res.seq_res,
6070 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006071}
6072
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006073static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6074{
6075 switch(task->tk_status) {
6076 case 0:
6077 case -NFS4ERR_COMPLETE_ALREADY:
6078 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6079 break;
6080 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006081 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006082 /* fall through */
6083 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006084 return -EAGAIN;
6085 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006086 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006087 }
6088 return 0;
6089}
6090
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006091static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6092{
6093 struct nfs4_reclaim_complete_data *calldata = data;
6094 struct nfs_client *clp = calldata->clp;
6095 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6096
6097 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006098 if (!nfs41_sequence_done(task, res))
6099 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006100
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006101 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6102 rpc_restart_call_prepare(task);
6103 return;
6104 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006105 dprintk("<-- %s\n", __func__);
6106}
6107
6108static void nfs4_free_reclaim_complete_data(void *data)
6109{
6110 struct nfs4_reclaim_complete_data *calldata = data;
6111
6112 kfree(calldata);
6113}
6114
6115static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6116 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6117 .rpc_call_done = nfs4_reclaim_complete_done,
6118 .rpc_release = nfs4_free_reclaim_complete_data,
6119};
6120
6121/*
6122 * Issue a global reclaim complete.
6123 */
6124static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6125{
6126 struct nfs4_reclaim_complete_data *calldata;
6127 struct rpc_task *task;
6128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6130 };
6131 struct rpc_task_setup task_setup_data = {
6132 .rpc_client = clp->cl_rpcclient,
6133 .rpc_message = &msg,
6134 .callback_ops = &nfs4_reclaim_complete_call_ops,
6135 .flags = RPC_TASK_ASYNC,
6136 };
6137 int status = -ENOMEM;
6138
6139 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006140 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006141 if (calldata == NULL)
6142 goto out;
6143 calldata->clp = clp;
6144 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006145
Trond Myklebust9d12b212012-01-17 22:04:25 -05006146 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006147 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006148 msg.rpc_argp = &calldata->arg;
6149 msg.rpc_resp = &calldata->res;
6150 task_setup_data.callback_data = calldata;
6151 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006152 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006153 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006154 goto out;
6155 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006156 status = nfs4_wait_for_completion_rpc_task(task);
6157 if (status == 0)
6158 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006160 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006161out:
6162 dprintk("<-- %s status=%d\n", __func__, status);
6163 return status;
6164}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006165
6166static void
6167nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6168{
6169 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006170 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006171 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172
6173 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006174 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6175 * right now covering the LAYOUTGET we are about to send.
6176 * However, that is not so catastrophic, and there seems
6177 * to be no way to prevent it completely.
6178 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006179 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006180 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006181 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006182 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6183 NFS_I(lgp->args.inode)->layout,
6184 lgp->args.ctx->state)) {
6185 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006186 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006187}
6188
6189static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6190{
6191 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006192 struct inode *inode = lgp->args.inode;
6193 struct nfs_server *server = NFS_SERVER(inode);
6194 struct pnfs_layout_hdr *lo;
6195 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006196 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197
6198 dprintk("--> %s\n", __func__);
6199
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006200 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006201 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006202
6203 switch (task->tk_status) {
6204 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006205 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206 case -NFS4ERR_LAYOUTTRYLATER:
6207 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006208 timeo = rpc_get_timeout(task->tk_client);
6209 giveup = lgp->args.timestamp + timeo;
6210 if (time_after(giveup, jiffies))
6211 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006212 break;
6213 case -NFS4ERR_EXPIRED:
6214 case -NFS4ERR_BAD_STATEID:
6215 spin_lock(&inode->i_lock);
6216 lo = NFS_I(inode)->layout;
6217 if (!lo || list_empty(&lo->plh_segs)) {
6218 spin_unlock(&inode->i_lock);
6219 /* If the open stateid was bad, then recover it. */
6220 state = lgp->args.ctx->state;
6221 } else {
6222 LIST_HEAD(head);
6223
6224 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6225 spin_unlock(&inode->i_lock);
6226 /* Mark the bad layout state as invalid, then
6227 * retry using the open stateid. */
6228 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006229 }
6230 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006231 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6232 rpc_restart_call_prepare(task);
6233out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006234 dprintk("<-- %s\n", __func__);
6235}
6236
Idan Kedar85541162012-08-02 11:47:10 +03006237static size_t max_response_pages(struct nfs_server *server)
6238{
6239 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6240 return nfs_page_array_len(0, max_resp_sz);
6241}
6242
6243static void nfs4_free_pages(struct page **pages, size_t size)
6244{
6245 int i;
6246
6247 if (!pages)
6248 return;
6249
6250 for (i = 0; i < size; i++) {
6251 if (!pages[i])
6252 break;
6253 __free_page(pages[i]);
6254 }
6255 kfree(pages);
6256}
6257
6258static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6259{
6260 struct page **pages;
6261 int i;
6262
6263 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6264 if (!pages) {
6265 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6266 return NULL;
6267 }
6268
6269 for (i = 0; i < size; i++) {
6270 pages[i] = alloc_page(gfp_flags);
6271 if (!pages[i]) {
6272 dprintk("%s: failed to allocate page\n", __func__);
6273 nfs4_free_pages(pages, size);
6274 return NULL;
6275 }
6276 }
6277
6278 return pages;
6279}
6280
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006281static void nfs4_layoutget_release(void *calldata)
6282{
6283 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006284 struct inode *inode = lgp->args.inode;
6285 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006286 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006287
6288 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006289 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006290 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006291 put_nfs_open_context(lgp->args.ctx);
6292 kfree(calldata);
6293 dprintk("<-- %s\n", __func__);
6294}
6295
6296static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6297 .rpc_call_prepare = nfs4_layoutget_prepare,
6298 .rpc_call_done = nfs4_layoutget_done,
6299 .rpc_release = nfs4_layoutget_release,
6300};
6301
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006302struct pnfs_layout_segment *
6303nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006304{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006305 struct inode *inode = lgp->args.inode;
6306 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006307 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006308 struct rpc_task *task;
6309 struct rpc_message msg = {
6310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6311 .rpc_argp = &lgp->args,
6312 .rpc_resp = &lgp->res,
6313 };
6314 struct rpc_task_setup task_setup_data = {
6315 .rpc_client = server->client,
6316 .rpc_message = &msg,
6317 .callback_ops = &nfs4_layoutget_call_ops,
6318 .callback_data = lgp,
6319 .flags = RPC_TASK_ASYNC,
6320 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006321 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006322 int status = 0;
6323
6324 dprintk("--> %s\n", __func__);
6325
Idan Kedar85541162012-08-02 11:47:10 +03006326 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6327 if (!lgp->args.layout.pages) {
6328 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006329 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006330 }
6331 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006332 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006333
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006334 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006335 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006336 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006337
6338 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6339 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6340
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006341 task = rpc_run_task(&task_setup_data);
6342 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006343 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006344 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006345 if (status == 0)
6346 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006347 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6348 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006349 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 rpc_put_task(task);
6351 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006352 if (status)
6353 return ERR_PTR(status);
6354 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006355}
6356
Benny Halevycbe82602011-05-22 19:52:37 +03006357static void
6358nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6359{
6360 struct nfs4_layoutreturn *lrp = calldata;
6361
6362 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006363 nfs41_setup_sequence(lrp->clp->cl_session,
6364 &lrp->args.seq_args,
6365 &lrp->res.seq_res,
6366 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006367}
6368
6369static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6370{
6371 struct nfs4_layoutreturn *lrp = calldata;
6372 struct nfs_server *server;
6373
6374 dprintk("--> %s\n", __func__);
6375
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006376 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006377 return;
6378
6379 server = NFS_SERVER(lrp->args.inode);
6380 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006381 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006382 return;
6383 }
Benny Halevycbe82602011-05-22 19:52:37 +03006384 dprintk("<-- %s\n", __func__);
6385}
6386
6387static void nfs4_layoutreturn_release(void *calldata)
6388{
6389 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006390 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006391
6392 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006393 spin_lock(&lo->plh_inode->i_lock);
6394 if (lrp->res.lrs_present)
6395 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6396 lo->plh_block_lgets--;
6397 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006398 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006399 kfree(calldata);
6400 dprintk("<-- %s\n", __func__);
6401}
6402
6403static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6404 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6405 .rpc_call_done = nfs4_layoutreturn_done,
6406 .rpc_release = nfs4_layoutreturn_release,
6407};
6408
6409int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6410{
6411 struct rpc_task *task;
6412 struct rpc_message msg = {
6413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6414 .rpc_argp = &lrp->args,
6415 .rpc_resp = &lrp->res,
6416 };
6417 struct rpc_task_setup task_setup_data = {
6418 .rpc_client = lrp->clp->cl_rpcclient,
6419 .rpc_message = &msg,
6420 .callback_ops = &nfs4_layoutreturn_call_ops,
6421 .callback_data = lrp,
6422 };
6423 int status;
6424
6425 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006426 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006427 task = rpc_run_task(&task_setup_data);
6428 if (IS_ERR(task))
6429 return PTR_ERR(task);
6430 status = task->tk_status;
6431 dprintk("<-- %s status=%d\n", __func__, status);
6432 rpc_put_task(task);
6433 return status;
6434}
6435
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006436/*
6437 * Retrieve the list of Data Server devices from the MDS.
6438 */
6439static int _nfs4_getdevicelist(struct nfs_server *server,
6440 const struct nfs_fh *fh,
6441 struct pnfs_devicelist *devlist)
6442{
6443 struct nfs4_getdevicelist_args args = {
6444 .fh = fh,
6445 .layoutclass = server->pnfs_curr_ld->id,
6446 };
6447 struct nfs4_getdevicelist_res res = {
6448 .devlist = devlist,
6449 };
6450 struct rpc_message msg = {
6451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6452 .rpc_argp = &args,
6453 .rpc_resp = &res,
6454 };
6455 int status;
6456
6457 dprintk("--> %s\n", __func__);
6458 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6459 &res.seq_res, 0);
6460 dprintk("<-- %s status=%d\n", __func__, status);
6461 return status;
6462}
6463
6464int nfs4_proc_getdevicelist(struct nfs_server *server,
6465 const struct nfs_fh *fh,
6466 struct pnfs_devicelist *devlist)
6467{
6468 struct nfs4_exception exception = { };
6469 int err;
6470
6471 do {
6472 err = nfs4_handle_exception(server,
6473 _nfs4_getdevicelist(server, fh, devlist),
6474 &exception);
6475 } while (exception.retry);
6476
6477 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6478 err, devlist->num_devs);
6479
6480 return err;
6481}
6482EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6483
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006484static int
6485_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6486{
6487 struct nfs4_getdeviceinfo_args args = {
6488 .pdev = pdev,
6489 };
6490 struct nfs4_getdeviceinfo_res res = {
6491 .pdev = pdev,
6492 };
6493 struct rpc_message msg = {
6494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6495 .rpc_argp = &args,
6496 .rpc_resp = &res,
6497 };
6498 int status;
6499
6500 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006501 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006502 dprintk("<-- %s status=%d\n", __func__, status);
6503
6504 return status;
6505}
6506
6507int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6508{
6509 struct nfs4_exception exception = { };
6510 int err;
6511
6512 do {
6513 err = nfs4_handle_exception(server,
6514 _nfs4_proc_getdeviceinfo(server, pdev),
6515 &exception);
6516 } while (exception.retry);
6517 return err;
6518}
6519EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6520
Andy Adamson863a3c62011-03-23 13:27:54 +00006521static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6522{
6523 struct nfs4_layoutcommit_data *data = calldata;
6524 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006525 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006526
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006527 nfs41_setup_sequence(session,
6528 &data->args.seq_args,
6529 &data->res.seq_res,
6530 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006531}
6532
6533static void
6534nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6535{
6536 struct nfs4_layoutcommit_data *data = calldata;
6537 struct nfs_server *server = NFS_SERVER(data->args.inode);
6538
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006539 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006540 return;
6541
6542 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006543 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6544 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6545 case -NFS4ERR_BADLAYOUT: /* no layout */
6546 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006547 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006548 break;
6549 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006550 nfs_post_op_update_inode_force_wcc(data->args.inode,
6551 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006552 break;
6553 default:
6554 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6555 rpc_restart_call_prepare(task);
6556 return;
6557 }
6558 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006559}
6560
6561static void nfs4_layoutcommit_release(void *calldata)
6562{
6563 struct nfs4_layoutcommit_data *data = calldata;
6564
Andy Adamsondb29c082011-07-30 20:52:38 -04006565 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006566 put_rpccred(data->cred);
6567 kfree(data);
6568}
6569
6570static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6571 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6572 .rpc_call_done = nfs4_layoutcommit_done,
6573 .rpc_release = nfs4_layoutcommit_release,
6574};
6575
6576int
Andy Adamsonef311532011-03-12 02:58:10 -05006577nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006578{
6579 struct rpc_message msg = {
6580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6581 .rpc_argp = &data->args,
6582 .rpc_resp = &data->res,
6583 .rpc_cred = data->cred,
6584 };
6585 struct rpc_task_setup task_setup_data = {
6586 .task = &data->task,
6587 .rpc_client = NFS_CLIENT(data->args.inode),
6588 .rpc_message = &msg,
6589 .callback_ops = &nfs4_layoutcommit_ops,
6590 .callback_data = data,
6591 .flags = RPC_TASK_ASYNC,
6592 };
6593 struct rpc_task *task;
6594 int status = 0;
6595
6596 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6597 "lbw: %llu inode %lu\n",
6598 data->task.tk_pid, sync,
6599 data->args.lastbytewritten,
6600 data->args.inode->i_ino);
6601
Trond Myklebust9d12b212012-01-17 22:04:25 -05006602 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006603 task = rpc_run_task(&task_setup_data);
6604 if (IS_ERR(task))
6605 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006606 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006607 goto out;
6608 status = nfs4_wait_for_completion_rpc_task(task);
6609 if (status != 0)
6610 goto out;
6611 status = task->tk_status;
6612out:
6613 dprintk("%s: status %d\n", __func__, status);
6614 rpc_put_task(task);
6615 return status;
6616}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006617
6618static int
6619_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6620 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6621{
6622 struct nfs41_secinfo_no_name_args args = {
6623 .style = SECINFO_STYLE_CURRENT_FH,
6624 };
6625 struct nfs4_secinfo_res res = {
6626 .flavors = flavors,
6627 };
6628 struct rpc_message msg = {
6629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6630 .rpc_argp = &args,
6631 .rpc_resp = &res,
6632 };
6633 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6634}
6635
6636static int
6637nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6638 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6639{
6640 struct nfs4_exception exception = { };
6641 int err;
6642 do {
6643 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6644 switch (err) {
6645 case 0:
6646 case -NFS4ERR_WRONGSEC:
6647 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006648 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006649 default:
6650 err = nfs4_handle_exception(server, err, &exception);
6651 }
6652 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006653out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006654 return err;
6655}
6656
6657static int
6658nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6659 struct nfs_fsinfo *info)
6660{
6661 int err;
6662 struct page *page;
6663 rpc_authflavor_t flavor;
6664 struct nfs4_secinfo_flavors *flavors;
6665
6666 page = alloc_page(GFP_KERNEL);
6667 if (!page) {
6668 err = -ENOMEM;
6669 goto out;
6670 }
6671
6672 flavors = page_address(page);
6673 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6674
6675 /*
6676 * Fall back on "guess and check" method if
6677 * the server doesn't support SECINFO_NO_NAME
6678 */
6679 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6680 err = nfs4_find_root_sec(server, fhandle, info);
6681 goto out_freepage;
6682 }
6683 if (err)
6684 goto out_freepage;
6685
6686 flavor = nfs_find_best_sec(flavors);
6687 if (err == 0)
6688 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6689
6690out_freepage:
6691 put_page(page);
6692 if (err == -EACCES)
6693 return -EPERM;
6694out:
6695 return err;
6696}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006697
6698static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006699{
6700 int status;
6701 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006702 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006703 };
6704 struct nfs41_test_stateid_res res;
6705 struct rpc_message msg = {
6706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6707 .rpc_argp = &args,
6708 .rpc_resp = &res,
6709 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006710
Chuck Lever38527b12012-07-11 16:30:23 -04006711 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006712 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006713 nfs4_set_sequence_privileged(&args.seq_args);
6714 status = nfs4_call_sync_sequence(server->client, server, &msg,
6715 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006716 if (status != NFS_OK) {
6717 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006718 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006719 }
6720 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006721 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006722}
6723
Chuck Lever38527b12012-07-11 16:30:23 -04006724/**
6725 * nfs41_test_stateid - perform a TEST_STATEID operation
6726 *
6727 * @server: server / transport on which to perform the operation
6728 * @stateid: state ID to test
6729 *
6730 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6731 * Otherwise a negative NFS4ERR value is returned if the operation
6732 * failed or the state ID is not currently valid.
6733 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006734static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006735{
6736 struct nfs4_exception exception = { };
6737 int err;
6738 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006739 err = _nfs41_test_stateid(server, stateid);
6740 if (err != -NFS4ERR_DELAY)
6741 break;
6742 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006743 } while (exception.retry);
6744 return err;
6745}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006746
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006747static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006748{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006749 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006750 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006751 };
6752 struct nfs41_free_stateid_res res;
6753 struct rpc_message msg = {
6754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6755 .rpc_argp = &args,
6756 .rpc_resp = &res,
6757 };
Chuck Lever38527b12012-07-11 16:30:23 -04006758 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006759
Chuck Lever38527b12012-07-11 16:30:23 -04006760 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006761 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006762 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006763 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006764 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006765 dprintk("NFS reply free_stateid: %d\n", status);
6766 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006767}
6768
Chuck Lever38527b12012-07-11 16:30:23 -04006769/**
6770 * nfs41_free_stateid - perform a FREE_STATEID operation
6771 *
6772 * @server: server / transport on which to perform the operation
6773 * @stateid: state ID to release
6774 *
6775 * Returns NFS_OK if the server freed "stateid". Otherwise a
6776 * negative NFS4ERR value is returned.
6777 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006778static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006779{
6780 struct nfs4_exception exception = { };
6781 int err;
6782 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006783 err = _nfs4_free_stateid(server, stateid);
6784 if (err != -NFS4ERR_DELAY)
6785 break;
6786 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006787 } while (exception.retry);
6788 return err;
6789}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006790
6791static bool nfs41_match_stateid(const nfs4_stateid *s1,
6792 const nfs4_stateid *s2)
6793{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006794 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006795 return false;
6796
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006797 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006798 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006799 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006800 return true;
6801
6802 return false;
6803}
6804
Andy Adamson557134a2009-04-01 09:21:53 -04006805#endif /* CONFIG_NFS_V4_1 */
6806
Trond Myklebust36281ca2012-03-04 18:13:56 -05006807static bool nfs4_match_stateid(const nfs4_stateid *s1,
6808 const nfs4_stateid *s2)
6809{
Trond Myklebustf597c532012-03-04 18:13:56 -05006810 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006811}
6812
6813
Trond Myklebust17280172012-03-11 13:11:00 -04006814static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006815 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006816 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 .recover_open = nfs4_open_reclaim,
6818 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006819 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006820 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006821 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822};
6823
Andy Adamson591d71c2009-04-01 09:22:47 -04006824#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006825static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006826 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6827 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6828 .recover_open = nfs4_open_reclaim,
6829 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006830 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006831 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006832 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006833 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006834};
6835#endif /* CONFIG_NFS_V4_1 */
6836
Trond Myklebust17280172012-03-11 13:11:00 -04006837static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006838 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006839 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 .recover_open = nfs4_open_expired,
6841 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006842 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006843 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844};
6845
Andy Adamson591d71c2009-04-01 09:22:47 -04006846#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006847static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006848 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6849 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006850 .recover_open = nfs41_open_expired,
6851 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006852 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006853 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006854};
6855#endif /* CONFIG_NFS_V4_1 */
6856
Trond Myklebust17280172012-03-11 13:11:00 -04006857static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006858 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006859 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006860 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006861};
6862
6863#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006864static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006865 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006866 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006867 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006868};
6869#endif
6870
Trond Myklebust97dc1352010-06-16 09:52:26 -04006871static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6872 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006873 .init_caps = NFS_CAP_READDIRPLUS
6874 | NFS_CAP_ATOMIC_OPEN
6875 | NFS_CAP_CHANGE_ATTR
6876 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006877 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006878 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006879 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006880 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6881 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6882 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006883};
6884
6885#if defined(CONFIG_NFS_V4_1)
6886static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6887 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006888 .init_caps = NFS_CAP_READDIRPLUS
6889 | NFS_CAP_ATOMIC_OPEN
6890 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006891 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006892 | NFS_CAP_STATEID_NFSV41
6893 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006894 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006895 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006896 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006897 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6898 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6899 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006900};
6901#endif
6902
Trond Myklebust97dc1352010-06-16 09:52:26 -04006903const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6904 [0] = &nfs_v4_0_minor_ops,
6905#if defined(CONFIG_NFS_V4_1)
6906 [1] = &nfs_v4_1_minor_ops,
6907#endif
6908};
6909
Bryan Schumaker73a79702012-07-16 16:39:12 -04006910const struct inode_operations nfs4_dir_inode_operations = {
6911 .create = nfs_create,
6912 .lookup = nfs_lookup,
6913 .atomic_open = nfs_atomic_open,
6914 .link = nfs_link,
6915 .unlink = nfs_unlink,
6916 .symlink = nfs_symlink,
6917 .mkdir = nfs_mkdir,
6918 .rmdir = nfs_rmdir,
6919 .mknod = nfs_mknod,
6920 .rename = nfs_rename,
6921 .permission = nfs_permission,
6922 .getattr = nfs_getattr,
6923 .setattr = nfs_setattr,
6924 .getxattr = generic_getxattr,
6925 .setxattr = generic_setxattr,
6926 .listxattr = generic_listxattr,
6927 .removexattr = generic_removexattr,
6928};
6929
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006930static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006931 .permission = nfs_permission,
6932 .getattr = nfs_getattr,
6933 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006934 .getxattr = generic_getxattr,
6935 .setxattr = generic_setxattr,
6936 .listxattr = generic_listxattr,
6937 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006938};
6939
David Howells509de812006-08-22 20:06:11 -04006940const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 .version = 4, /* protocol version */
6942 .dentry_ops = &nfs4_dentry_operations,
6943 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006944 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006945 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006947 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006948 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 .getattr = nfs4_proc_getattr,
6950 .setattr = nfs4_proc_setattr,
6951 .lookup = nfs4_proc_lookup,
6952 .access = nfs4_proc_access,
6953 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 .create = nfs4_proc_create,
6955 .remove = nfs4_proc_remove,
6956 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006957 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 .unlink_done = nfs4_proc_unlink_done,
6959 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006960 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006961 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006962 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 .link = nfs4_proc_link,
6964 .symlink = nfs4_proc_symlink,
6965 .mkdir = nfs4_proc_mkdir,
6966 .rmdir = nfs4_proc_remove,
6967 .readdir = nfs4_proc_readdir,
6968 .mknod = nfs4_proc_mknod,
6969 .statfs = nfs4_proc_statfs,
6970 .fsinfo = nfs4_proc_fsinfo,
6971 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006972 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 .decode_dirent = nfs4_decode_dirent,
6974 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006975 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006976 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006977 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006979 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006980 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006981 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006983 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006984 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006986 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006987 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006988 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006989 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006990 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006991 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006992 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006993 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006994 .create_server = nfs4_create_server,
6995 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996};
6997
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006998static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6999 .prefix = XATTR_NAME_NFSV4_ACL,
7000 .list = nfs4_xattr_list_nfs4_acl,
7001 .get = nfs4_xattr_get_nfs4_acl,
7002 .set = nfs4_xattr_set_nfs4_acl,
7003};
7004
7005const struct xattr_handler *nfs4_xattr_handlers[] = {
7006 &nfs4_xattr_nfs4_acl_handler,
7007 NULL
7008};
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010/*
7011 * Local variables:
7012 * c-basic-offset: 8
7013 * End:
7014 */