blob: af05df3c7c79398d8b22df2924b0e9d7f0517392 [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 Myklebustdc0b0272008-12-23 15:21:56 -0500981 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 case FMODE_READ:
983 set_bit(NFS_O_RDONLY_STATE, &state->flags);
984 break;
985 case FMODE_WRITE:
986 set_bit(NFS_O_WRONLY_STATE, &state->flags);
987 break;
988 case FMODE_READ|FMODE_WRITE:
989 set_bit(NFS_O_RDWR_STATE, &state->flags);
990 }
991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400994{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400995 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static 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 -07001001{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001002 /*
1003 * Protect the call to nfs4_state_set_mode_locked and
1004 * serialise the stateid update
1005 */
1006 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001007 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001008 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 set_bit(NFS_DELEGATED_STATE, &state->flags);
1010 }
1011 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001013 write_sequnlock(&state->seqlock);
1014 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001016 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static 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 -05001020{
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *deleg_cur;
1023 int ret = 0;
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001026
1027 rcu_read_lock();
1028 deleg_cur = rcu_dereference(nfsi->delegation);
1029 if (deleg_cur == NULL)
1030 goto no_delegation;
1031
1032 spin_lock(&deleg_cur->lock);
1033 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001034 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001036 goto no_delegation_unlock;
1037
1038 if (delegation == NULL)
1039 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001040 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001041 goto no_delegation_unlock;
1042
Trond Myklebustb7391f42008-12-23 15:21:52 -05001043 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001045 ret = 1;
1046no_delegation_unlock:
1047 spin_unlock(&deleg_cur->lock);
1048no_delegation:
1049 rcu_read_unlock();
1050
1051 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001053 ret = 1;
1054 }
1055
1056 return ret;
1057}
1058
1059
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061{
1062 struct nfs_delegation *delegation;
1063
1064 rcu_read_lock();
1065 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001067 rcu_read_unlock();
1068 return;
1069 }
1070 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001071 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072}
1073
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075{
1076 struct nfs4_state *state = opendata->state;
1077 struct nfs_inode *nfsi = NFS_I(state->inode);
1078 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001079 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001080 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001081 nfs4_stateid stateid;
1082 int ret = -EAGAIN;
1083
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 if (can_open_cached(state, fmode, open_mode)) {
1088 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 goto out_return_state;
1091 }
1092 spin_unlock(&state->owner->so_lock);
1093 }
Trond Myklebust34310432008-12-23 15:21:38 -05001094 rcu_read_lock();
1095 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001096 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001097 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001098 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001099 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001101 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001103 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 if (ret != 0)
1105 goto out;
1106 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001107
1108 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001110 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112out:
1113 return ERR_PTR(ret);
1114out_return_state:
1115 atomic_inc(&state->count);
1116 return state;
1117}
1118
Andy Adamsone23008e2012-10-02 21:07:32 -04001119static void
1120nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1121{
1122 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1123 struct nfs_delegation *delegation;
1124 int delegation_flags = 0;
1125
1126 rcu_read_lock();
1127 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1128 if (delegation)
1129 delegation_flags = delegation->flags;
1130 rcu_read_unlock();
1131 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1132 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1133 "returning a delegation for "
1134 "OPEN(CLAIM_DELEGATE_CUR)\n",
1135 clp->cl_hostname);
1136 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1137 nfs_inode_set_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140 else
1141 nfs_inode_reclaim_delegation(state->inode,
1142 data->owner->so_cred,
1143 &data->o_res);
1144}
1145
1146/*
1147 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1148 * and update the nfs4_state.
1149 */
1150static struct nfs4_state *
1151_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1152{
1153 struct inode *inode = data->state->inode;
1154 struct nfs4_state *state = data->state;
1155 int ret;
1156
1157 if (!data->rpc_done) {
1158 ret = data->rpc_status;
1159 goto err;
1160 }
1161
1162 ret = -ESTALE;
1163 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1164 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1165 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1166 goto err;
1167
1168 ret = -ENOMEM;
1169 state = nfs4_get_open_state(inode, data->owner);
1170 if (state == NULL)
1171 goto err;
1172
1173 ret = nfs_refresh_inode(inode, &data->f_attr);
1174 if (ret)
1175 goto err;
1176
1177 if (data->o_res.delegation_type != 0)
1178 nfs4_opendata_check_deleg(data, state);
1179 update_open_stateid(state, &data->o_res.stateid, NULL,
1180 data->o_arg.fmode);
1181
1182 return state;
1183err:
1184 return ERR_PTR(ret);
1185
1186}
1187
1188static struct nfs4_state *
1189_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190{
1191 struct inode *inode;
1192 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001193 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001194
Trond Myklebustaac00a82007-07-05 19:02:21 -04001195 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001196 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001197 goto out;
1198 }
1199
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001201 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001203 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001204 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001205 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err;
1207 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001208 state = nfs4_get_open_state(inode, data->owner);
1209 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001211 if (data->o_res.delegation_type != 0)
1212 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001215 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001216out:
Trond Myklebust7aa262b2013-02-28 16:19:59 -08001217 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001218 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001219err_put_inode:
1220 iput(inode);
1221err:
1222 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001223}
1224
Andy Adamsone23008e2012-10-02 21:07:32 -04001225static struct nfs4_state *
1226nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1227{
1228 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1229 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1230 return _nfs4_opendata_to_nfs4_state(data);
1231}
1232
Trond Myklebust864472e2006-01-03 09:55:15 +01001233static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1234{
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_open_context *ctx;
1237
1238 spin_lock(&state->inode->i_lock);
1239 list_for_each_entry(ctx, &nfsi->open_files, list) {
1240 if (ctx->state != state)
1241 continue;
1242 get_nfs_open_context(ctx);
1243 spin_unlock(&state->inode->i_lock);
1244 return ctx;
1245 }
1246 spin_unlock(&state->inode->i_lock);
1247 return ERR_PTR(-ENOENT);
1248}
1249
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001250static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1251 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001252{
1253 struct nfs4_opendata *opendata;
1254
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1256 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001257 if (opendata == NULL)
1258 return ERR_PTR(-ENOMEM);
1259 opendata->state = state;
1260 atomic_inc(&state->count);
1261 return opendata;
1262}
1263
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001264static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001265{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001266 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
1268
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269 opendata->o_arg.open_flags = 0;
1270 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001271 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1272 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1273 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001274 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 if (ret != 0)
1276 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 if (IS_ERR(newstate))
1279 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001280 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 return 0;
1283}
1284
1285static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1286{
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 int ret;
1289
1290 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 smp_rmb();
1293 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001294 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001295 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 if (ret != 0)
1297 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001298 if (newstate != state)
1299 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 }
1301 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001302 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001303 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 if (ret != 0)
1305 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001306 if (newstate != state)
1307 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 }
1309 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001310 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 if (ret != 0)
1313 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001314 if (newstate != state)
1315 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001317 /*
1318 * We may have performed cached opens for all three recoveries.
1319 * Check if we need to update the current stateid.
1320 */
1321 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001322 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001323 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001324 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001325 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001326 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001327 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001328 return 0;
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/*
1332 * OPEN_RECLAIM:
1333 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001335static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001337 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001338 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int status;
1341
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001342 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1343 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001344 if (IS_ERR(opendata))
1345 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 rcu_read_lock();
1347 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001348 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001349 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001350 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001351 opendata->o_arg.u.delegation_type = delegation_type;
1352 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001353 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return status;
1355}
1356
Trond Myklebust539cd032007-06-05 11:46:42 -04001357static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct nfs_server *server = NFS_SERVER(state->inode);
1360 struct nfs4_exception exception = { };
1361 int err;
1362 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001363 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001364 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1365 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001366 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001367 break;
1368 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 } while (exception.retry);
1370 return err;
1371}
1372
Trond Myklebust864472e2006-01-03 09:55:15 +01001373static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1374{
1375 struct nfs_open_context *ctx;
1376 int ret;
1377
1378 ctx = nfs4_state_find_open_context(state);
1379 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001380 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001381 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001382 put_nfs_open_context(ctx);
1383 return ret;
1384}
1385
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001386static 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 -07001387{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001388 switch (err) {
1389 default:
1390 printk(KERN_ERR "NFS: %s: unhandled error "
1391 "%d.\n", __func__, err);
1392 case 0:
1393 case -ENOENT:
1394 case -ESTALE:
1395 break;
1396 case -NFS4ERR_BADSESSION:
1397 case -NFS4ERR_BADSLOT:
1398 case -NFS4ERR_BAD_HIGH_SLOT:
1399 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1400 case -NFS4ERR_DEADSESSION:
1401 set_bit(NFS_DELEGATED_STATE, &state->flags);
1402 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1403 return -EAGAIN;
1404 case -NFS4ERR_STALE_CLIENTID:
1405 case -NFS4ERR_STALE_STATEID:
1406 set_bit(NFS_DELEGATED_STATE, &state->flags);
1407 case -NFS4ERR_EXPIRED:
1408 /* Don't recall a delegation if it was lost */
1409 nfs4_schedule_lease_recovery(server->nfs_client);
1410 return -EAGAIN;
1411 case -NFS4ERR_DELEG_REVOKED:
1412 case -NFS4ERR_ADMIN_REVOKED:
1413 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001414 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001415 nfs_inode_find_state_and_recover(state->inode,
1416 stateid);
1417 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001418 return 0;
1419 case -NFS4ERR_DELAY:
1420 case -NFS4ERR_GRACE:
1421 set_bit(NFS_DELEGATED_STATE, &state->flags);
1422 ssleep(1);
1423 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001424 case -ENOMEM:
1425 case -NFS4ERR_DENIED:
1426 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1427 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return err;
1430}
1431
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001432int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1433{
1434 struct nfs_server *server = NFS_SERVER(state->inode);
1435 struct nfs4_opendata *opendata;
1436 int err;
1437
1438 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1439 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1440 if (IS_ERR(opendata))
1441 return PTR_ERR(opendata);
1442 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1443 err = nfs4_open_recover(opendata, state);
1444 nfs4_opendata_put(opendata);
1445 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1446}
1447
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001448static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1449{
1450 struct nfs4_opendata *data = calldata;
1451
1452 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001453 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001454 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001455 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001456 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001457 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001459}
1460
1461static void nfs4_open_confirm_release(void *calldata)
1462{
1463 struct nfs4_opendata *data = calldata;
1464 struct nfs4_state *state = NULL;
1465
1466 /* If this request hasn't been cancelled, do nothing */
1467 if (data->cancelled == 0)
1468 goto out_free;
1469 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001470 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001471 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001473 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001474 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001475out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001476 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477}
1478
1479static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001480 .rpc_call_done = nfs4_open_confirm_done,
1481 .rpc_release = nfs4_open_confirm_release,
1482};
1483
1484/*
1485 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1486 */
1487static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1488{
1489 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1490 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001491 struct rpc_message msg = {
1492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1493 .rpc_argp = &data->c_arg,
1494 .rpc_resp = &data->c_res,
1495 .rpc_cred = data->owner->so_cred,
1496 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001497 struct rpc_task_setup task_setup_data = {
1498 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001499 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001500 .callback_ops = &nfs4_open_confirm_ops,
1501 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001502 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001503 .flags = RPC_TASK_ASYNC,
1504 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int status;
1506
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001507 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001508 data->rpc_done = 0;
1509 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001510 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001511 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001512 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001513 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001514 status = nfs4_wait_for_completion_rpc_task(task);
1515 if (status != 0) {
1516 data->cancelled = 1;
1517 smp_wmb();
1518 } else
1519 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001520 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return status;
1522}
1523
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 struct nfs4_opendata *data = calldata;
1527 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001528
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001530 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001531 /*
1532 * Check if we still need to send an OPEN call, or if we can use
1533 * a delegation instead.
1534 */
1535 if (data->state != NULL) {
1536 struct nfs_delegation *delegation;
1537
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001539 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001542 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1543 can_open_delegated(delegation, data->o_arg.fmode))
1544 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001545 rcu_read_unlock();
1546 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001547 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001548 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001549 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001550 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001551 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001552 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1553 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001554 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001555 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001556 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001557 &data->o_res.seq_res,
1558 task) != 0)
1559 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001560 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001561unlock_no_action:
1562 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001563out_no_action:
1564 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001565out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001566 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001567}
1568
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569static void nfs4_open_done(struct rpc_task *task, void *calldata)
1570{
1571 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001574
Trond Myklebust14516c32010-07-31 14:29:06 -04001575 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1576 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001577
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001578 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001579 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1580 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001581 case S_IFREG:
1582 break;
1583 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001584 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001585 break;
1586 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001588 break;
1589 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001591 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001592 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001593 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001594 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1595 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001596 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001597 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001598}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001599
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600static void nfs4_open_release(void *calldata)
1601{
1602 struct nfs4_opendata *data = calldata;
1603 struct nfs4_state *state = NULL;
1604
1605 /* If this request hasn't been cancelled, do nothing */
1606 if (data->cancelled == 0)
1607 goto out_free;
1608 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001609 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 goto out_free;
1611 /* In case we need an open_confirm, no cleanup! */
1612 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1613 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001615 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001616 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001618 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619}
1620
1621static const struct rpc_call_ops nfs4_open_ops = {
1622 .rpc_call_prepare = nfs4_open_prepare,
1623 .rpc_call_done = nfs4_open_done,
1624 .rpc_release = nfs4_open_release,
1625};
1626
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001627static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628{
1629 struct inode *dir = data->dir->d_inode;
1630 struct nfs_server *server = NFS_SERVER(dir);
1631 struct nfs_openargs *o_arg = &data->o_arg;
1632 struct nfs_openres *o_res = &data->o_res;
1633 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001634 struct rpc_message msg = {
1635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1636 .rpc_argp = o_arg,
1637 .rpc_resp = o_res,
1638 .rpc_cred = data->owner->so_cred,
1639 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001640 struct rpc_task_setup task_setup_data = {
1641 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001642 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001643 .callback_ops = &nfs4_open_ops,
1644 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001645 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001646 .flags = RPC_TASK_ASYNC,
1647 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648 int status;
1649
Trond Myklebust9d12b212012-01-17 22:04:25 -05001650 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001651 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001652 data->rpc_done = 0;
1653 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001654 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001655 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001656 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001657 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001658 if (IS_ERR(task))
1659 return PTR_ERR(task);
1660 status = nfs4_wait_for_completion_rpc_task(task);
1661 if (status != 0) {
1662 data->cancelled = 1;
1663 smp_wmb();
1664 } else
1665 status = data->rpc_status;
1666 rpc_put_task(task);
1667
1668 return status;
1669}
1670
1671static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1672{
1673 struct inode *dir = data->dir->d_inode;
1674 struct nfs_openres *o_res = &data->o_res;
1675 int status;
1676
1677 status = nfs4_run_open_task(data, 1);
1678 if (status != 0 || !data->rpc_done)
1679 return status;
1680
Trond Myklebust6926afd2012-01-07 13:22:46 -05001681 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1682
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001683 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1684 status = _nfs4_proc_open_confirm(data);
1685 if (status != 0)
1686 return status;
1687 }
1688
1689 return status;
1690}
1691
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001692static int nfs4_opendata_access(struct rpc_cred *cred,
1693 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001694 struct nfs4_state *state, fmode_t fmode,
1695 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001696{
1697 struct nfs_access_entry cache;
1698 u32 mask;
1699
1700 /* access call failed or for some reason the server doesn't
1701 * support any access modes -- defer access call until later */
1702 if (opendata->o_res.access_supported == 0)
1703 return 0;
1704
1705 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001706 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001707 * write mode bits, but POSIX allows the creating process to write.
1708 * use openflags to check for exec, because fmode won't
1709 * always have FMODE_EXEC set when file open for exec. */
1710 if (openflags & __FMODE_EXEC) {
1711 /* ONLY check for exec rights */
1712 mask = MAY_EXEC;
1713 } else if (fmode & FMODE_READ)
1714 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001715
1716 cache.cred = cred;
1717 cache.jiffies = jiffies;
1718 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1719 nfs_access_add_cache(state->inode, &cache);
1720
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001721 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001722 return 0;
1723
1724 /* even though OPEN succeeded, access is denied. Close the file */
1725 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001726 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001727}
1728
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001729/*
1730 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1731 */
1732static int _nfs4_proc_open(struct nfs4_opendata *data)
1733{
1734 struct inode *dir = data->dir->d_inode;
1735 struct nfs_server *server = NFS_SERVER(dir);
1736 struct nfs_openargs *o_arg = &data->o_arg;
1737 struct nfs_openres *o_res = &data->o_res;
1738 int status;
1739
1740 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001741 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001743 if (status != 0) {
1744 if (status == -NFS4ERR_BADNAME &&
1745 !(o_arg->open_flags & O_CREAT))
1746 return -ENOENT;
1747 return status;
1748 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001749
Trond Myklebust6926afd2012-01-07 13:22:46 -05001750 nfs_fattr_map_and_free_names(server, &data->f_attr);
1751
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001752 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001753 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001754 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1755 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001757 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001762 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Andy Adamsond83217c2011-03-01 01:34:17 +00001766static int nfs4_recover_expired_lease(struct nfs_server *server)
1767{
1768 return nfs4_client_recover_expired_lease(server->nfs_client);
1769}
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771/*
1772 * OPEN_EXPIRED:
1773 * reclaim state on the server after a network partition.
1774 * Assumes caller holds the appropriate lock
1775 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001776static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001778 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001779 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001781 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001782 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001783 if (IS_ERR(opendata))
1784 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001786 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001787 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001788 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001792static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001793{
Trond Myklebust539cd032007-06-05 11:46:42 -04001794 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001795 struct nfs4_exception exception = { };
1796 int err;
1797
1798 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001799 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001800 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1801 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001802 switch (err) {
1803 default:
1804 goto out;
1805 case -NFS4ERR_GRACE:
1806 case -NFS4ERR_DELAY:
1807 nfs4_handle_exception(server, err, &exception);
1808 err = 0;
1809 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001810 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001811out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001812 return err;
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1816{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001818 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 ctx = nfs4_state_find_open_context(state);
1821 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001822 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001823 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 put_nfs_open_context(ctx);
1825 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001828#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001829static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001830{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001831 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001832 nfs4_stateid *stateid = &state->stateid;
1833 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001834
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001835 /* If a state reset has been done, test_stateid is unneeded */
1836 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1837 return;
1838
1839 status = nfs41_test_stateid(server, stateid);
1840 if (status != NFS_OK) {
1841 /* Free the stateid unless the server explicitly
1842 * informs us the stateid is unrecognized. */
1843 if (status != -NFS4ERR_BAD_STATEID)
1844 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001845 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001846
Bryan Schumaker69388672012-09-26 15:25:52 -04001847 write_seqlock(&state->seqlock);
1848 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1849 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001850 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1851 }
1852}
1853
1854/**
1855 * nfs41_check_open_stateid - possibly free an open stateid
1856 *
1857 * @state: NFSv4 state for an inode
1858 *
1859 * Returns NFS_OK if recovery for this stateid is now finished.
1860 * Otherwise a negative NFS4ERR value is returned.
1861 */
1862static int nfs41_check_open_stateid(struct nfs4_state *state)
1863{
1864 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001865 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001866 int status;
1867
1868 /* If a state reset has been done, test_stateid is unneeded */
1869 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1870 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1871 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1872 return -NFS4ERR_BAD_STATEID;
1873
1874 status = nfs41_test_stateid(server, stateid);
1875 if (status != NFS_OK) {
1876 /* Free the stateid unless the server explicitly
1877 * informs us the stateid is unrecognized. */
1878 if (status != -NFS4ERR_BAD_STATEID)
1879 nfs41_free_stateid(server, stateid);
1880
1881 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1882 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1883 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001884 }
1885 return status;
1886}
1887
1888static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1889{
Chuck Levereb64cf92012-07-11 16:30:05 -04001890 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001891
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001892 nfs41_clear_delegation_stateid(state);
1893 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001894 if (status != NFS_OK)
1895 status = nfs4_open_expired(sp, state);
1896 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001897}
1898#endif
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001901 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1902 * fields corresponding to attributes that were used to store the verifier.
1903 * Make sure we clobber those fields in the later setattr call
1904 */
1905static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1906{
1907 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1908 !(sattr->ia_valid & ATTR_ATIME_SET))
1909 sattr->ia_valid |= ATTR_ATIME;
1910
1911 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1912 !(sattr->ia_valid & ATTR_MTIME_SET))
1913 sattr->ia_valid |= ATTR_MTIME;
1914}
1915
Trond Myklebustc21443c2013-02-07 14:26:21 -05001916static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1917 fmode_t fmode,
1918 int flags,
1919 struct nfs4_state **res)
1920{
1921 struct nfs4_state_owner *sp = opendata->owner;
1922 struct nfs_server *server = sp->so_server;
1923 struct nfs4_state *state;
1924 unsigned int seq;
1925 int ret;
1926
1927 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1928
1929 ret = _nfs4_proc_open(opendata);
1930 if (ret != 0)
1931 goto out;
1932
1933 state = nfs4_opendata_to_nfs4_state(opendata);
1934 ret = PTR_ERR(state);
1935 if (IS_ERR(state))
1936 goto out;
1937 if (server->caps & NFS_CAP_POSIX_LOCK)
1938 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1939
1940 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1941 if (ret != 0)
1942 goto out;
1943
Trond Myklebust91876b12013-03-28 14:01:33 -04001944 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001945 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001946 *res = state;
1947out:
1948 return ret;
1949}
1950
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001951/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001952 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Andy Adamson82be4172012-05-23 05:02:35 -04001954static int _nfs4_do_open(struct inode *dir,
1955 struct dentry *dentry,
1956 fmode_t fmode,
1957 int flags,
1958 struct iattr *sattr,
1959 struct rpc_cred *cred,
1960 struct nfs4_state **res,
1961 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct nfs4_state_owner *sp;
1964 struct nfs4_state *state = NULL;
1965 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001966 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001967 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001968 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001972 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1973 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1975 goto out_err;
1976 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001977 status = nfs4_recover_expired_lease(server);
1978 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001979 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001980 if (dentry->d_inode != NULL)
1981 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001982 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001983 if (dentry->d_inode)
1984 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001985 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001986 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001987 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001988 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Andy Adamson82be4172012-05-23 05:02:35 -04001990 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1991 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1992 if (!opendata->f_attr.mdsthreshold)
1993 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001994 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001995 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001996 if (dentry->d_inode != NULL)
1997 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001998
Trond Myklebustc21443c2013-02-07 14:26:21 -05001999 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002000 if (status != 0)
2001 goto err_opendata_put;
2002
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002003 if (opendata->o_arg.open_flags & O_EXCL) {
2004 nfs4_exclusive_attrset(opendata, sattr);
2005
2006 nfs_fattr_init(opendata->o_res.f_attr);
2007 status = nfs4_do_setattr(state->inode, cred,
2008 opendata->o_res.f_attr, sattr,
2009 state);
2010 if (status == 0)
2011 nfs_setattr_update_inode(state->inode, sattr);
2012 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2013 }
Andy Adamson82be4172012-05-23 05:02:35 -04002014
2015 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2016 *ctx_th = opendata->f_attr.mdsthreshold;
2017 else
2018 kfree(opendata->f_attr.mdsthreshold);
2019 opendata->f_attr.mdsthreshold = NULL;
2020
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002021 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 *res = state;
2024 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002025err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002026 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002027 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002028err_put_state_owner:
2029 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *res = NULL;
2032 return status;
2033}
2034
2035
Andy Adamson82be4172012-05-23 05:02:35 -04002036static struct nfs4_state *nfs4_do_open(struct inode *dir,
2037 struct dentry *dentry,
2038 fmode_t fmode,
2039 int flags,
2040 struct iattr *sattr,
2041 struct rpc_cred *cred,
2042 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002044 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct nfs4_exception exception = { };
2046 struct nfs4_state *res;
2047 int status;
2048
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002049 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002051 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2052 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (status == 0)
2054 break;
2055 /* NOTE: BAD_SEQID means the server and client disagree about the
2056 * book-keeping w.r.t. state-changing operations
2057 * (OPEN/CLOSE/LOCK/LOCKU...)
2058 * It is actually a sign of a bug on the client or on the server.
2059 *
2060 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002061 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * have unhashed the old state_owner for us, and that we can
2063 * therefore safely retry using a new one. We should still warn
2064 * the user though...
2065 */
2066 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002067 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002068 " returned a bad sequence-id error!\n",
2069 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 exception.retry = 1;
2071 continue;
2072 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002073 /*
2074 * BAD_STATEID on OPEN means that the server cancelled our
2075 * state before it received the OPEN_CONFIRM.
2076 * Recover by retrying the request as per the discussion
2077 * on Page 181 of RFC3530.
2078 */
2079 if (status == -NFS4ERR_BAD_STATEID) {
2080 exception.retry = 1;
2081 continue;
2082 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002083 if (status == -EAGAIN) {
2084 /* We must have found a delegation */
2085 exception.retry = 1;
2086 continue;
2087 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002088 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2089 continue;
2090 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 status, &exception));
2092 } while (exception.retry);
2093 return res;
2094}
2095
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002096static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2097 struct nfs_fattr *fattr, struct iattr *sattr,
2098 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002100 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002102 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .iap = sattr,
2104 .server = server,
2105 .bitmask = server->attr_bitmask,
2106 };
2107 struct nfs_setattrres res = {
2108 .fattr = fattr,
2109 .server = server,
2110 };
2111 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2113 .rpc_argp = &arg,
2114 .rpc_resp = &res,
2115 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002117 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002118 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Trond Myklebust0e574af2005-10-27 22:12:38 -04002120 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Trond Myklebust5d422302013-03-14 16:57:48 -04002122 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002123 struct nfs_lockowner lockowner = {
2124 .l_owner = current->files,
2125 .l_pid = current->tgid,
2126 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002127 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002128 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002129 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2130 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002131 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002132 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002133 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Bryan Schumaker7c513052011-03-24 17:12:24 +00002135 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 if (status == 0 && state != NULL)
2137 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002138 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002141static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2142 struct nfs_fattr *fattr, struct iattr *sattr,
2143 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002145 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002146 struct nfs4_exception exception = {
2147 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002148 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002149 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int err;
2151 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002152 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2153 switch (err) {
2154 case -NFS4ERR_OPENMODE:
2155 if (state && !(state->state & FMODE_WRITE)) {
2156 err = -EBADF;
2157 if (sattr->ia_valid & ATTR_OPEN)
2158 err = -EACCES;
2159 goto out;
2160 }
2161 }
2162 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002164out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return err;
2166}
2167
2168struct nfs4_closedata {
2169 struct inode *inode;
2170 struct nfs4_state *state;
2171 struct nfs_closeargs arg;
2172 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002173 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002174 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002175 bool roc;
2176 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002179static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002180{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002181 struct nfs4_closedata *calldata = data;
2182 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002183 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002184
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 if (calldata->roc)
2186 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002187 nfs4_put_open_state(calldata->state);
2188 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002189 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002190 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002191 kfree(calldata);
2192}
2193
Trond Myklebust88069f72009-12-08 08:33:16 -05002194static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2195 fmode_t fmode)
2196{
2197 spin_lock(&state->owner->so_lock);
2198 if (!(fmode & FMODE_READ))
2199 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2200 if (!(fmode & FMODE_WRITE))
2201 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2202 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2203 spin_unlock(&state->owner->so_lock);
2204}
2205
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002206static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002208 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct nfs_server *server = NFS_SERVER(calldata->inode);
2211
Chuck Levera3ca5652012-03-01 17:00:40 -05002212 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002213 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2214 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* hmm. we are done with the inode, and in the process of freeing
2216 * the state_owner. we keep this around to process errors
2217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 switch (task->tk_status) {
2219 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002220 if (calldata->roc)
2221 pnfs_roc_set_barrier(state->inode,
2222 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002223 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002224 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002225 nfs4_close_clear_stateid_flags(state,
2226 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002229 case -NFS4ERR_OLD_STATEID:
2230 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002232 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002235 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2236 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002238 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002239 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002240 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002243static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002245 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002246 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002247 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002248 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002249
Chuck Levera3ca5652012-03-01 17:00:40 -05002250 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002251 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002252 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002253
Trond Myklebust88069f72009-12-08 08:33:16 -05002254 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2255 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002256 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002257 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002258 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002259 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002260 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2261 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2262 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002263 }
2264 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002265 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2266 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2267 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002268 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002269 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002270 if (!nfs4_valid_open_stateid(state))
2271 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002272 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002273
2274 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002275 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002276 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002277 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002278
Fred Isamanf7e89172011-01-06 11:36:32 +00002279 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002280 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002281 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002282 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2283 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002284 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002285 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002286 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002287
Trond Myklebust516a6af2005-10-27 22:12:41 -04002288 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002289 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002290 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002291 &calldata->arg.seq_args,
2292 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002293 task) != 0)
2294 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002295 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002296 return;
2297out_no_action:
2298 task->tk_action = NULL;
2299out_wait:
2300 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002303static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002304 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002305 .rpc_call_done = nfs4_close_done,
2306 .rpc_release = nfs4_free_closedata,
2307};
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309/*
2310 * It is possible for data to be read/written from a mem-mapped file
2311 * after the sys_close call (which hits the vfs layer as a flush).
2312 * This means that we can't safely call nfsv4 close on a file until
2313 * the inode is cleared. This in turn means that we are not good
2314 * NFSv4 citizens - we do not indicate to the server to update the file's
2315 * share state even when we are done with one of the three share
2316 * stateid's in the inode.
2317 *
2318 * NOTE: Caller must be holding the sp->so_owner semaphore!
2319 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002320int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002322 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002324 struct nfs4_state_owner *sp = state->owner;
2325 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002326 struct rpc_message msg = {
2327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2328 .rpc_cred = state->owner->so_cred,
2329 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002330 struct rpc_task_setup task_setup_data = {
2331 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002332 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002333 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002334 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002335 .flags = RPC_TASK_ASYNC,
2336 };
Trond Myklebust95121352005-10-18 14:20:12 -07002337 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002339 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002341 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002342 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002343 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002345 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002346 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002348 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002349 if (calldata->arg.seqid == NULL)
2350 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002351 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002352 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002353 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002354 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002355 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002356 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002357 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002358
Trond Myklebust1174dd12010-12-21 10:52:24 -05002359 msg.rpc_argp = &calldata->arg;
2360 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002361 task_setup_data.callback_data = calldata;
2362 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002363 if (IS_ERR(task))
2364 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002365 status = 0;
2366 if (wait)
2367 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002368 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002369 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002370out_free_calldata:
2371 kfree(calldata);
2372out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002373 nfs4_put_open_state(state);
2374 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002375 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Trond Myklebust2b484292010-09-17 10:56:51 -04002378static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002379nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct nfs4_state *state;
2382
Trond Myklebust565277f2007-10-15 18:17:53 -04002383 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002384 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2385 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002386 if (IS_ERR(state))
2387 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002388 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002389 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Trond Myklebust1185a552009-12-03 15:54:02 -05002392static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002393{
2394 if (ctx->state == NULL)
2395 return;
2396 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002397 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002398 else
Al Viro643168c2011-06-22 18:20:23 -04002399 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2403{
Benny Halevy43652ad2009-04-01 09:21:54 -04002404 struct nfs4_server_caps_arg args = {
2405 .fhandle = fhandle,
2406 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct nfs4_server_caps_res res = {};
2408 struct rpc_message msg = {
2409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002410 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 .rpc_resp = &res,
2412 };
2413 int status;
2414
Bryan Schumaker7c513052011-03-24 17:12:24 +00002415 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (status == 0) {
2417 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002418 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2419 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2420 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2421 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2422 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2424 server->caps |= NFS_CAP_ACLS;
2425 if (res.has_links != 0)
2426 server->caps |= NFS_CAP_HARDLINKS;
2427 if (res.has_symlinks != 0)
2428 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002429 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2430 server->caps |= NFS_CAP_FILEID;
2431 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2432 server->caps |= NFS_CAP_MODE;
2433 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2434 server->caps |= NFS_CAP_NLINK;
2435 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2436 server->caps |= NFS_CAP_OWNER;
2437 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2438 server->caps |= NFS_CAP_OWNER_GROUP;
2439 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2440 server->caps |= NFS_CAP_ATIME;
2441 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2442 server->caps |= NFS_CAP_CTIME;
2443 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2444 server->caps |= NFS_CAP_MTIME;
2445
Trond Myklebusta65318b2009-03-11 14:10:28 -04002446 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2447 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2448 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002450 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return status;
2454}
2455
Trond Myklebust55a97592006-06-09 09:34:19 -04002456int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 struct nfs4_exception exception = { };
2459 int err;
2460 do {
2461 err = nfs4_handle_exception(server,
2462 _nfs4_server_capabilities(server, fhandle),
2463 &exception);
2464 } while (exception.retry);
2465 return err;
2466}
2467
2468static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2469 struct nfs_fsinfo *info)
2470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct nfs4_lookup_root_arg args = {
2472 .bitmask = nfs4_fattr_bitmap,
2473 };
2474 struct nfs4_lookup_res res = {
2475 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002476 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 .fh = fhandle,
2478 };
2479 struct rpc_message msg = {
2480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2481 .rpc_argp = &args,
2482 .rpc_resp = &res,
2483 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002484
Trond Myklebust0e574af2005-10-27 22:12:38 -04002485 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2490 struct nfs_fsinfo *info)
2491{
2492 struct nfs4_exception exception = { };
2493 int err;
2494 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002495 err = _nfs4_lookup_root(server, fhandle, info);
2496 switch (err) {
2497 case 0:
2498 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002499 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002500 default:
2501 err = nfs4_handle_exception(server, err, &exception);
2502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002504out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return err;
2506}
2507
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002508static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2509 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2510{
2511 struct rpc_auth *auth;
2512 int ret;
2513
2514 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002515 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002516 ret = -EIO;
2517 goto out;
2518 }
2519 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002520out:
2521 return ret;
2522}
2523
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002524static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002525 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002527 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002528 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002530 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002531 if (len < 0)
2532 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002533
2534 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002535 /* AUTH_UNIX is the default flavor if none was specified,
2536 * thus has already been tried. */
2537 if (flav_array[i] == RPC_AUTH_UNIX)
2538 continue;
2539
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002540 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002541 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002542 continue;
2543 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002544 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002545 /*
2546 * -EACCESS could mean that the user doesn't have correct permissions
2547 * to access the mount. It could also mean that we tried to mount
2548 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2549 * existing mount programs don't handle -EACCES very well so it should
2550 * be mapped to -EPERM instead.
2551 */
2552 if (status == -EACCES)
2553 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002554 return status;
2555}
2556
2557/*
2558 * get the file handle for the "/" directory on the server
2559 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002560int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2561 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002562{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002563 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002564 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002565 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2566 /*
2567 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2568 * by nfs4_map_errors() as this function exits.
2569 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002570 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (status == 0)
2572 status = nfs4_server_capabilities(server, fhandle);
2573 if (status == 0)
2574 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002575 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Bryan Schumakerbae36242012-05-10 15:07:31 -04002578static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2579 struct nfs_fsinfo *info)
2580{
2581 int error;
2582 struct nfs_fattr *fattr = info->fattr;
2583
2584 error = nfs4_server_capabilities(server, mntfh);
2585 if (error < 0) {
2586 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2587 return error;
2588 }
2589
2590 error = nfs4_proc_getattr(server, mntfh, fattr);
2591 if (error < 0) {
2592 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2593 return error;
2594 }
2595
2596 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2597 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2598 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2599
2600 return error;
2601}
2602
Manoj Naik6b97fd32006-06-09 09:34:29 -04002603/*
2604 * Get locations and (maybe) other attributes of a referral.
2605 * Note that we'll actually follow the referral later when
2606 * we detect fsid mismatch in inode revalidation
2607 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002608static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2609 const struct qstr *name, struct nfs_fattr *fattr,
2610 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002611{
2612 int status = -ENOMEM;
2613 struct page *page = NULL;
2614 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002615
2616 page = alloc_page(GFP_KERNEL);
2617 if (page == NULL)
2618 goto out;
2619 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2620 if (locations == NULL)
2621 goto out;
2622
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002623 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002624 if (status != 0)
2625 goto out;
2626 /* Make sure server returned a different fsid for the referral */
2627 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002628 dprintk("%s: server did not return a different fsid for"
2629 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002630 status = -EIO;
2631 goto out;
2632 }
Andy Adamson533eb462011-06-13 18:25:56 -04002633 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2634 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002635
Andy Adamson533eb462011-06-13 18:25:56 -04002636 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002638 memset(fhandle, 0, sizeof(struct nfs_fh));
2639out:
2640 if (page)
2641 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002642 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643 return status;
2644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2647{
2648 struct nfs4_getattr_arg args = {
2649 .fh = fhandle,
2650 .bitmask = server->attr_bitmask,
2651 };
2652 struct nfs4_getattr_res res = {
2653 .fattr = fattr,
2654 .server = server,
2655 };
2656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2658 .rpc_argp = &args,
2659 .rpc_resp = &res,
2660 };
2661
Trond Myklebust0e574af2005-10-27 22:12:38 -04002662 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002663 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
2666static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2667{
2668 struct nfs4_exception exception = { };
2669 int err;
2670 do {
2671 err = nfs4_handle_exception(server,
2672 _nfs4_proc_getattr(server, fhandle, fattr),
2673 &exception);
2674 } while (exception.retry);
2675 return err;
2676}
2677
2678/*
2679 * The file is not closed if it is opened due to the a request to change
2680 * the size of the file. The open call will not be needed once the
2681 * VFS layer lookup-intents are implemented.
2682 *
2683 * Close is called when the inode is destroyed.
2684 * If we haven't opened the file for O_WRONLY, we
2685 * need to in the size_change case to obtain a stateid.
2686 *
2687 * Got race?
2688 * Because OPEN is always done by name in nfsv4, it is
2689 * possible that we opened a different file by the same
2690 * name. We can recognize this race condition, but we
2691 * can't do anything about it besides returning an error.
2692 *
2693 * This will be fixed with VFS changes (lookup-intent).
2694 */
2695static int
2696nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2697 struct iattr *sattr)
2698{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002699 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002700 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002701 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 int status;
2703
Benny Halevy8a1636c2010-07-14 15:43:57 -04002704 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002705 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002706
Trond Myklebust0e574af2005-10-27 22:12:38 -04002707 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Andy Adamson26699402012-05-30 16:12:24 -04002709 /* Deal with open(O_TRUNC) */
2710 if (sattr->ia_valid & ATTR_OPEN)
2711 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2712
2713 /* Optimization: if the end result is no change, don't RPC */
2714 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2715 return 0;
2716
Trond Myklebustd5308382005-11-04 15:33:38 -05002717 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002718 if (sattr->ia_valid & ATTR_FILE) {
2719 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002720
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002721 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002722 if (ctx) {
2723 cred = ctx->cred;
2724 state = ctx->state;
2725 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002726 }
2727
2728 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002729 if (status == 0)
2730 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return status;
2732}
2733
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002734static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2735 const struct qstr *name, struct nfs_fh *fhandle,
2736 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002737{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002738 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002739 int status;
2740 struct nfs4_lookup_arg args = {
2741 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002742 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002743 .name = name,
2744 };
2745 struct nfs4_lookup_res res = {
2746 .server = server,
2747 .fattr = fattr,
2748 .fh = fhandle,
2749 };
2750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2752 .rpc_argp = &args,
2753 .rpc_resp = &res,
2754 };
2755
2756 nfs_fattr_init(fattr);
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002759 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 dprintk("NFS reply lookup: %d\n", status);
2761 return status;
2762}
2763
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002764static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002765{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002766 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002767 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002768 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2769 fattr->nlink = 2;
2770}
2771
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002772static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2773 struct qstr *name, struct nfs_fh *fhandle,
2774 struct nfs_fattr *fattr)
2775{
2776 struct nfs4_exception exception = { };
2777 struct rpc_clnt *client = *clnt;
2778 int err;
2779 do {
2780 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2781 switch (err) {
2782 case -NFS4ERR_BADNAME:
2783 err = -ENOENT;
2784 goto out;
2785 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002786 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002787 goto out;
2788 case -NFS4ERR_WRONGSEC:
2789 err = -EPERM;
2790 if (client != *clnt)
2791 goto out;
2792
2793 client = nfs4_create_sec_client(client, dir, name);
2794 if (IS_ERR(client))
2795 return PTR_ERR(client);
2796
2797 exception.retry = 1;
2798 break;
2799 default:
2800 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2801 }
2802 } while (exception.retry);
2803
2804out:
2805 if (err == 0)
2806 *clnt = client;
2807 else if (client != *clnt)
2808 rpc_shutdown_client(client);
2809
2810 return err;
2811}
2812
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002813static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002814 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002816 int status;
2817 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002818
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002819 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2820 if (client != NFS_CLIENT(dir)) {
2821 rpc_shutdown_client(client);
2822 nfs_fixup_secinfo_attributes(fattr);
2823 }
2824 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002827struct rpc_clnt *
2828nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2829 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2830{
2831 int status;
2832 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2833
2834 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2835 if (status < 0) {
2836 rpc_shutdown_client(client);
2837 return ERR_PTR(status);
2838 }
2839 return client;
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2843{
Trond Myklebust76b32992007-08-10 17:45:11 -04002844 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 struct nfs4_accessargs args = {
2846 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002847 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002849 struct nfs4_accessres res = {
2850 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 struct rpc_message msg = {
2853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2854 .rpc_argp = &args,
2855 .rpc_resp = &res,
2856 .rpc_cred = entry->cred,
2857 };
2858 int mode = entry->mask;
2859 int status;
2860
2861 /*
2862 * Determine which access bits we want to ask for...
2863 */
2864 if (mode & MAY_READ)
2865 args.access |= NFS4_ACCESS_READ;
2866 if (S_ISDIR(inode->i_mode)) {
2867 if (mode & MAY_WRITE)
2868 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2869 if (mode & MAY_EXEC)
2870 args.access |= NFS4_ACCESS_LOOKUP;
2871 } else {
2872 if (mode & MAY_WRITE)
2873 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2874 if (mode & MAY_EXEC)
2875 args.access |= NFS4_ACCESS_EXECUTE;
2876 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002877
2878 res.fattr = nfs_alloc_fattr();
2879 if (res.fattr == NULL)
2880 return -ENOMEM;
2881
Bryan Schumaker7c513052011-03-24 17:12:24 +00002882 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002884 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002885 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002887 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return status;
2889}
2890
2891static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2892{
2893 struct nfs4_exception exception = { };
2894 int err;
2895 do {
2896 err = nfs4_handle_exception(NFS_SERVER(inode),
2897 _nfs4_proc_access(inode, entry),
2898 &exception);
2899 } while (exception.retry);
2900 return err;
2901}
2902
2903/*
2904 * TODO: For the time being, we don't try to get any attributes
2905 * along with any of the zero-copy operations READ, READDIR,
2906 * READLINK, WRITE.
2907 *
2908 * In the case of the first three, we want to put the GETATTR
2909 * after the read-type operation -- this is because it is hard
2910 * to predict the length of a GETATTR response in v4, and thus
2911 * align the READ data correctly. This means that the GETATTR
2912 * may end up partially falling into the page cache, and we should
2913 * shift it into the 'tail' of the xdr_buf before processing.
2914 * To do this efficiently, we need to know the total length
2915 * of data received, which doesn't seem to be available outside
2916 * of the RPC layer.
2917 *
2918 * In the case of WRITE, we also want to put the GETATTR after
2919 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002920 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 *
2922 * Both of these changes to the XDR layer would in fact be quite
2923 * minor, but I decided to leave them for a subsequent patch.
2924 */
2925static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2926 unsigned int pgbase, unsigned int pglen)
2927{
2928 struct nfs4_readlink args = {
2929 .fh = NFS_FH(inode),
2930 .pgbase = pgbase,
2931 .pglen = pglen,
2932 .pages = &page,
2933 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002934 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct rpc_message msg = {
2936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2937 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002938 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 };
2940
Bryan Schumaker7c513052011-03-24 17:12:24 +00002941 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 -07002942}
2943
2944static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2945 unsigned int pgbase, unsigned int pglen)
2946{
2947 struct nfs4_exception exception = { };
2948 int err;
2949 do {
2950 err = nfs4_handle_exception(NFS_SERVER(inode),
2951 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2952 &exception);
2953 } while (exception.retry);
2954 return err;
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002958 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960static int
2961nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002962 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002964 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 int status = 0;
2967
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002968 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2969 if (IS_ERR(ctx))
2970 return PTR_ERR(ctx);
2971
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002972 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002973 state = nfs4_do_open(dir, dentry, ctx->mode,
2974 flags, sattr, ctx->cred,
2975 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002976 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (IS_ERR(state)) {
2978 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002981 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002982 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002983 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002985 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return status;
2987}
2988
2989static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2990{
Trond Myklebust16e42952005-10-27 22:12:44 -04002991 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002992 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002994 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002996 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002997 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3001 .rpc_argp = &args,
3002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003004 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003005
Bryan Schumaker7c513052011-03-24 17:12:24 +00003006 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003007 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003008 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return status;
3010}
3011
3012static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3013{
3014 struct nfs4_exception exception = { };
3015 int err;
3016 do {
3017 err = nfs4_handle_exception(NFS_SERVER(dir),
3018 _nfs4_proc_remove(dir, name),
3019 &exception);
3020 } while (exception.retry);
3021 return err;
3022}
3023
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003024static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003026 struct nfs_server *server = NFS_SERVER(dir);
3027 struct nfs_removeargs *args = msg->rpc_argp;
3028 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003030 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003032 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003035static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3036{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003037 nfs4_setup_sequence(NFS_SERVER(data->dir),
3038 &data->args.seq_args,
3039 &data->res.seq_res,
3040 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003043static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003045 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3046
Trond Myklebust14516c32010-07-31 14:29:06 -04003047 if (!nfs4_sequence_done(task, &res->seq_res))
3048 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003049 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003050 return 0;
3051 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003052 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
Jeff Laytond3d41522010-09-17 17:31:57 -04003055static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3056{
3057 struct nfs_server *server = NFS_SERVER(dir);
3058 struct nfs_renameargs *arg = msg->rpc_argp;
3059 struct nfs_renameres *res = msg->rpc_resp;
3060
3061 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003062 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003063 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003064}
3065
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003066static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3067{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003068 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3069 &data->args.seq_args,
3070 &data->res.seq_res,
3071 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003072}
3073
3074static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3075 struct inode *new_dir)
3076{
3077 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3078
3079 if (!nfs4_sequence_done(task, &res->seq_res))
3080 return 0;
3081 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3082 return 0;
3083
3084 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003085 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003086 return 1;
3087}
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3090 struct inode *new_dir, struct qstr *new_name)
3091{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003092 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003093 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 .old_dir = NFS_FH(old_dir),
3095 .new_dir = NFS_FH(new_dir),
3096 .old_name = old_name,
3097 .new_name = new_name,
3098 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003099 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003100 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3104 .rpc_argp = &arg,
3105 .rpc_resp = &res,
3106 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003107 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Bryan Schumaker7c513052011-03-24 17:12:24 +00003109 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!status) {
3111 update_changeattr(old_dir, &res.old_cinfo);
3112 update_changeattr(new_dir, &res.new_cinfo);
3113 }
3114 return status;
3115}
3116
3117static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3118 struct inode *new_dir, struct qstr *new_name)
3119{
3120 struct nfs4_exception exception = { };
3121 int err;
3122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3124 _nfs4_proc_rename(old_dir, old_name,
3125 new_dir, new_name),
3126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
3131static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3132{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003133 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct nfs4_link_arg arg = {
3135 .fh = NFS_FH(inode),
3136 .dir_fh = NFS_FH(dir),
3137 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003138 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003140 struct nfs4_link_res res = {
3141 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 struct rpc_message msg = {
3144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3145 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003146 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003148 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Trond Myklebust136f2622010-04-16 16:22:49 -04003150 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003151 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003152 goto out;
3153
Bryan Schumaker7c513052011-03-24 17:12:24 +00003154 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003155 if (!status) {
3156 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003157 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003158 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003159out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003160 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return status;
3162}
3163
3164static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3165{
3166 struct nfs4_exception exception = { };
3167 int err;
3168 do {
3169 err = nfs4_handle_exception(NFS_SERVER(inode),
3170 _nfs4_proc_link(inode, dir, name),
3171 &exception);
3172 } while (exception.retry);
3173 return err;
3174}
3175
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003176struct nfs4_createdata {
3177 struct rpc_message msg;
3178 struct nfs4_create_arg arg;
3179 struct nfs4_create_res res;
3180 struct nfs_fh fh;
3181 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003182};
3183
3184static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3185 struct qstr *name, struct iattr *sattr, u32 ftype)
3186{
3187 struct nfs4_createdata *data;
3188
3189 data = kzalloc(sizeof(*data), GFP_KERNEL);
3190 if (data != NULL) {
3191 struct nfs_server *server = NFS_SERVER(dir);
3192
3193 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3194 data->msg.rpc_argp = &data->arg;
3195 data->msg.rpc_resp = &data->res;
3196 data->arg.dir_fh = NFS_FH(dir);
3197 data->arg.server = server;
3198 data->arg.name = name;
3199 data->arg.attrs = sattr;
3200 data->arg.ftype = ftype;
3201 data->arg.bitmask = server->attr_bitmask;
3202 data->res.server = server;
3203 data->res.fh = &data->fh;
3204 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003205 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003206 }
3207 return data;
3208}
3209
3210static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3211{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003212 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003213 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003214 if (status == 0) {
3215 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3217 }
3218 return status;
3219}
3220
3221static void nfs4_free_createdata(struct nfs4_createdata *data)
3222{
3223 kfree(data);
3224}
3225
Chuck Lever4f390c12006-08-22 20:06:22 -04003226static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003227 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 struct nfs4_createdata *data;
3230 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Chuck Lever94a6d752006-08-22 20:06:23 -04003232 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003233 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003234
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003235 status = -ENOMEM;
3236 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3237 if (data == NULL)
3238 goto out;
3239
3240 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3241 data->arg.u.symlink.pages = &page;
3242 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003244 status = nfs4_do_create(dir, dentry, data);
3245
3246 nfs4_free_createdata(data);
3247out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return status;
3249}
3250
Chuck Lever4f390c12006-08-22 20:06:22 -04003251static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003252 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 struct nfs4_exception exception = { };
3255 int err;
3256 do {
3257 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003258 _nfs4_proc_symlink(dir, dentry, page,
3259 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
3265static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3266 struct iattr *sattr)
3267{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003268 struct nfs4_createdata *data;
3269 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003271 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3272 if (data == NULL)
3273 goto out;
3274
3275 status = nfs4_do_create(dir, dentry, data);
3276
3277 nfs4_free_createdata(data);
3278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
3282static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3283 struct iattr *sattr)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003287
3288 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 do {
3290 err = nfs4_handle_exception(NFS_SERVER(dir),
3291 _nfs4_proc_mkdir(dir, dentry, sattr),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003298 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct inode *dir = dentry->d_inode;
3301 struct nfs4_readdir_arg args = {
3302 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003303 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 .pgbase = 0,
3305 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003306 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003307 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 };
3309 struct nfs4_readdir_res res;
3310 struct rpc_message msg = {
3311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3312 .rpc_argp = &args,
3313 .rpc_resp = &res,
3314 .rpc_cred = cred,
3315 };
3316 int status;
3317
Harvey Harrison3110ff82008-05-02 13:42:44 -07003318 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003319 dentry->d_parent->d_name.name,
3320 dentry->d_name.name,
3321 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003322 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003324 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 -05003325 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003326 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003327 status += args.pgbase;
3328 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003329
3330 nfs_invalidate_atime(dir);
3331
Harvey Harrison3110ff82008-05-02 13:42:44 -07003332 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return status;
3334}
3335
3336static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003337 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct nfs4_exception exception = { };
3340 int err;
3341 do {
3342 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3343 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003344 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
3350static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3351 struct iattr *sattr, dev_t rdev)
3352{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003353 struct nfs4_createdata *data;
3354 int mode = sattr->ia_mode;
3355 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003357 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3358 if (data == NULL)
3359 goto out;
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003362 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003364 data->arg.ftype = NF4BLK;
3365 data->arg.u.device.specdata1 = MAJOR(rdev);
3366 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003369 data->arg.ftype = NF4CHR;
3370 data->arg.u.device.specdata1 = MAJOR(rdev);
3371 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003372 } else if (!S_ISSOCK(mode)) {
3373 status = -EINVAL;
3374 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003377 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003378out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003379 nfs4_free_createdata(data);
3380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return status;
3382}
3383
3384static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3385 struct iattr *sattr, dev_t rdev)
3386{
3387 struct nfs4_exception exception = { };
3388 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003389
3390 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 do {
3392 err = nfs4_handle_exception(NFS_SERVER(dir),
3393 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3394 &exception);
3395 } while (exception.retry);
3396 return err;
3397}
3398
3399static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_fsstat *fsstat)
3401{
3402 struct nfs4_statfs_arg args = {
3403 .fh = fhandle,
3404 .bitmask = server->attr_bitmask,
3405 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003406 struct nfs4_statfs_res res = {
3407 .fsstat = fsstat,
3408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3411 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003412 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 };
3414
Trond Myklebust0e574af2005-10-27 22:12:38 -04003415 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003416 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
3419static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3420{
3421 struct nfs4_exception exception = { };
3422 int err;
3423 do {
3424 err = nfs4_handle_exception(server,
3425 _nfs4_proc_statfs(server, fhandle, fsstat),
3426 &exception);
3427 } while (exception.retry);
3428 return err;
3429}
3430
3431static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3432 struct nfs_fsinfo *fsinfo)
3433{
3434 struct nfs4_fsinfo_arg args = {
3435 .fh = fhandle,
3436 .bitmask = server->attr_bitmask,
3437 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003438 struct nfs4_fsinfo_res res = {
3439 .fsinfo = fsinfo,
3440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 struct rpc_message msg = {
3442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3443 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003444 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 };
3446
Bryan Schumaker7c513052011-03-24 17:12:24 +00003447 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
3450static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3451{
3452 struct nfs4_exception exception = { };
3453 int err;
3454
3455 do {
3456 err = nfs4_handle_exception(server,
3457 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3458 &exception);
3459 } while (exception.retry);
3460 return err;
3461}
3462
3463static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3464{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003465 int error;
3466
Trond Myklebust0e574af2005-10-27 22:12:38 -04003467 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003468 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003469 if (error == 0) {
3470 /* block layout checks this! */
3471 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003472 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003473 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003474
3475 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
3478static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3479 struct nfs_pathconf *pathconf)
3480{
3481 struct nfs4_pathconf_arg args = {
3482 .fh = fhandle,
3483 .bitmask = server->attr_bitmask,
3484 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003485 struct nfs4_pathconf_res res = {
3486 .pathconf = pathconf,
3487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 struct rpc_message msg = {
3489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3490 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003491 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 };
3493
3494 /* None of the pathconf attributes are mandatory to implement */
3495 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3496 memset(pathconf, 0, sizeof(*pathconf));
3497 return 0;
3498 }
3499
Trond Myklebust0e574af2005-10-27 22:12:38 -04003500 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003501 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3505 struct nfs_pathconf *pathconf)
3506{
3507 struct nfs4_exception exception = { };
3508 int err;
3509
3510 do {
3511 err = nfs4_handle_exception(server,
3512 _nfs4_proc_pathconf(server, fhandle, pathconf),
3513 &exception);
3514 } while (exception.retry);
3515 return err;
3516}
3517
Trond Myklebust5521abf2013-03-16 20:54:34 -04003518int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003519 const struct nfs_open_context *ctx,
3520 const struct nfs_lock_context *l_ctx,
3521 fmode_t fmode)
3522{
3523 const struct nfs_lockowner *lockowner = NULL;
3524
3525 if (l_ctx != NULL)
3526 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003527 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003528}
3529EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3530
Trond Myklebust5521abf2013-03-16 20:54:34 -04003531static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3532 const struct nfs_open_context *ctx,
3533 const struct nfs_lock_context *l_ctx,
3534 fmode_t fmode)
3535{
3536 nfs4_stateid current_stateid;
3537
3538 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3539 return false;
3540 return nfs4_stateid_match(stateid, &current_stateid);
3541}
3542
3543static bool nfs4_error_stateid_expired(int err)
3544{
3545 switch (err) {
3546 case -NFS4ERR_DELEG_REVOKED:
3547 case -NFS4ERR_ADMIN_REVOKED:
3548 case -NFS4ERR_BAD_STATEID:
3549 case -NFS4ERR_STALE_STATEID:
3550 case -NFS4ERR_OLD_STATEID:
3551 case -NFS4ERR_OPENMODE:
3552 case -NFS4ERR_EXPIRED:
3553 return true;
3554 }
3555 return false;
3556}
3557
Benny Halevyd20581a2011-05-22 19:52:03 +03003558void __nfs4_read_done_cb(struct nfs_read_data *data)
3559{
Fred Isamancd841602012-04-20 14:47:44 -04003560 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003561}
3562
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003563static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Fred Isamancd841602012-04-20 14:47:44 -04003565 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003567 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003568 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003569 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003571
Benny Halevyd20581a2011-05-22 19:52:03 +03003572 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003574 renew_lease(server, data->timestamp);
3575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebust5521abf2013-03-16 20:54:34 -04003578static bool nfs4_read_stateid_changed(struct rpc_task *task,
3579 struct nfs_readargs *args)
3580{
3581
3582 if (!nfs4_error_stateid_expired(task->tk_status) ||
3583 nfs4_stateid_is_current(&args->stateid,
3584 args->context,
3585 args->lock_context,
3586 FMODE_READ))
3587 return false;
3588 rpc_restart_call_prepare(task);
3589 return true;
3590}
3591
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003592static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3593{
3594
3595 dprintk("--> %s\n", __func__);
3596
3597 if (!nfs4_sequence_done(task, &data->res.seq_res))
3598 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003599 if (nfs4_read_stateid_changed(task, &data->args))
3600 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003601 return data->read_done_cb ? data->read_done_cb(task, data) :
3602 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003603}
3604
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003605static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003608 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003609 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003610 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003613static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3614{
Trond Myklebust9b206142013-03-17 15:52:00 -04003615 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003616 &data->args.seq_args,
3617 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003618 task))
3619 return;
3620 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3621 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Fred Isamanb029bc92011-03-03 15:13:42 +00003624static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Fred Isamancd841602012-04-20 14:47:44 -04003626 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003628 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003629 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003630 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003632 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003634 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003635 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
Trond Myklebust5521abf2013-03-16 20:54:34 -04003639static bool nfs4_write_stateid_changed(struct rpc_task *task,
3640 struct nfs_writeargs *args)
3641{
3642
3643 if (!nfs4_error_stateid_expired(task->tk_status) ||
3644 nfs4_stateid_is_current(&args->stateid,
3645 args->context,
3646 args->lock_context,
3647 FMODE_WRITE))
3648 return false;
3649 rpc_restart_call_prepare(task);
3650 return true;
3651}
3652
Fred Isamanb029bc92011-03-03 15:13:42 +00003653static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3654{
3655 if (!nfs4_sequence_done(task, &data->res.seq_res))
3656 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003657 if (nfs4_write_stateid_changed(task, &data->args))
3658 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003659 return data->write_done_cb ? data->write_done_cb(task, data) :
3660 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003661}
3662
Trond Myklebust5a37f852012-04-28 14:55:16 -04003663static
3664bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003665{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003666 const struct nfs_pgio_header *hdr = data->header;
3667
3668 /* Don't request attributes for pNFS or O_DIRECT writes */
3669 if (data->ds_clp != NULL || hdr->dreq != NULL)
3670 return false;
3671 /* Otherwise, request attributes if and only if we don't hold
3672 * a delegation
3673 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003674 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003675}
Fred Isamana69aef12011-03-03 15:13:47 +00003676
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003677static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Fred Isamancd841602012-04-20 14:47:44 -04003679 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003680
Trond Myklebust5a37f852012-04-28 14:55:16 -04003681 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003682 data->args.bitmask = NULL;
3683 data->res.fattr = NULL;
3684 } else
3685 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003686
Fred Isamanb029bc92011-03-03 15:13:42 +00003687 if (!data->write_done_cb)
3688 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003689 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 data->timestamp = jiffies;
3691
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003692 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003693 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003696static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3697{
Trond Myklebust9b206142013-03-17 15:52:00 -04003698 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003699 &data->args.seq_args,
3700 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003701 task))
3702 return;
3703 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3704 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
Fred Isaman0b7c0152012-04-20 14:47:39 -04003707static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3708{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003709 nfs4_setup_sequence(NFS_SERVER(data->inode),
3710 &data->args.seq_args,
3711 &data->res.seq_res,
3712 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Fred Isaman0b7c0152012-04-20 14:47:39 -04003715static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003718
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003719 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003720 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003721 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
Fred Isaman0b7c0152012-04-20 14:47:39 -04003726static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003727{
3728 if (!nfs4_sequence_done(task, &data->res.seq_res))
3729 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003730 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003731}
3732
Fred Isaman0b7c0152012-04-20 14:47:39 -04003733static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003735 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003736
Fred Isaman0b7c0152012-04-20 14:47:39 -04003737 if (data->commit_done_cb == NULL)
3738 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003739 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003740 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003741 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003744struct nfs4_renewdata {
3745 struct nfs_client *client;
3746 unsigned long timestamp;
3747};
3748
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749/*
3750 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3751 * standalone procedure for queueing an asynchronous RENEW.
3752 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003753static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003754{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003755 struct nfs4_renewdata *data = calldata;
3756 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003757
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003758 if (atomic_read(&clp->cl_count) > 1)
3759 nfs4_schedule_state_renewal(clp);
3760 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003761 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003762}
3763
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003764static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003766 struct nfs4_renewdata *data = calldata;
3767 struct nfs_client *clp = data->client;
3768 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003771 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003772 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3773 return;
3774 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003775 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003776 return;
3777 }
3778 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003780 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003783static const struct rpc_call_ops nfs4_renew_ops = {
3784 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003785 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003786};
3787
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003788static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3792 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003793 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003795 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003797 if (renew_flags == 0)
3798 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003799 if (!atomic_inc_not_zero(&clp->cl_count))
3800 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003801 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003802 if (data == NULL)
3803 return -ENOMEM;
3804 data->client = clp;
3805 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003806 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003807 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003810static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3814 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003815 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 };
3817 unsigned long now = jiffies;
3818 int status;
3819
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003820 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (status < 0)
3822 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003823 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return 0;
3825}
3826
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003827static inline int nfs4_server_supports_acls(struct nfs_server *server)
3828{
3829 return (server->caps & NFS_CAP_ACLS)
3830 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3831 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3832}
3833
Trond Myklebust21f498c2012-08-24 10:59:25 -04003834/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3835 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003836 * the stack.
3837 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003838#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003839
Neil Hormane9e3d722011-03-04 19:26:03 -05003840static int buf_to_pages_noslab(const void *buf, size_t buflen,
3841 struct page **pages, unsigned int *pgbase)
3842{
3843 struct page *newpage, **spages;
3844 int rc = 0;
3845 size_t len;
3846 spages = pages;
3847
3848 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003849 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003850 newpage = alloc_page(GFP_KERNEL);
3851
3852 if (newpage == NULL)
3853 goto unwind;
3854 memcpy(page_address(newpage), buf, len);
3855 buf += len;
3856 buflen -= len;
3857 *pages++ = newpage;
3858 rc++;
3859 } while (buflen != 0);
3860
3861 return rc;
3862
3863unwind:
3864 for(; rc > 0; rc--)
3865 __free_page(spages[rc-1]);
3866 return -ENOMEM;
3867}
3868
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869struct nfs4_cached_acl {
3870 int cached;
3871 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003872 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003873};
3874
3875static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003876{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003877 struct nfs_inode *nfsi = NFS_I(inode);
3878
3879 spin_lock(&inode->i_lock);
3880 kfree(nfsi->nfs4_acl);
3881 nfsi->nfs4_acl = acl;
3882 spin_unlock(&inode->i_lock);
3883}
3884
3885static void nfs4_zap_acl_attr(struct inode *inode)
3886{
3887 nfs4_set_cached_acl(inode, NULL);
3888}
3889
3890static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3891{
3892 struct nfs_inode *nfsi = NFS_I(inode);
3893 struct nfs4_cached_acl *acl;
3894 int ret = -ENOENT;
3895
3896 spin_lock(&inode->i_lock);
3897 acl = nfsi->nfs4_acl;
3898 if (acl == NULL)
3899 goto out;
3900 if (buf == NULL) /* user is just asking for length */
3901 goto out_len;
3902 if (acl->cached == 0)
3903 goto out;
3904 ret = -ERANGE; /* see getxattr(2) man page */
3905 if (acl->len > buflen)
3906 goto out;
3907 memcpy(buf, acl->data, acl->len);
3908out_len:
3909 ret = acl->len;
3910out:
3911 spin_unlock(&inode->i_lock);
3912 return ret;
3913}
3914
Sachin Prabhu5794d212012-04-17 14:36:40 +01003915static 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 +00003916{
3917 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003918 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003919
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003920 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003921 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003922 if (acl == NULL)
3923 goto out;
3924 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003925 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003926 } else {
3927 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3928 if (acl == NULL)
3929 goto out;
3930 acl->cached = 0;
3931 }
3932 acl->len = acl_len;
3933out:
3934 nfs4_set_cached_acl(inode, acl);
3935}
3936
Andy Adamsonbf118a32011-12-07 11:55:27 -05003937/*
3938 * The getxattr API returns the required buffer length when called with a
3939 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3940 * the required buf. On a NULL buf, we send a page of data to the server
3941 * guessing that the ACL request can be serviced by a page. If so, we cache
3942 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3943 * the cache. If not so, we throw away the page, and cache the required
3944 * length. The next getxattr call will then produce another round trip to
3945 * the server, this time with the input buf of the required size.
3946 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003947static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003948{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003949 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003950 struct nfs_getaclargs args = {
3951 .fh = NFS_FH(inode),
3952 .acl_pages = pages,
3953 .acl_len = buflen,
3954 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003955 struct nfs_getaclres res = {
3956 .acl_len = buflen,
3957 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3960 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003961 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003962 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003963 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3964 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003965
Andy Adamsonbf118a32011-12-07 11:55:27 -05003966 /* As long as we're doing a round trip to the server anyway,
3967 * let's be prepared for a page of acl data. */
3968 if (npages == 0)
3969 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003970 if (npages > ARRAY_SIZE(pages))
3971 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003972
Andy Adamsonbf118a32011-12-07 11:55:27 -05003973 for (i = 0; i < npages; i++) {
3974 pages[i] = alloc_page(GFP_KERNEL);
3975 if (!pages[i])
3976 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003977 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003978
3979 /* for decoding across pages */
3980 res.acl_scratch = alloc_page(GFP_KERNEL);
3981 if (!res.acl_scratch)
3982 goto out_free;
3983
Andy Adamsonbf118a32011-12-07 11:55:27 -05003984 args.acl_len = npages * PAGE_SIZE;
3985 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003986
Peng Taode040be2012-01-10 22:42:47 +08003987 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003988 __func__, buf, buflen, npages, args.acl_len);
3989 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3990 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003991 if (ret)
3992 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003993
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003994 /* Handle the case where the passed-in buffer is too short */
3995 if (res.acl_flags & NFS4_ACL_TRUNC) {
3996 /* Did the user only issue a request for the acl length? */
3997 if (buf == NULL)
3998 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003999 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004000 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004001 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004002 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004003 if (buf) {
4004 if (res.acl_len > buflen) {
4005 ret = -ERANGE;
4006 goto out_free;
4007 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004008 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004009 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004010out_ok:
4011 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004012out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004013 for (i = 0; i < npages; i++)
4014 if (pages[i])
4015 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004016 if (res.acl_scratch)
4017 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004018 return ret;
4019}
4020
Trond Myklebust16b42892006-08-24 12:27:15 -04004021static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4022{
4023 struct nfs4_exception exception = { };
4024 ssize_t ret;
4025 do {
4026 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4027 if (ret >= 0)
4028 break;
4029 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4030 } while (exception.retry);
4031 return ret;
4032}
4033
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004034static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4035{
4036 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004037 int ret;
4038
4039 if (!nfs4_server_supports_acls(server))
4040 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004041 ret = nfs_revalidate_inode(server, inode);
4042 if (ret < 0)
4043 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004044 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4045 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004046 ret = nfs4_read_cached_acl(inode, buf, buflen);
4047 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004048 /* -ENOENT is returned if there is no ACL or if there is an ACL
4049 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004050 return ret;
4051 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004052}
4053
Trond Myklebust16b42892006-08-24 12:27:15 -04004054static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004055{
4056 struct nfs_server *server = NFS_SERVER(inode);
4057 struct page *pages[NFS4ACL_MAXPAGES];
4058 struct nfs_setaclargs arg = {
4059 .fh = NFS_FH(inode),
4060 .acl_pages = pages,
4061 .acl_len = buflen,
4062 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004063 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004064 struct rpc_message msg = {
4065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4066 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004067 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004068 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004069 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004070 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004071
4072 if (!nfs4_server_supports_acls(server))
4073 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004074 if (npages > ARRAY_SIZE(pages))
4075 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004076 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4077 if (i < 0)
4078 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004079 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004080 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004081
4082 /*
4083 * Free each page after tx, so the only ref left is
4084 * held by the network stack
4085 */
4086 for (; i > 0; i--)
4087 put_page(pages[i-1]);
4088
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004089 /*
4090 * Acl update can result in inode attribute update.
4091 * so mark the attribute cache invalid.
4092 */
4093 spin_lock(&inode->i_lock);
4094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4095 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004096 nfs_access_zap_cache(inode);
4097 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004098 return ret;
4099}
4100
Trond Myklebust16b42892006-08-24 12:27:15 -04004101static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4102{
4103 struct nfs4_exception exception = { };
4104 int err;
4105 do {
4106 err = nfs4_handle_exception(NFS_SERVER(inode),
4107 __nfs4_proc_set_acl(inode, buf, buflen),
4108 &exception);
4109 } while (exception.retry);
4110 return err;
4111}
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004114nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004116 struct nfs_client *clp = server->nfs_client;
4117
4118 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return 0;
4120 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004121 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004122 case -NFS4ERR_ADMIN_REVOKED:
4123 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004124 if (state == NULL)
4125 break;
4126 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004127 case -NFS4ERR_OPENMODE:
4128 if (state == NULL)
4129 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004130 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4131 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004132 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004133 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004134 if (state != NULL) {
4135 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4136 goto stateid_invalid;
4137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004139 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004140 nfs4_schedule_lease_recovery(clp);
4141 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004142#if defined(CONFIG_NFS_V4_1)
4143 case -NFS4ERR_BADSESSION:
4144 case -NFS4ERR_BADSLOT:
4145 case -NFS4ERR_BAD_HIGH_SLOT:
4146 case -NFS4ERR_DEADSESSION:
4147 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4148 case -NFS4ERR_SEQ_FALSE_RETRY:
4149 case -NFS4ERR_SEQ_MISORDERED:
4150 dprintk("%s ERROR %d, Reset session\n", __func__,
4151 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004152 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004153 task->tk_status = 0;
4154 return -EAGAIN;
4155#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004157 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004158 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4160 task->tk_status = 0;
4161 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004162 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 case -NFS4ERR_OLD_STATEID:
4164 task->tk_status = 0;
4165 return -EAGAIN;
4166 }
4167 task->tk_status = nfs4_map_errors(task->tk_status);
4168 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004169stateid_invalid:
4170 task->tk_status = -EIO;
4171 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004172wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004173 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004174 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4175 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4176 task->tk_status = 0;
4177 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
4179
Chuck Leverf0920752012-05-21 22:45:41 -04004180static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4181 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004182{
4183 __be32 verf[2];
4184
Chuck Lever2c820d92012-05-21 22:45:33 -04004185 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4186 /* An impossible timestamp guarantees this value
4187 * will never match a generated boot time. */
4188 verf[0] = 0;
4189 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4190 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004191 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4192 verf[0] = (__be32)nn->boot_time.tv_sec;
4193 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004194 }
Chuck Levercd937102012-03-02 17:14:31 -05004195 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4196}
4197
Chuck Levere984a552012-09-14 17:24:21 -04004198static unsigned int
4199nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4200 char *buf, size_t len)
4201{
4202 unsigned int result;
4203
4204 rcu_read_lock();
4205 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4206 clp->cl_ipaddr,
4207 rpc_peeraddr2str(clp->cl_rpcclient,
4208 RPC_DISPLAY_ADDR),
4209 rpc_peeraddr2str(clp->cl_rpcclient,
4210 RPC_DISPLAY_PROTO));
4211 rcu_read_unlock();
4212 return result;
4213}
4214
4215static unsigned int
4216nfs4_init_uniform_client_string(const struct nfs_client *clp,
4217 char *buf, size_t len)
4218{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004219 char *nodename = clp->cl_rpcclient->cl_nodename;
4220
4221 if (nfs4_client_id_uniquifier[0] != '\0')
4222 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004223 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4224 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004225 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004226}
4227
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004228/**
4229 * nfs4_proc_setclientid - Negotiate client ID
4230 * @clp: state data structure
4231 * @program: RPC program for NFSv4 callback service
4232 * @port: IP port number for NFS4 callback service
4233 * @cred: RPC credential to use for this call
4234 * @res: where to place the result
4235 *
4236 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4237 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004238int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4239 unsigned short port, struct rpc_cred *cred,
4240 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
4242 nfs4_verifier sc_verifier;
4243 struct nfs4_setclientid setclientid = {
4244 .sc_verifier = &sc_verifier,
4245 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004246 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 };
4248 struct rpc_message msg = {
4249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4250 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004251 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004252 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004254 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Chuck Leverde734832012-07-11 16:30:50 -04004256 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004257 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004258 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4259 setclientid.sc_name_len =
4260 nfs4_init_uniform_client_string(clp,
4261 setclientid.sc_name,
4262 sizeof(setclientid.sc_name));
4263 else
4264 setclientid.sc_name_len =
4265 nfs4_init_nonuniform_client_string(clp,
4266 setclientid.sc_name,
4267 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004268 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004269 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004270 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004271 sizeof(setclientid.sc_netid),
4272 rpc_peeraddr2str(clp->cl_rpcclient,
4273 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004274 rcu_read_unlock();
4275 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004276 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 clp->cl_ipaddr, port >> 8, port & 255);
4278
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004279 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4280 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4281 setclientid.sc_name_len, setclientid.sc_name);
4282 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4283 dprintk("NFS reply setclientid: %d\n", status);
4284 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004287/**
4288 * nfs4_proc_setclientid_confirm - Confirm client ID
4289 * @clp: state data structure
4290 * @res: result of a previous SETCLIENTID
4291 * @cred: RPC credential to use for this call
4292 *
4293 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4294 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004295int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004296 struct nfs4_setclientid_res *arg,
4297 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
4299 struct nfs_fsinfo fsinfo;
4300 struct rpc_message msg = {
4301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004302 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004304 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 };
4306 unsigned long now;
4307 int status;
4308
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004309 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4310 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4311 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004313 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (status == 0) {
4315 spin_lock(&clp->cl_lock);
4316 clp->cl_lease_time = fsinfo.lease_time * HZ;
4317 clp->cl_last_renewal = now;
4318 spin_unlock(&clp->cl_lock);
4319 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004320 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return status;
4322}
4323
Trond Myklebustfe650402006-01-03 09:55:18 +01004324struct nfs4_delegreturndata {
4325 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004326 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004327 struct nfs_fh fh;
4328 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004329 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004330 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004331 int rpc_status;
4332};
4333
Trond Myklebustfe650402006-01-03 09:55:18 +01004334static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4335{
4336 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004337
Trond Myklebust14516c32010-07-31 14:29:06 -04004338 if (!nfs4_sequence_done(task, &data->res.seq_res))
4339 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004340
Ricardo Labiaga79708862009-12-07 09:23:21 -05004341 switch (task->tk_status) {
4342 case -NFS4ERR_STALE_STATEID:
4343 case -NFS4ERR_EXPIRED:
4344 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004345 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004346 break;
4347 default:
4348 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4349 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004350 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004351 return;
4352 }
4353 }
4354 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004355}
4356
4357static void nfs4_delegreturn_release(void *calldata)
4358{
Trond Myklebustfe650402006-01-03 09:55:18 +01004359 kfree(calldata);
4360}
4361
Andy Adamson938e1012009-04-01 09:22:28 -04004362#if defined(CONFIG_NFS_V4_1)
4363static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4364{
4365 struct nfs4_delegreturndata *d_data;
4366
4367 d_data = (struct nfs4_delegreturndata *)data;
4368
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004369 nfs4_setup_sequence(d_data->res.server,
4370 &d_data->args.seq_args,
4371 &d_data->res.seq_res,
4372 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004373}
4374#endif /* CONFIG_NFS_V4_1 */
4375
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004376static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004377#if defined(CONFIG_NFS_V4_1)
4378 .rpc_call_prepare = nfs4_delegreturn_prepare,
4379#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004380 .rpc_call_done = nfs4_delegreturn_done,
4381 .rpc_release = nfs4_delegreturn_release,
4382};
4383
Trond Myklebuste6f81072008-01-24 18:14:34 -05004384static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004385{
4386 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004387 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004388 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004389 struct rpc_message msg = {
4390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4391 .rpc_cred = cred,
4392 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004393 struct rpc_task_setup task_setup_data = {
4394 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004395 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004396 .callback_ops = &nfs4_delegreturn_ops,
4397 .flags = RPC_TASK_ASYNC,
4398 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004399 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004400
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004401 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004402 if (data == NULL)
4403 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004404 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004405 data->args.fhandle = &data->fh;
4406 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004407 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004408 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004409 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004410 data->res.fattr = &data->fattr;
4411 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004412 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004413 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004414 data->rpc_status = 0;
4415
Trond Myklebustc970aa82007-07-14 15:39:59 -04004416 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004417 msg.rpc_argp = &data->args;
4418 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004419 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004420 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004421 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004422 if (!issync)
4423 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004424 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004425 if (status != 0)
4426 goto out;
4427 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004428 if (status == 0)
4429 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4430 else
4431 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004432out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004433 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004434 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Trond Myklebuste6f81072008-01-24 18:14:34 -05004437int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
4439 struct nfs_server *server = NFS_SERVER(inode);
4440 struct nfs4_exception exception = { };
4441 int err;
4442 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004443 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 switch (err) {
4445 case -NFS4ERR_STALE_STATEID:
4446 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 case 0:
4448 return 0;
4449 }
4450 err = nfs4_handle_exception(server, err, &exception);
4451 } while (exception.retry);
4452 return err;
4453}
4454
4455#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4456#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4457
4458/*
4459 * sleep, with exponential backoff, and retry the LOCK operation.
4460 */
4461static unsigned long
4462nfs4_set_lock_task_retry(unsigned long timeout)
4463{
Jeff Laytond3103102011-12-01 22:44:39 +01004464 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 timeout <<= 1;
4466 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4467 return NFS4_LOCK_MAXTIMEOUT;
4468 return timeout;
4469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4472{
4473 struct inode *inode = state->inode;
4474 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004475 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004476 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004478 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004480 struct nfs_lockt_res res = {
4481 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 };
4483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4485 .rpc_argp = &arg,
4486 .rpc_resp = &res,
4487 .rpc_cred = state->owner->so_cred,
4488 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct nfs4_lock_state *lsp;
4490 int status;
4491
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004492 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004493 status = nfs4_set_lock_state(state, request);
4494 if (status != 0)
4495 goto out;
4496 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004497 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004498 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004499 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004500 switch (status) {
4501 case 0:
4502 request->fl_type = F_UNLCK;
4503 break;
4504 case -NFS4ERR_DENIED:
4505 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004507 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004508out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return status;
4510}
4511
4512static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4513{
4514 struct nfs4_exception exception = { };
4515 int err;
4516
4517 do {
4518 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4519 _nfs4_proc_getlk(state, cmd, request),
4520 &exception);
4521 } while (exception.retry);
4522 return err;
4523}
4524
4525static int do_vfs_lock(struct file *file, struct file_lock *fl)
4526{
4527 int res = 0;
4528 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4529 case FL_POSIX:
4530 res = posix_lock_file_wait(file, fl);
4531 break;
4532 case FL_FLOCK:
4533 res = flock_lock_file_wait(file, fl);
4534 break;
4535 default:
4536 BUG();
4537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return res;
4539}
4540
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004541struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004542 struct nfs_locku_args arg;
4543 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004544 struct nfs4_lock_state *lsp;
4545 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004546 struct file_lock fl;
4547 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004548 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004549};
4550
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004551static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4552 struct nfs_open_context *ctx,
4553 struct nfs4_lock_state *lsp,
4554 struct nfs_seqid *seqid)
4555{
4556 struct nfs4_unlockdata *p;
4557 struct inode *inode = lsp->ls_state->inode;
4558
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004559 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004560 if (p == NULL)
4561 return NULL;
4562 p->arg.fh = NFS_FH(inode);
4563 p->arg.fl = &p->fl;
4564 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004565 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004566 p->arg.stateid = &lsp->ls_stateid;
4567 p->lsp = lsp;
4568 atomic_inc(&lsp->ls_count);
4569 /* Ensure we don't close file until we're done freeing locks! */
4570 p->ctx = get_nfs_open_context(ctx);
4571 memcpy(&p->fl, fl, sizeof(p->fl));
4572 p->server = NFS_SERVER(inode);
4573 return p;
4574}
4575
Trond Myklebust06f814a2006-01-03 09:55:07 +01004576static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004577{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004578 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004579 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004580 nfs4_put_lock_state(calldata->lsp);
4581 put_nfs_open_context(calldata->ctx);
4582 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004583}
4584
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004585static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004586{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004587 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004588
Trond Myklebust14516c32010-07-31 14:29:06 -04004589 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4590 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004591 switch (task->tk_status) {
4592 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004593 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4594 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004595 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004596 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004597 case -NFS4ERR_BAD_STATEID:
4598 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004599 case -NFS4ERR_STALE_STATEID:
4600 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601 break;
4602 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004603 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004604 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004605 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004606 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004607}
4608
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004609static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004610{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004611 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004613 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004614 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004615 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004616 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004617 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004618 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004619 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004620 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004621 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004622 &calldata->res.seq_res,
4623 task) != 0)
4624 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004625 return;
4626out_no_action:
4627 task->tk_action = NULL;
4628out_wait:
4629 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004632static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004633 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004634 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004635 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004636};
4637
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004638static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4639 struct nfs_open_context *ctx,
4640 struct nfs4_lock_state *lsp,
4641 struct nfs_seqid *seqid)
4642{
4643 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004644 struct rpc_message msg = {
4645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4646 .rpc_cred = ctx->cred,
4647 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004648 struct rpc_task_setup task_setup_data = {
4649 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004650 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004651 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004652 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004653 .flags = RPC_TASK_ASYNC,
4654 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655
Frank Filz137d6ac2007-07-09 15:32:29 -07004656 /* Ensure this is an unlock - when canceling a lock, the
4657 * canceled lock is passed in, and it won't be an unlock.
4658 */
4659 fl->fl_type = F_UNLCK;
4660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004661 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4662 if (data == NULL) {
4663 nfs_free_seqid(seqid);
4664 return ERR_PTR(-ENOMEM);
4665 }
4666
Trond Myklebust9d12b212012-01-17 22:04:25 -05004667 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004668 msg.rpc_argp = &data->arg;
4669 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004670 task_setup_data.callback_data = data;
4671 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672}
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4675{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004676 struct inode *inode = state->inode;
4677 struct nfs4_state_owner *sp = state->owner;
4678 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004679 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004680 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004681 struct rpc_task *task;
4682 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004683 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Trond Myklebust9b073572006-06-29 16:38:34 -04004685 status = nfs4_set_lock_state(state, request);
4686 /* Unlock _before_ we do the RPC call */
4687 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004688 /* Exclude nfs_delegation_claim_locks() */
4689 mutex_lock(&sp->so_delegreturn_mutex);
4690 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004691 down_read(&nfsi->rwsem);
4692 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4693 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004694 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004695 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004696 }
4697 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004698 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004699 if (status != 0)
4700 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004701 /* Is this a delegated lock? */
4702 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004703 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004704 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004705 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004706 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004707 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004708 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004709 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004710 status = PTR_ERR(task);
4711 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004712 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004713 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004714 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004715out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004716 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004717 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720struct nfs4_lockdata {
4721 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004722 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 struct nfs4_lock_state *lsp;
4724 struct nfs_open_context *ctx;
4725 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004726 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004727 int rpc_status;
4728 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004729 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004730};
4731
4732static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004733 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4734 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735{
4736 struct nfs4_lockdata *p;
4737 struct inode *inode = lsp->ls_state->inode;
4738 struct nfs_server *server = NFS_SERVER(inode);
4739
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004740 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004741 if (p == NULL)
4742 return NULL;
4743
4744 p->arg.fh = NFS_FH(inode);
4745 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004746 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004747 if (p->arg.open_seqid == NULL)
4748 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004749 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004750 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004751 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004753 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004754 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004755 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004756 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004758 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759 atomic_inc(&lsp->ls_count);
4760 p->ctx = get_nfs_open_context(ctx);
4761 memcpy(&p->fl, fl, sizeof(p->fl));
4762 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004763out_free_seqid:
4764 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765out_free:
4766 kfree(p);
4767 return NULL;
4768}
4769
4770static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4771{
4772 struct nfs4_lockdata *data = calldata;
4773 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Harvey Harrison3110ff82008-05-02 13:42:44 -07004775 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004776 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004777 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004778 /* Do we need to do an open_to_lock_owner? */
4779 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004780 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004781 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004782 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004783 data->arg.open_stateid = &state->stateid;
4784 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004785 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004786 } else
4787 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004788 if (!nfs4_valid_open_stateid(state)) {
4789 data->rpc_status = -EBADF;
4790 task->tk_action = NULL;
4791 goto out_release_open_seqid;
4792 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004793 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004794 if (nfs4_setup_sequence(data->server,
4795 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004796 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004797 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004798 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004799out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004800 nfs_release_seqid(data->arg.open_seqid);
4801out_release_lock_seqid:
4802 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004803out_wait:
4804 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004805 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004806}
4807
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004808static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4809{
4810 struct nfs4_lockdata *data = calldata;
4811
Harvey Harrison3110ff82008-05-02 13:42:44 -07004812 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004813
Trond Myklebust14516c32010-07-31 14:29:06 -04004814 if (!nfs4_sequence_done(task, &data->res.seq_res))
4815 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004816
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004817 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004819 if (data->rpc_status == 0)
4820 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4821 else
4822 goto out;
4823 }
4824 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004825 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004826 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004827 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004828 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004829out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004830 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831}
4832
4833static void nfs4_lock_release(void *calldata)
4834{
4835 struct nfs4_lockdata *data = calldata;
4836
Harvey Harrison3110ff82008-05-02 13:42:44 -07004837 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004838 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004839 if (data->cancelled != 0) {
4840 struct rpc_task *task;
4841 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4842 data->arg.lock_seqid);
4843 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004844 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004845 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004846 } else
4847 nfs_free_seqid(data->arg.lock_seqid);
4848 nfs4_put_lock_state(data->lsp);
4849 put_nfs_open_context(data->ctx);
4850 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004851 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852}
4853
4854static const struct rpc_call_ops nfs4_lock_ops = {
4855 .rpc_call_prepare = nfs4_lock_prepare,
4856 .rpc_call_done = nfs4_lock_done,
4857 .rpc_release = nfs4_lock_release,
4858};
4859
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004860static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4861{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004862 switch (error) {
4863 case -NFS4ERR_ADMIN_REVOKED:
4864 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004865 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004866 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004867 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004868 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004869 break;
4870 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004871 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004872 case -NFS4ERR_EXPIRED:
4873 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004874 };
4875}
4876
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004877static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004878{
4879 struct nfs4_lockdata *data;
4880 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004881 struct rpc_message msg = {
4882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4883 .rpc_cred = state->owner->so_cred,
4884 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004885 struct rpc_task_setup task_setup_data = {
4886 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004887 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004888 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004889 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004890 .flags = RPC_TASK_ASYNC,
4891 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004892 int ret;
4893
Harvey Harrison3110ff82008-05-02 13:42:44 -07004894 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004895 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004896 fl->fl_u.nfs4_fl.owner,
4897 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004898 if (data == NULL)
4899 return -ENOMEM;
4900 if (IS_SETLKW(cmd))
4901 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004902 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004903 msg.rpc_argp = &data->arg;
4904 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004905 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004906 if (recovery_type > NFS_LOCK_NEW) {
4907 if (recovery_type == NFS_LOCK_RECLAIM)
4908 data->arg.reclaim = NFS_LOCK_RECLAIM;
4909 nfs4_set_sequence_privileged(&data->arg.seq_args);
4910 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004911 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004912 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004913 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004914 ret = nfs4_wait_for_completion_rpc_task(task);
4915 if (ret == 0) {
4916 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004917 if (ret)
4918 nfs4_handle_setlk_error(data->server, data->lsp,
4919 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004920 } else
4921 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004922 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004923 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}
4926
4927static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4928{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004929 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004930 struct nfs4_exception exception = {
4931 .inode = state->inode,
4932 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004933 int err;
4934
4935 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004936 /* Cache the lock if possible... */
4937 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4938 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004939 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004940 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004941 break;
4942 nfs4_handle_exception(server, err, &exception);
4943 } while (exception.retry);
4944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
4947static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4948{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004949 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004950 struct nfs4_exception exception = {
4951 .inode = state->inode,
4952 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004953 int err;
4954
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004955 err = nfs4_set_lock_state(state, request);
4956 if (err != 0)
4957 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004958 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4960 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004961 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004962 switch (err) {
4963 default:
4964 goto out;
4965 case -NFS4ERR_GRACE:
4966 case -NFS4ERR_DELAY:
4967 nfs4_handle_exception(server, err, &exception);
4968 err = 0;
4969 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004970 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004971out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004975#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004976/**
4977 * nfs41_check_expired_locks - possibly free a lock stateid
4978 *
4979 * @state: NFSv4 state for an inode
4980 *
4981 * Returns NFS_OK if recovery for this stateid is now finished.
4982 * Otherwise a negative NFS4ERR value is returned.
4983 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004984static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004985{
Chuck Levereb64cf92012-07-11 16:30:05 -04004986 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004987 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004988 struct nfs_server *server = NFS_SERVER(state->inode);
4989
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004990 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004991 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004992 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4993 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004994 /* Free the stateid unless the server
4995 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004996 if (status != -NFS4ERR_BAD_STATEID)
4997 nfs41_free_stateid(server,
4998 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004999 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005000 ret = status;
5001 }
5002 }
5003 };
5004
5005 return ret;
5006}
5007
5008static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5009{
5010 int status = NFS_OK;
5011
5012 if (test_bit(LK_STATE_IN_USE, &state->flags))
5013 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005014 if (status != NFS_OK)
5015 status = nfs4_lock_expired(state, request);
5016 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005017}
5018#endif
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5021{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005022 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005023 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005024 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005025 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005026 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005028 if ((fl_flags & FL_POSIX) &&
5029 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5030 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005031 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005032 status = nfs4_set_lock_state(state, request);
5033 if (status != 0)
5034 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005035 request->fl_flags |= FL_ACCESS;
5036 status = do_vfs_lock(request->fl_file, request);
5037 if (status < 0)
5038 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005039 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005040 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005041 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005042 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005043 request->fl_flags = fl_flags & ~FL_SLEEP;
5044 status = do_vfs_lock(request->fl_file, request);
5045 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005046 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005047 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5048 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005049 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005050 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005051 goto out;
5052 down_read(&nfsi->rwsem);
5053 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5054 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005055 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005056 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005057 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005059 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005060 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5061 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005062out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005063 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005064out:
5065 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 return status;
5067}
5068
5069static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5070{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005071 struct nfs4_exception exception = {
5072 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005073 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 int err;
5076
5077 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005078 err = _nfs4_proc_setlk(state, cmd, request);
5079 if (err == -NFS4ERR_DENIED)
5080 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005082 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 } while (exception.retry);
5084 return err;
5085}
5086
5087static int
5088nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5089{
5090 struct nfs_open_context *ctx;
5091 struct nfs4_state *state;
5092 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5093 int status;
5094
5095 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005096 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 state = ctx->state;
5098
5099 if (request->fl_start < 0 || request->fl_end < 0)
5100 return -EINVAL;
5101
Trond Myklebustd9531262009-07-21 19:22:38 -04005102 if (IS_GETLK(cmd)) {
5103 if (state != NULL)
5104 return nfs4_proc_getlk(state, F_GETLK, request);
5105 return 0;
5106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5109 return -EINVAL;
5110
Trond Myklebustd9531262009-07-21 19:22:38 -04005111 if (request->fl_type == F_UNLCK) {
5112 if (state != NULL)
5113 return nfs4_proc_unlck(state, cmd, request);
5114 return 0;
5115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Trond Myklebustd9531262009-07-21 19:22:38 -04005117 if (state == NULL)
5118 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005119 /*
5120 * Don't rely on the VFS having checked the file open mode,
5121 * since it won't do this for flock() locks.
5122 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005123 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005124 case F_RDLCK:
5125 if (!(filp->f_mode & FMODE_READ))
5126 return -EBADF;
5127 break;
5128 case F_WRLCK:
5129 if (!(filp->f_mode & FMODE_WRITE))
5130 return -EBADF;
5131 }
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 do {
5134 status = nfs4_proc_setlk(state, cmd, request);
5135 if ((status != -EAGAIN) || IS_SETLK(cmd))
5136 break;
5137 timeout = nfs4_set_lock_task_retry(timeout);
5138 status = -ERESTARTSYS;
5139 if (signalled())
5140 break;
5141 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return status;
5143}
5144
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005145int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005146{
5147 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005148 int err;
5149
5150 err = nfs4_set_lock_state(state, fl);
5151 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005152 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005153 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005154 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005155}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005156
Trond Myklebustcf470c32012-03-07 13:49:12 -05005157struct nfs_release_lockowner_data {
5158 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005159 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005160 struct nfs_release_lockowner_args args;
5161};
5162
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005163static void nfs4_release_lockowner_release(void *calldata)
5164{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005165 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005166 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005167 kfree(calldata);
5168}
5169
Trond Myklebust17280172012-03-11 13:11:00 -04005170static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005171 .rpc_release = nfs4_release_lockowner_release,
5172};
5173
Trond Myklebustcf470c32012-03-07 13:49:12 -05005174int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005175{
5176 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005177 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5180 };
5181
5182 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005183 return -EINVAL;
5184 data = kmalloc(sizeof(*data), GFP_NOFS);
5185 if (!data)
5186 return -ENOMEM;
5187 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005188 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005189 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5190 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5191 data->args.lock_owner.s_dev = server->s_dev;
5192 msg.rpc_argp = &data->args;
5193 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5194 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005195}
5196
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005197#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5198
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005199static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5200 const void *buf, size_t buflen,
5201 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005202{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005203 if (strcmp(key, "") != 0)
5204 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005205
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005206 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005207}
5208
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005209static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5210 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005211{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005212 if (strcmp(key, "") != 0)
5213 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005214
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005215 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005216}
5217
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005218static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5219 size_t list_len, const char *name,
5220 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005221{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005222 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005223
J. Bruce Fields096455a2006-03-20 23:23:42 -05005224 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5225 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005226
5227 if (list && len <= list_len)
5228 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005229 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005230}
5231
Andy Adamson533eb462011-06-13 18:25:56 -04005232/*
5233 * nfs_fhget will use either the mounted_on_fileid or the fileid
5234 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005235static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5236{
Andy Adamson533eb462011-06-13 18:25:56 -04005237 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5238 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5239 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005240 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005241 return;
5242
5243 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005244 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005245 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5246 fattr->nlink = 2;
5247}
5248
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005249static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5250 const struct qstr *name,
5251 struct nfs4_fs_locations *fs_locations,
5252 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005253{
5254 struct nfs_server *server = NFS_SERVER(dir);
5255 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005256 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005257 };
5258 struct nfs4_fs_locations_arg args = {
5259 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005260 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005261 .page = page,
5262 .bitmask = bitmask,
5263 };
Benny Halevy22958462009-04-01 09:22:02 -04005264 struct nfs4_fs_locations_res res = {
5265 .fs_locations = fs_locations,
5266 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005267 struct rpc_message msg = {
5268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5269 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005270 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005271 };
5272 int status;
5273
Harvey Harrison3110ff82008-05-02 13:42:44 -07005274 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005275
5276 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5277 * is not supported */
5278 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5279 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5280 else
5281 bitmask[0] |= FATTR4_WORD0_FILEID;
5282
Trond Myklebustc228fd32007-01-13 02:28:11 -05005283 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005284 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005285 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005286 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005287 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005288 return status;
5289}
5290
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005291int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5292 const struct qstr *name,
5293 struct nfs4_fs_locations *fs_locations,
5294 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005295{
5296 struct nfs4_exception exception = { };
5297 int err;
5298 do {
5299 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005300 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005301 &exception);
5302 } while (exception.retry);
5303 return err;
5304}
5305
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005306static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5307{
5308 int status;
5309 struct nfs4_secinfo_arg args = {
5310 .dir_fh = NFS_FH(dir),
5311 .name = name,
5312 };
5313 struct nfs4_secinfo_res res = {
5314 .flavors = flavors,
5315 };
5316 struct rpc_message msg = {
5317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5318 .rpc_argp = &args,
5319 .rpc_resp = &res,
5320 };
5321
5322 dprintk("NFS call secinfo %s\n", name->name);
5323 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5324 dprintk("NFS reply secinfo: %d\n", status);
5325 return status;
5326}
5327
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005328int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5329 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005330{
5331 struct nfs4_exception exception = { };
5332 int err;
5333 do {
5334 err = nfs4_handle_exception(NFS_SERVER(dir),
5335 _nfs4_proc_secinfo(dir, name, flavors),
5336 &exception);
5337 } while (exception.retry);
5338 return err;
5339}
5340
Andy Adamson557134a2009-04-01 09:21:53 -04005341#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005342/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005343 * Check the exchange flags returned by the server for invalid flags, having
5344 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5345 * DS flags set.
5346 */
5347static int nfs4_check_cl_exchange_flags(u32 flags)
5348{
5349 if (flags & ~EXCHGID4_FLAG_MASK_R)
5350 goto out_inval;
5351 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5352 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5353 goto out_inval;
5354 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5355 goto out_inval;
5356 return NFS_OK;
5357out_inval:
5358 return -NFS4ERR_INVAL;
5359}
5360
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005361static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005362nfs41_same_server_scope(struct nfs41_server_scope *a,
5363 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005364{
5365 if (a->server_scope_sz == b->server_scope_sz &&
5366 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5367 return true;
5368
5369 return false;
5370}
5371
Andy Adamson357f54d2010-12-14 10:11:57 -05005372/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005373 * nfs4_proc_bind_conn_to_session()
5374 *
5375 * The 4.1 client currently uses the same TCP connection for the
5376 * fore and backchannel.
5377 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005378int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005379{
5380 int status;
5381 struct nfs41_bind_conn_to_session_res res;
5382 struct rpc_message msg = {
5383 .rpc_proc =
5384 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5385 .rpc_argp = clp,
5386 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005387 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005388 };
5389
5390 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005391
5392 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5393 if (unlikely(res.session == NULL)) {
5394 status = -ENOMEM;
5395 goto out;
5396 }
5397
5398 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5399 if (status == 0) {
5400 if (memcmp(res.session->sess_id.data,
5401 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5402 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5403 status = -EIO;
5404 goto out_session;
5405 }
5406 if (res.dir != NFS4_CDFS4_BOTH) {
5407 dprintk("NFS: %s: Unexpected direction from server\n",
5408 __func__);
5409 status = -EIO;
5410 goto out_session;
5411 }
5412 if (res.use_conn_in_rdma_mode) {
5413 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5414 __func__);
5415 status = -EIO;
5416 goto out_session;
5417 }
5418 }
5419out_session:
5420 kfree(res.session);
5421out:
5422 dprintk("<-- %s status= %d\n", __func__, status);
5423 return status;
5424}
5425
5426/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005427 * nfs4_proc_exchange_id()
5428 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005429 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5430 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005431 * Since the clientid has expired, all compounds using sessions
5432 * associated with the stale clientid will be returning
5433 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5434 * be in some phase of session reset.
5435 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005436int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005437{
5438 nfs4_verifier verifier;
5439 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005440 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005441 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005442 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005443 };
5444 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005445 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005446 };
5447 int status;
5448 struct rpc_message msg = {
5449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5450 .rpc_argp = &args,
5451 .rpc_resp = &res,
5452 .rpc_cred = cred,
5453 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005454
Chuck Leverf0920752012-05-21 22:45:41 -04005455 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005456 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5457 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005458 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5459 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5460 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005461
Chuck Leveracdeb692012-05-21 22:46:16 -04005462 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005463 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005464 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005465 status = -ENOMEM;
5466 goto out;
5467 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005468
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005469 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005470 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005471 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005472 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005473 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005474 }
5475
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005476 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005477 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005478 status = -ENOMEM;
5479 goto out_server_scope;
5480 }
5481
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005482 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005483 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005484 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005485
Chuck Lever177313f2012-05-21 22:44:58 -04005486 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005487 clp->cl_clientid = res.clientid;
5488 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5489 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5490 clp->cl_seqid = res.seqid;
5491
Chuck Leveracdeb692012-05-21 22:46:16 -04005492 kfree(clp->cl_serverowner);
5493 clp->cl_serverowner = res.server_owner;
5494 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005495
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005496 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005497 kfree(clp->cl_implid);
5498 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005499
Chuck Lever177313f2012-05-21 22:44:58 -04005500 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005501 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005502 res.server_scope)) {
5503 dprintk("%s: server_scope mismatch detected\n",
5504 __func__);
5505 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005506 kfree(clp->cl_serverscope);
5507 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005508 }
5509
Chuck Lever177313f2012-05-21 22:44:58 -04005510 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005511 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005512 goto out;
5513 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005514 } else
5515 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005516
Chuck Leveracdeb692012-05-21 22:46:16 -04005517out_server_owner:
5518 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005519out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005520 kfree(res.server_scope);
5521out:
Chuck Lever177313f2012-05-21 22:44:58 -04005522 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005523 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005524 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005525 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005526 clp->cl_implid->date.seconds,
5527 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005528 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005529 return status;
5530}
5531
Trond Myklebust66245532012-05-25 17:18:09 -04005532static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5533 struct rpc_cred *cred)
5534{
5535 struct rpc_message msg = {
5536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5537 .rpc_argp = clp,
5538 .rpc_cred = cred,
5539 };
5540 int status;
5541
5542 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5543 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005544 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005545 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5546 return status;
5547}
5548
5549static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5550 struct rpc_cred *cred)
5551{
5552 unsigned int loop;
5553 int ret;
5554
5555 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5556 ret = _nfs4_proc_destroy_clientid(clp, cred);
5557 switch (ret) {
5558 case -NFS4ERR_DELAY:
5559 case -NFS4ERR_CLIENTID_BUSY:
5560 ssleep(1);
5561 break;
5562 default:
5563 return ret;
5564 }
5565 }
5566 return 0;
5567}
5568
5569int nfs4_destroy_clientid(struct nfs_client *clp)
5570{
5571 struct rpc_cred *cred;
5572 int ret = 0;
5573
5574 if (clp->cl_mvops->minor_version < 1)
5575 goto out;
5576 if (clp->cl_exchange_flags == 0)
5577 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005578 if (clp->cl_preserve_clid)
5579 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005580 cred = nfs4_get_exchange_id_cred(clp);
5581 ret = nfs4_proc_destroy_clientid(clp, cred);
5582 if (cred)
5583 put_rpccred(cred);
5584 switch (ret) {
5585 case 0:
5586 case -NFS4ERR_STALE_CLIENTID:
5587 clp->cl_exchange_flags = 0;
5588 }
5589out:
5590 return ret;
5591}
5592
Andy Adamson2050f0c2009-04-01 09:22:30 -04005593struct nfs4_get_lease_time_data {
5594 struct nfs4_get_lease_time_args *args;
5595 struct nfs4_get_lease_time_res *res;
5596 struct nfs_client *clp;
5597};
5598
5599static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5600 void *calldata)
5601{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005602 struct nfs4_get_lease_time_data *data =
5603 (struct nfs4_get_lease_time_data *)calldata;
5604
5605 dprintk("--> %s\n", __func__);
5606 /* just setup sequence, do not trigger session recovery
5607 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005608 nfs41_setup_sequence(data->clp->cl_session,
5609 &data->args->la_seq_args,
5610 &data->res->lr_seq_res,
5611 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005612 dprintk("<-- %s\n", __func__);
5613}
5614
5615/*
5616 * Called from nfs4_state_manager thread for session setup, so don't recover
5617 * from sequence operation or clientid errors.
5618 */
5619static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5620{
5621 struct nfs4_get_lease_time_data *data =
5622 (struct nfs4_get_lease_time_data *)calldata;
5623
5624 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005625 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5626 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005627 switch (task->tk_status) {
5628 case -NFS4ERR_DELAY:
5629 case -NFS4ERR_GRACE:
5630 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5631 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5632 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005633 /* fall through */
5634 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005635 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636 return;
5637 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005638 dprintk("<-- %s\n", __func__);
5639}
5640
Trond Myklebust17280172012-03-11 13:11:00 -04005641static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005642 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5643 .rpc_call_done = nfs4_get_lease_time_done,
5644};
5645
5646int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5647{
5648 struct rpc_task *task;
5649 struct nfs4_get_lease_time_args args;
5650 struct nfs4_get_lease_time_res res = {
5651 .lr_fsinfo = fsinfo,
5652 };
5653 struct nfs4_get_lease_time_data data = {
5654 .args = &args,
5655 .res = &res,
5656 .clp = clp,
5657 };
5658 struct rpc_message msg = {
5659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5660 .rpc_argp = &args,
5661 .rpc_resp = &res,
5662 };
5663 struct rpc_task_setup task_setup = {
5664 .rpc_client = clp->cl_rpcclient,
5665 .rpc_message = &msg,
5666 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005667 .callback_data = &data,
5668 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005669 };
5670 int status;
5671
Trond Myklebust9d12b212012-01-17 22:04:25 -05005672 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005673 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005674 dprintk("--> %s\n", __func__);
5675 task = rpc_run_task(&task_setup);
5676
5677 if (IS_ERR(task))
5678 status = PTR_ERR(task);
5679 else {
5680 status = task->tk_status;
5681 rpc_put_task(task);
5682 }
5683 dprintk("<-- %s return %d\n", __func__, status);
5684
5685 return status;
5686}
5687
Andy Adamsonfc931582009-04-01 09:22:31 -04005688/*
5689 * Initialize the values to be used by the client in CREATE_SESSION
5690 * If nfs4_init_session set the fore channel request and response sizes,
5691 * use them.
5692 *
5693 * Set the back channel max_resp_sz_cached to zero to force the client to
5694 * always set csa_cachethis to FALSE because the current implementation
5695 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5696 */
5697static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5698{
5699 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005700 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5701 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005702
5703 if (mxrqst_sz == 0)
5704 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5705 if (mxresp_sz == 0)
5706 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5707 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005708 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5709 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005710 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005711 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005712
5713 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005714 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005715 __func__,
5716 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005717 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005718
5719 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005720 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5721 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5722 args->bc_attrs.max_resp_sz_cached = 0;
5723 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5724 args->bc_attrs.max_reqs = 1;
5725
5726 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5727 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5728 __func__,
5729 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5730 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5731 args->bc_attrs.max_reqs);
5732}
5733
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005734static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005735{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005736 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5737 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5738
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005739 if (rcvd->max_resp_sz > sent->max_resp_sz)
5740 return -EINVAL;
5741 /*
5742 * Our requested max_ops is the minimum we need; we're not
5743 * prepared to break up compounds into smaller pieces than that.
5744 * So, no point even trying to continue if the server won't
5745 * cooperate:
5746 */
5747 if (rcvd->max_ops < sent->max_ops)
5748 return -EINVAL;
5749 if (rcvd->max_reqs == 0)
5750 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005751 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5752 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005753 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005754}
5755
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005756static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5757{
5758 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5759 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005760
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005761 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5762 return -EINVAL;
5763 if (rcvd->max_resp_sz < sent->max_resp_sz)
5764 return -EINVAL;
5765 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5766 return -EINVAL;
5767 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005768 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005769 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005770 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005771 return -EINVAL;
5772 return 0;
5773}
Andy Adamson8d353012009-04-01 09:22:32 -04005774
Andy Adamson8d353012009-04-01 09:22:32 -04005775static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5776 struct nfs4_session *session)
5777{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005778 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005779
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005780 ret = nfs4_verify_fore_channel_attrs(args, session);
5781 if (ret)
5782 return ret;
5783 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005784}
5785
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005786static int _nfs4_proc_create_session(struct nfs_client *clp,
5787 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005788{
5789 struct nfs4_session *session = clp->cl_session;
5790 struct nfs41_create_session_args args = {
5791 .client = clp,
5792 .cb_program = NFS4_CALLBACK,
5793 };
5794 struct nfs41_create_session_res res = {
5795 .client = clp,
5796 };
5797 struct rpc_message msg = {
5798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5799 .rpc_argp = &args,
5800 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005801 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005802 };
5803 int status;
5804
5805 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005806 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005807
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005808 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005809
Trond Myklebust43095d32012-11-20 11:13:12 -05005810 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005811 /* Verify the session's negotiated channel_attrs values */
5812 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005813 /* Increment the clientid slot sequence id */
5814 clp->cl_seqid++;
5815 }
5816
5817 return status;
5818}
5819
5820/*
5821 * Issues a CREATE_SESSION operation to the server.
5822 * It is the responsibility of the caller to verify the session is
5823 * expired before calling this routine.
5824 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005825int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005826{
5827 int status;
5828 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005829 struct nfs4_session *session = clp->cl_session;
5830
5831 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5832
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005833 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005834 if (status)
5835 goto out;
5836
Andy Adamsonaacd5532011-11-09 13:58:21 -05005837 /* Init or reset the session slot tables */
5838 status = nfs4_setup_session_slot_tables(session);
5839 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005840 if (status)
5841 goto out;
5842
5843 ptr = (unsigned *)&session->sess_id.data[0];
5844 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5845 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005846out:
5847 dprintk("<-- %s\n", __func__);
5848 return status;
5849}
5850
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005851/*
5852 * Issue the over-the-wire RPC DESTROY_SESSION.
5853 * The caller must serialize access to this routine.
5854 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005855int nfs4_proc_destroy_session(struct nfs4_session *session,
5856 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005857{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005858 struct rpc_message msg = {
5859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5860 .rpc_argp = session,
5861 .rpc_cred = cred,
5862 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005863 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005864
5865 dprintk("--> nfs4_proc_destroy_session\n");
5866
5867 /* session is still being setup */
5868 if (session->clp->cl_cons_state != NFS_CS_READY)
5869 return status;
5870
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005871 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005872
5873 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005874 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005875 "Session has been destroyed regardless...\n", status);
5876
5877 dprintk("<-- nfs4_proc_destroy_session\n");
5878 return status;
5879}
5880
Trond Myklebust7b38c362012-05-23 13:23:31 -04005881/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005882 * Renew the cl_session lease.
5883 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005884struct nfs4_sequence_data {
5885 struct nfs_client *clp;
5886 struct nfs4_sequence_args args;
5887 struct nfs4_sequence_res res;
5888};
5889
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005890static void nfs41_sequence_release(void *data)
5891{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005892 struct nfs4_sequence_data *calldata = data;
5893 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005894
Alexandros Batsakis71358402010-02-05 03:45:05 -08005895 if (atomic_read(&clp->cl_count) > 1)
5896 nfs4_schedule_state_renewal(clp);
5897 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005898 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005899}
5900
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005901static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5902{
5903 switch(task->tk_status) {
5904 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005905 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5906 return -EAGAIN;
5907 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005908 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005909 }
5910 return 0;
5911}
5912
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005913static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005915 struct nfs4_sequence_data *calldata = data;
5916 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005917
Trond Myklebust14516c32010-07-31 14:29:06 -04005918 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5919 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005920
5921 if (task->tk_status < 0) {
5922 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005923 if (atomic_read(&clp->cl_count) == 1)
5924 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005925
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005926 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5927 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005928 return;
5929 }
5930 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005931 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005932out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005933 dprintk("<-- %s\n", __func__);
5934}
5935
5936static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5937{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005938 struct nfs4_sequence_data *calldata = data;
5939 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005940 struct nfs4_sequence_args *args;
5941 struct nfs4_sequence_res *res;
5942
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005943 args = task->tk_msg.rpc_argp;
5944 res = task->tk_msg.rpc_resp;
5945
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005946 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005947}
5948
5949static const struct rpc_call_ops nfs41_sequence_ops = {
5950 .rpc_call_done = nfs41_sequence_call_done,
5951 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005952 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005953};
5954
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005955static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5956 struct rpc_cred *cred,
5957 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005958{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005959 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005960 struct rpc_message msg = {
5961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5962 .rpc_cred = cred,
5963 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005964 struct rpc_task_setup task_setup_data = {
5965 .rpc_client = clp->cl_rpcclient,
5966 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005967 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005968 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005969 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005970
Alexandros Batsakis71358402010-02-05 03:45:05 -08005971 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005972 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005973 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005974 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005975 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005976 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005978 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005979 if (is_privileged)
5980 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005981 msg.rpc_argp = &calldata->args;
5982 msg.rpc_resp = &calldata->res;
5983 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005984 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005985
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005986 return rpc_run_task(&task_setup_data);
5987}
5988
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005989static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005990{
5991 struct rpc_task *task;
5992 int ret = 0;
5993
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005994 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5995 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005996 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997 if (IS_ERR(task))
5998 ret = PTR_ERR(task);
5999 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006000 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006001 dprintk("<-- %s status=%d\n", __func__, ret);
6002 return ret;
6003}
6004
6005static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6006{
6007 struct rpc_task *task;
6008 int ret;
6009
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006010 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006011 if (IS_ERR(task)) {
6012 ret = PTR_ERR(task);
6013 goto out;
6014 }
6015 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006016 if (!ret) {
6017 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6018
6019 if (task->tk_status == 0)
6020 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006021 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006022 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006023 rpc_put_task(task);
6024out:
6025 dprintk("<-- %s status=%d\n", __func__, ret);
6026 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006027}
6028
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006029struct nfs4_reclaim_complete_data {
6030 struct nfs_client *clp;
6031 struct nfs41_reclaim_complete_args arg;
6032 struct nfs41_reclaim_complete_res res;
6033};
6034
6035static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6036{
6037 struct nfs4_reclaim_complete_data *calldata = data;
6038
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006039 nfs41_setup_sequence(calldata->clp->cl_session,
6040 &calldata->arg.seq_args,
6041 &calldata->res.seq_res,
6042 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006043}
6044
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006045static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6046{
6047 switch(task->tk_status) {
6048 case 0:
6049 case -NFS4ERR_COMPLETE_ALREADY:
6050 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6051 break;
6052 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006053 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006054 /* fall through */
6055 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006056 return -EAGAIN;
6057 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006058 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006059 }
6060 return 0;
6061}
6062
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006063static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6064{
6065 struct nfs4_reclaim_complete_data *calldata = data;
6066 struct nfs_client *clp = calldata->clp;
6067 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6068
6069 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006070 if (!nfs41_sequence_done(task, res))
6071 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006072
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006073 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6074 rpc_restart_call_prepare(task);
6075 return;
6076 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006077 dprintk("<-- %s\n", __func__);
6078}
6079
6080static void nfs4_free_reclaim_complete_data(void *data)
6081{
6082 struct nfs4_reclaim_complete_data *calldata = data;
6083
6084 kfree(calldata);
6085}
6086
6087static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6088 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6089 .rpc_call_done = nfs4_reclaim_complete_done,
6090 .rpc_release = nfs4_free_reclaim_complete_data,
6091};
6092
6093/*
6094 * Issue a global reclaim complete.
6095 */
6096static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6097{
6098 struct nfs4_reclaim_complete_data *calldata;
6099 struct rpc_task *task;
6100 struct rpc_message msg = {
6101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6102 };
6103 struct rpc_task_setup task_setup_data = {
6104 .rpc_client = clp->cl_rpcclient,
6105 .rpc_message = &msg,
6106 .callback_ops = &nfs4_reclaim_complete_call_ops,
6107 .flags = RPC_TASK_ASYNC,
6108 };
6109 int status = -ENOMEM;
6110
6111 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006112 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006113 if (calldata == NULL)
6114 goto out;
6115 calldata->clp = clp;
6116 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006117
Trond Myklebust9d12b212012-01-17 22:04:25 -05006118 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006119 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006120 msg.rpc_argp = &calldata->arg;
6121 msg.rpc_resp = &calldata->res;
6122 task_setup_data.callback_data = calldata;
6123 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006124 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006125 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006126 goto out;
6127 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006128 status = nfs4_wait_for_completion_rpc_task(task);
6129 if (status == 0)
6130 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006131 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006132 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006133out:
6134 dprintk("<-- %s status=%d\n", __func__, status);
6135 return status;
6136}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137
6138static void
6139nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6140{
6141 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006142 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006143 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006144
6145 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006146 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6147 * right now covering the LAYOUTGET we are about to send.
6148 * However, that is not so catastrophic, and there seems
6149 * to be no way to prevent it completely.
6150 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006151 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006152 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006154 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6155 NFS_I(lgp->args.inode)->layout,
6156 lgp->args.ctx->state)) {
6157 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006158 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006159}
6160
6161static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6162{
6163 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006164 struct inode *inode = lgp->args.inode;
6165 struct nfs_server *server = NFS_SERVER(inode);
6166 struct pnfs_layout_hdr *lo;
6167 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006168 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006169
6170 dprintk("--> %s\n", __func__);
6171
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006172 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006173 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006174
6175 switch (task->tk_status) {
6176 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006177 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178 case -NFS4ERR_LAYOUTTRYLATER:
6179 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006180 timeo = rpc_get_timeout(task->tk_client);
6181 giveup = lgp->args.timestamp + timeo;
6182 if (time_after(giveup, jiffies))
6183 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006184 break;
6185 case -NFS4ERR_EXPIRED:
6186 case -NFS4ERR_BAD_STATEID:
6187 spin_lock(&inode->i_lock);
6188 lo = NFS_I(inode)->layout;
6189 if (!lo || list_empty(&lo->plh_segs)) {
6190 spin_unlock(&inode->i_lock);
6191 /* If the open stateid was bad, then recover it. */
6192 state = lgp->args.ctx->state;
6193 } else {
6194 LIST_HEAD(head);
6195
6196 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6197 spin_unlock(&inode->i_lock);
6198 /* Mark the bad layout state as invalid, then
6199 * retry using the open stateid. */
6200 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006201 }
6202 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006203 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6204 rpc_restart_call_prepare(task);
6205out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006206 dprintk("<-- %s\n", __func__);
6207}
6208
Idan Kedar85541162012-08-02 11:47:10 +03006209static size_t max_response_pages(struct nfs_server *server)
6210{
6211 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6212 return nfs_page_array_len(0, max_resp_sz);
6213}
6214
6215static void nfs4_free_pages(struct page **pages, size_t size)
6216{
6217 int i;
6218
6219 if (!pages)
6220 return;
6221
6222 for (i = 0; i < size; i++) {
6223 if (!pages[i])
6224 break;
6225 __free_page(pages[i]);
6226 }
6227 kfree(pages);
6228}
6229
6230static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6231{
6232 struct page **pages;
6233 int i;
6234
6235 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6236 if (!pages) {
6237 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6238 return NULL;
6239 }
6240
6241 for (i = 0; i < size; i++) {
6242 pages[i] = alloc_page(gfp_flags);
6243 if (!pages[i]) {
6244 dprintk("%s: failed to allocate page\n", __func__);
6245 nfs4_free_pages(pages, size);
6246 return NULL;
6247 }
6248 }
6249
6250 return pages;
6251}
6252
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253static void nfs4_layoutget_release(void *calldata)
6254{
6255 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006256 struct inode *inode = lgp->args.inode;
6257 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006258 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006259
6260 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006261 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006262 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006263 put_nfs_open_context(lgp->args.ctx);
6264 kfree(calldata);
6265 dprintk("<-- %s\n", __func__);
6266}
6267
6268static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6269 .rpc_call_prepare = nfs4_layoutget_prepare,
6270 .rpc_call_done = nfs4_layoutget_done,
6271 .rpc_release = nfs4_layoutget_release,
6272};
6273
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006274struct pnfs_layout_segment *
6275nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006277 struct inode *inode = lgp->args.inode;
6278 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006279 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006280 struct rpc_task *task;
6281 struct rpc_message msg = {
6282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6283 .rpc_argp = &lgp->args,
6284 .rpc_resp = &lgp->res,
6285 };
6286 struct rpc_task_setup task_setup_data = {
6287 .rpc_client = server->client,
6288 .rpc_message = &msg,
6289 .callback_ops = &nfs4_layoutget_call_ops,
6290 .callback_data = lgp,
6291 .flags = RPC_TASK_ASYNC,
6292 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006293 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294 int status = 0;
6295
6296 dprintk("--> %s\n", __func__);
6297
Idan Kedar85541162012-08-02 11:47:10 +03006298 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6299 if (!lgp->args.layout.pages) {
6300 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006301 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006302 }
6303 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006304 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006305
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006306 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006307 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006308 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006309
6310 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6311 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6312
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006313 task = rpc_run_task(&task_setup_data);
6314 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006315 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006316 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006317 if (status == 0)
6318 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006319 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6320 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006321 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006322 rpc_put_task(task);
6323 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006324 if (status)
6325 return ERR_PTR(status);
6326 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006327}
6328
Benny Halevycbe82602011-05-22 19:52:37 +03006329static void
6330nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6331{
6332 struct nfs4_layoutreturn *lrp = calldata;
6333
6334 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006335 nfs41_setup_sequence(lrp->clp->cl_session,
6336 &lrp->args.seq_args,
6337 &lrp->res.seq_res,
6338 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006339}
6340
6341static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6342{
6343 struct nfs4_layoutreturn *lrp = calldata;
6344 struct nfs_server *server;
6345
6346 dprintk("--> %s\n", __func__);
6347
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006348 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006349 return;
6350
6351 server = NFS_SERVER(lrp->args.inode);
6352 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006353 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006354 return;
6355 }
Benny Halevycbe82602011-05-22 19:52:37 +03006356 dprintk("<-- %s\n", __func__);
6357}
6358
6359static void nfs4_layoutreturn_release(void *calldata)
6360{
6361 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006362 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006363
6364 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006365 spin_lock(&lo->plh_inode->i_lock);
6366 if (lrp->res.lrs_present)
6367 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6368 lo->plh_block_lgets--;
6369 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006370 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006371 kfree(calldata);
6372 dprintk("<-- %s\n", __func__);
6373}
6374
6375static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6376 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6377 .rpc_call_done = nfs4_layoutreturn_done,
6378 .rpc_release = nfs4_layoutreturn_release,
6379};
6380
6381int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6382{
6383 struct rpc_task *task;
6384 struct rpc_message msg = {
6385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6386 .rpc_argp = &lrp->args,
6387 .rpc_resp = &lrp->res,
6388 };
6389 struct rpc_task_setup task_setup_data = {
6390 .rpc_client = lrp->clp->cl_rpcclient,
6391 .rpc_message = &msg,
6392 .callback_ops = &nfs4_layoutreturn_call_ops,
6393 .callback_data = lrp,
6394 };
6395 int status;
6396
6397 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006398 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006399 task = rpc_run_task(&task_setup_data);
6400 if (IS_ERR(task))
6401 return PTR_ERR(task);
6402 status = task->tk_status;
6403 dprintk("<-- %s status=%d\n", __func__, status);
6404 rpc_put_task(task);
6405 return status;
6406}
6407
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006408/*
6409 * Retrieve the list of Data Server devices from the MDS.
6410 */
6411static int _nfs4_getdevicelist(struct nfs_server *server,
6412 const struct nfs_fh *fh,
6413 struct pnfs_devicelist *devlist)
6414{
6415 struct nfs4_getdevicelist_args args = {
6416 .fh = fh,
6417 .layoutclass = server->pnfs_curr_ld->id,
6418 };
6419 struct nfs4_getdevicelist_res res = {
6420 .devlist = devlist,
6421 };
6422 struct rpc_message msg = {
6423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6424 .rpc_argp = &args,
6425 .rpc_resp = &res,
6426 };
6427 int status;
6428
6429 dprintk("--> %s\n", __func__);
6430 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6431 &res.seq_res, 0);
6432 dprintk("<-- %s status=%d\n", __func__, status);
6433 return status;
6434}
6435
6436int nfs4_proc_getdevicelist(struct nfs_server *server,
6437 const struct nfs_fh *fh,
6438 struct pnfs_devicelist *devlist)
6439{
6440 struct nfs4_exception exception = { };
6441 int err;
6442
6443 do {
6444 err = nfs4_handle_exception(server,
6445 _nfs4_getdevicelist(server, fh, devlist),
6446 &exception);
6447 } while (exception.retry);
6448
6449 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6450 err, devlist->num_devs);
6451
6452 return err;
6453}
6454EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6455
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006456static int
6457_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6458{
6459 struct nfs4_getdeviceinfo_args args = {
6460 .pdev = pdev,
6461 };
6462 struct nfs4_getdeviceinfo_res res = {
6463 .pdev = pdev,
6464 };
6465 struct rpc_message msg = {
6466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6467 .rpc_argp = &args,
6468 .rpc_resp = &res,
6469 };
6470 int status;
6471
6472 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006473 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006474 dprintk("<-- %s status=%d\n", __func__, status);
6475
6476 return status;
6477}
6478
6479int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6480{
6481 struct nfs4_exception exception = { };
6482 int err;
6483
6484 do {
6485 err = nfs4_handle_exception(server,
6486 _nfs4_proc_getdeviceinfo(server, pdev),
6487 &exception);
6488 } while (exception.retry);
6489 return err;
6490}
6491EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6492
Andy Adamson863a3c62011-03-23 13:27:54 +00006493static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6494{
6495 struct nfs4_layoutcommit_data *data = calldata;
6496 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006497 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006498
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006499 nfs41_setup_sequence(session,
6500 &data->args.seq_args,
6501 &data->res.seq_res,
6502 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006503}
6504
6505static void
6506nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6507{
6508 struct nfs4_layoutcommit_data *data = calldata;
6509 struct nfs_server *server = NFS_SERVER(data->args.inode);
6510
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006511 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006512 return;
6513
6514 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006515 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6516 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6517 case -NFS4ERR_BADLAYOUT: /* no layout */
6518 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006519 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006520 break;
6521 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006522 nfs_post_op_update_inode_force_wcc(data->args.inode,
6523 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006524 break;
6525 default:
6526 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6527 rpc_restart_call_prepare(task);
6528 return;
6529 }
6530 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006531}
6532
6533static void nfs4_layoutcommit_release(void *calldata)
6534{
6535 struct nfs4_layoutcommit_data *data = calldata;
6536
Andy Adamsondb29c082011-07-30 20:52:38 -04006537 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006538 put_rpccred(data->cred);
6539 kfree(data);
6540}
6541
6542static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6543 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6544 .rpc_call_done = nfs4_layoutcommit_done,
6545 .rpc_release = nfs4_layoutcommit_release,
6546};
6547
6548int
Andy Adamsonef311532011-03-12 02:58:10 -05006549nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006550{
6551 struct rpc_message msg = {
6552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6553 .rpc_argp = &data->args,
6554 .rpc_resp = &data->res,
6555 .rpc_cred = data->cred,
6556 };
6557 struct rpc_task_setup task_setup_data = {
6558 .task = &data->task,
6559 .rpc_client = NFS_CLIENT(data->args.inode),
6560 .rpc_message = &msg,
6561 .callback_ops = &nfs4_layoutcommit_ops,
6562 .callback_data = data,
6563 .flags = RPC_TASK_ASYNC,
6564 };
6565 struct rpc_task *task;
6566 int status = 0;
6567
6568 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6569 "lbw: %llu inode %lu\n",
6570 data->task.tk_pid, sync,
6571 data->args.lastbytewritten,
6572 data->args.inode->i_ino);
6573
Trond Myklebust9d12b212012-01-17 22:04:25 -05006574 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006575 task = rpc_run_task(&task_setup_data);
6576 if (IS_ERR(task))
6577 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006578 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006579 goto out;
6580 status = nfs4_wait_for_completion_rpc_task(task);
6581 if (status != 0)
6582 goto out;
6583 status = task->tk_status;
6584out:
6585 dprintk("%s: status %d\n", __func__, status);
6586 rpc_put_task(task);
6587 return status;
6588}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006589
6590static int
6591_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6592 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6593{
6594 struct nfs41_secinfo_no_name_args args = {
6595 .style = SECINFO_STYLE_CURRENT_FH,
6596 };
6597 struct nfs4_secinfo_res res = {
6598 .flavors = flavors,
6599 };
6600 struct rpc_message msg = {
6601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6602 .rpc_argp = &args,
6603 .rpc_resp = &res,
6604 };
6605 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6606}
6607
6608static int
6609nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6610 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6611{
6612 struct nfs4_exception exception = { };
6613 int err;
6614 do {
6615 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6616 switch (err) {
6617 case 0:
6618 case -NFS4ERR_WRONGSEC:
6619 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006620 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006621 default:
6622 err = nfs4_handle_exception(server, err, &exception);
6623 }
6624 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006625out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006626 return err;
6627}
6628
6629static int
6630nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6631 struct nfs_fsinfo *info)
6632{
6633 int err;
6634 struct page *page;
6635 rpc_authflavor_t flavor;
6636 struct nfs4_secinfo_flavors *flavors;
6637
6638 page = alloc_page(GFP_KERNEL);
6639 if (!page) {
6640 err = -ENOMEM;
6641 goto out;
6642 }
6643
6644 flavors = page_address(page);
6645 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6646
6647 /*
6648 * Fall back on "guess and check" method if
6649 * the server doesn't support SECINFO_NO_NAME
6650 */
6651 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6652 err = nfs4_find_root_sec(server, fhandle, info);
6653 goto out_freepage;
6654 }
6655 if (err)
6656 goto out_freepage;
6657
6658 flavor = nfs_find_best_sec(flavors);
6659 if (err == 0)
6660 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6661
6662out_freepage:
6663 put_page(page);
6664 if (err == -EACCES)
6665 return -EPERM;
6666out:
6667 return err;
6668}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006669
6670static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006671{
6672 int status;
6673 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006674 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006675 };
6676 struct nfs41_test_stateid_res res;
6677 struct rpc_message msg = {
6678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6679 .rpc_argp = &args,
6680 .rpc_resp = &res,
6681 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006682
Chuck Lever38527b12012-07-11 16:30:23 -04006683 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006684 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006685 nfs4_set_sequence_privileged(&args.seq_args);
6686 status = nfs4_call_sync_sequence(server->client, server, &msg,
6687 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006688 if (status != NFS_OK) {
6689 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006690 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006691 }
6692 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006693 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006694}
6695
Chuck Lever38527b12012-07-11 16:30:23 -04006696/**
6697 * nfs41_test_stateid - perform a TEST_STATEID operation
6698 *
6699 * @server: server / transport on which to perform the operation
6700 * @stateid: state ID to test
6701 *
6702 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6703 * Otherwise a negative NFS4ERR value is returned if the operation
6704 * failed or the state ID is not currently valid.
6705 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006706static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006707{
6708 struct nfs4_exception exception = { };
6709 int err;
6710 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006711 err = _nfs41_test_stateid(server, stateid);
6712 if (err != -NFS4ERR_DELAY)
6713 break;
6714 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006715 } while (exception.retry);
6716 return err;
6717}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006718
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006719static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006720{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006721 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006722 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006723 };
6724 struct nfs41_free_stateid_res res;
6725 struct rpc_message msg = {
6726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6727 .rpc_argp = &args,
6728 .rpc_resp = &res,
6729 };
Chuck Lever38527b12012-07-11 16:30:23 -04006730 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006731
Chuck Lever38527b12012-07-11 16:30:23 -04006732 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006733 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006734 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006735 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006736 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006737 dprintk("NFS reply free_stateid: %d\n", status);
6738 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006739}
6740
Chuck Lever38527b12012-07-11 16:30:23 -04006741/**
6742 * nfs41_free_stateid - perform a FREE_STATEID operation
6743 *
6744 * @server: server / transport on which to perform the operation
6745 * @stateid: state ID to release
6746 *
6747 * Returns NFS_OK if the server freed "stateid". Otherwise a
6748 * negative NFS4ERR value is returned.
6749 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006750static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006751{
6752 struct nfs4_exception exception = { };
6753 int err;
6754 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006755 err = _nfs4_free_stateid(server, stateid);
6756 if (err != -NFS4ERR_DELAY)
6757 break;
6758 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006759 } while (exception.retry);
6760 return err;
6761}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006762
6763static bool nfs41_match_stateid(const nfs4_stateid *s1,
6764 const nfs4_stateid *s2)
6765{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006766 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006767 return false;
6768
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006769 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006770 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006771 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006772 return true;
6773
6774 return false;
6775}
6776
Andy Adamson557134a2009-04-01 09:21:53 -04006777#endif /* CONFIG_NFS_V4_1 */
6778
Trond Myklebust36281ca2012-03-04 18:13:56 -05006779static bool nfs4_match_stateid(const nfs4_stateid *s1,
6780 const nfs4_stateid *s2)
6781{
Trond Myklebustf597c532012-03-04 18:13:56 -05006782 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006783}
6784
6785
Trond Myklebust17280172012-03-11 13:11:00 -04006786static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006787 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006788 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 .recover_open = nfs4_open_reclaim,
6790 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006791 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006792 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006793 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794};
6795
Andy Adamson591d71c2009-04-01 09:22:47 -04006796#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006797static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006798 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6799 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6800 .recover_open = nfs4_open_reclaim,
6801 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006802 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006803 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006804 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006805 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006806};
6807#endif /* CONFIG_NFS_V4_1 */
6808
Trond Myklebust17280172012-03-11 13:11:00 -04006809static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006810 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006811 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 .recover_open = nfs4_open_expired,
6813 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006814 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006815 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816};
6817
Andy Adamson591d71c2009-04-01 09:22:47 -04006818#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006819static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006820 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6821 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006822 .recover_open = nfs41_open_expired,
6823 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006824 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006825 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006826};
6827#endif /* CONFIG_NFS_V4_1 */
6828
Trond Myklebust17280172012-03-11 13:11:00 -04006829static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006830 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006831 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006832 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006833};
6834
6835#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006836static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006837 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006838 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006839 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006840};
6841#endif
6842
Trond Myklebust97dc1352010-06-16 09:52:26 -04006843static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6844 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006845 .init_caps = NFS_CAP_READDIRPLUS
6846 | NFS_CAP_ATOMIC_OPEN
6847 | NFS_CAP_CHANGE_ATTR
6848 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006849 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006850 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006851 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006852 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6853 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6854 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006855};
6856
6857#if defined(CONFIG_NFS_V4_1)
6858static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6859 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006860 .init_caps = NFS_CAP_READDIRPLUS
6861 | NFS_CAP_ATOMIC_OPEN
6862 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006863 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006864 | NFS_CAP_STATEID_NFSV41
6865 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006866 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006867 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006868 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006869 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6870 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6871 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006872};
6873#endif
6874
Trond Myklebust97dc1352010-06-16 09:52:26 -04006875const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6876 [0] = &nfs_v4_0_minor_ops,
6877#if defined(CONFIG_NFS_V4_1)
6878 [1] = &nfs_v4_1_minor_ops,
6879#endif
6880};
6881
Bryan Schumaker73a79702012-07-16 16:39:12 -04006882const struct inode_operations nfs4_dir_inode_operations = {
6883 .create = nfs_create,
6884 .lookup = nfs_lookup,
6885 .atomic_open = nfs_atomic_open,
6886 .link = nfs_link,
6887 .unlink = nfs_unlink,
6888 .symlink = nfs_symlink,
6889 .mkdir = nfs_mkdir,
6890 .rmdir = nfs_rmdir,
6891 .mknod = nfs_mknod,
6892 .rename = nfs_rename,
6893 .permission = nfs_permission,
6894 .getattr = nfs_getattr,
6895 .setattr = nfs_setattr,
6896 .getxattr = generic_getxattr,
6897 .setxattr = generic_setxattr,
6898 .listxattr = generic_listxattr,
6899 .removexattr = generic_removexattr,
6900};
6901
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006902static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006903 .permission = nfs_permission,
6904 .getattr = nfs_getattr,
6905 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006906 .getxattr = generic_getxattr,
6907 .setxattr = generic_setxattr,
6908 .listxattr = generic_listxattr,
6909 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006910};
6911
David Howells509de812006-08-22 20:06:11 -04006912const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 .version = 4, /* protocol version */
6914 .dentry_ops = &nfs4_dentry_operations,
6915 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006916 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006917 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006919 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006920 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 .getattr = nfs4_proc_getattr,
6922 .setattr = nfs4_proc_setattr,
6923 .lookup = nfs4_proc_lookup,
6924 .access = nfs4_proc_access,
6925 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 .create = nfs4_proc_create,
6927 .remove = nfs4_proc_remove,
6928 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006929 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 .unlink_done = nfs4_proc_unlink_done,
6931 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006932 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006933 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006934 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 .link = nfs4_proc_link,
6936 .symlink = nfs4_proc_symlink,
6937 .mkdir = nfs4_proc_mkdir,
6938 .rmdir = nfs4_proc_remove,
6939 .readdir = nfs4_proc_readdir,
6940 .mknod = nfs4_proc_mknod,
6941 .statfs = nfs4_proc_statfs,
6942 .fsinfo = nfs4_proc_fsinfo,
6943 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006944 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 .decode_dirent = nfs4_decode_dirent,
6946 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006947 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006948 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006949 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006951 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006952 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006953 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006955 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006956 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006958 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006959 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006960 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006961 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006962 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006963 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006964 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006965 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006966 .create_server = nfs4_create_server,
6967 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968};
6969
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006970static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6971 .prefix = XATTR_NAME_NFSV4_ACL,
6972 .list = nfs4_xattr_list_nfs4_acl,
6973 .get = nfs4_xattr_get_nfs4_acl,
6974 .set = nfs4_xattr_set_nfs4_acl,
6975};
6976
6977const struct xattr_handler *nfs4_xattr_handlers[] = {
6978 &nfs4_xattr_nfs4_acl_handler,
6979 NULL
6980};
6981
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982/*
6983 * Local variables:
6984 * c-basic-offset: 8
6985 * End:
6986 */