blob: 83197fb60d5f79ad5e93532e7105ff37660870f4 [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 Myklebust9e33bed2008-12-23 15:21:46 -0500307 if (state == NULL)
308 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400309 nfs_remove_bad_delegation(state->inode);
Trond Myklebust5d422302013-03-14 16:57:48 -0400310 ret = nfs4_schedule_stateid_recovery(server, state);
311 if (ret < 0)
312 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500313 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400314 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400315 if (state != NULL) {
316 ret = nfs4_schedule_stateid_recovery(server, state);
317 if (ret < 0)
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500321 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322 nfs4_schedule_lease_recovery(clp);
323 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500324#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400325 case -NFS4ERR_BADSESSION:
326 case -NFS4ERR_BADSLOT:
327 case -NFS4ERR_BAD_HIGH_SLOT:
328 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
329 case -NFS4ERR_DEADSESSION:
330 case -NFS4ERR_SEQ_FALSE_RETRY:
331 case -NFS4ERR_SEQ_MISORDERED:
332 dprintk("%s ERROR: %d Reset session\n", __func__,
333 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400334 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400335 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
347 ret = nfs4_delay(server->client, &exception->timeout);
348 if (ret != 0)
349 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400350 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_OLD_STATEID:
352 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800353 break;
354 case -NFS4ERR_BADOWNER:
355 /* The following works around a Linux server bug! */
356 case -NFS4ERR_BADNAME:
357 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
358 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
359 exception->retry = 1;
360 printk(KERN_WARNING "NFS: v4 server %s "
361 "does not accept raw "
362 "uid/gids. "
363 "Reenabling the idmapper.\n",
364 server->nfs_client->cl_hostname);
365 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500366 }
367 /* We failed to handle the error */
368 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500369wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500370 ret = nfs4_wait_clnt_recover(clp);
371 if (ret == 0)
372 exception->retry = 1;
373 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500374}
375
376
Trond Myklebust452e9352010-07-31 14:29:06 -0400377static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock(&clp->cl_lock);
380 if (time_before(clp->cl_last_renewal,timestamp))
381 clp->cl_last_renewal = timestamp;
382 spin_unlock(&clp->cl_lock);
383}
384
Trond Myklebust452e9352010-07-31 14:29:06 -0400385static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
386{
387 do_renew_lease(server->nfs_client, timestamp);
388}
389
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400390#if defined(CONFIG_NFS_V4_1)
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400393{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400395 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500396 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400397
Benny Halevydfb4f3092010-09-24 09:17:01 -0400398 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500401 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 return;
Andy Adamson13615872009-04-01 09:22:17 -0400403 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500404 tbl = res->sr_slot->table;
405 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500406
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408 /* Be nice to the server: try to ensure that the last transmitted
409 * value for highest_user_slotid <= target_highest_slotid
410 */
411 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
412 send_new_highest_used_slotid = true;
413
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500414 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
415 send_new_highest_used_slotid = false;
416 goto out_unlock;
417 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500418 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500419
420 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
421 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500422out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500423 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400424 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500425 if (send_new_highest_used_slotid)
426 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400427}
428
Trond Myklebust14516c32010-07-31 14:29:06 -0400429static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400433 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500434 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500435 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436
Bryan Schumaker468f8612011-04-18 15:57:32 -0400437 /* don't increment the sequence number if the task wasn't sent */
438 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439 goto out;
440
Trond Myklebust933602e2012-11-16 12:12:38 -0500441 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500442 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500443
Trond Myklebustac20d162012-12-15 15:36:07 -0500444 if (slot->interrupted) {
445 slot->interrupted = 0;
446 interrupted = true;
447 }
448
Andy Adamson691daf32009-12-04 15:55:39 -0500449 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 switch (res->sr_status) {
451 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500453 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500455 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500461 case 1:
462 /*
463 * sr_status remains 1 if an RPC level error occurred.
464 * The server may or may not have processed the sequence
465 * operation..
466 * Mark the slot as having hosted an interrupted RPC call.
467 */
468 slot->interrupted = 1;
469 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500475 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500477 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500478 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500480 case -NFS4ERR_BADSLOT:
481 /*
482 * The slot id we used was probably retired. Try again
483 * using a different slot id.
484 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 goto retry_nowait;
486 case -NFS4ERR_SEQ_MISORDERED:
487 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500488 * Was the last operation on this sequence interrupted?
489 * If so, retry after bumping the sequence number.
490 */
491 if (interrupted) {
492 ++slot->seq_nr;
493 goto retry_nowait;
494 }
495 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500496 * Could this slot have been previously retired?
497 * If so, then the server may be expecting seq_nr = 1!
498 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500499 if (slot->seq_nr != 1) {
500 slot->seq_nr = 1;
501 goto retry_nowait;
502 }
503 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500504 case -NFS4ERR_SEQ_FALSE_RETRY:
505 ++slot->seq_nr;
506 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400507 default:
508 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500509 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 }
511out:
512 /* The session may be reset by one of the error handlers. */
513 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400514 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500515 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500516retry_nowait:
517 if (rpc_restart_call_prepare(task)) {
518 task->tk_status = 0;
519 ret = 0;
520 }
521 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400522out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400523 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400524 goto out;
525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
526 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527}
528
Trond Myklebust14516c32010-07-31 14:29:06 -0400529static int nfs4_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400531{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500532 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 return 1;
534 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400535}
536
Trond Myklebust9d12b212012-01-17 22:04:25 -0500537static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
539{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400542 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500543 if (cache_reply)
544 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 res->sr_slot = NULL;
546}
547
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400548static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
549{
550 args->sa_privileged = 1;
551}
552
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000553int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556 struct rpc_task *task)
557{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 struct nfs4_slot *slot;
559 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560
561 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400563 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400564 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 tbl = &session->fc_slot_table;
567
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 task->tk_timeout = 0;
569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400571 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400572 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500573 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500574 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400575 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400576 }
577
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500578 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500579 if (IS_ERR(slot)) {
580 /* If out of memory, try again in 1/4 second */
581 if (slot == ERR_PTR(-ENOMEM))
582 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400584 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 }
586 spin_unlock(&tbl->slot_tbl_lock);
587
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500588 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
591 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500594 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400595 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 /*
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
599 */
600 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400601out_success:
602 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400604out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 /* Privileged tasks are queued with top priority */
606 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400607 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
608 NULL, RPC_PRIORITY_PRIVILEGED);
609 else
610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400611 spin_unlock(&tbl->slot_tbl_lock);
612 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000614EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615
Trond Myklebust035168a2010-06-16 09:52:26 -0400616int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Trond Myklebust035168a2010-06-16 09:52:26 -0400621 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 int ret = 0;
623
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400624 if (session == NULL) {
625 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400628
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500629 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400630 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500631 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebust9d12b212012-01-17 22:04:25 -0500633 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634out:
635 dprintk("<-- %s status=%d\n", __func__, ret);
636 return ret;
637}
638
639struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400640 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643};
644
645static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400648 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
651
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400652 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653}
654
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660}
661
Trond Myklebust17280172012-03-11 13:11:00 -0400662static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665};
666
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400671 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672{
673 int ret;
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400676 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .seq_args = args,
678 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 };
680 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000681 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
685 };
686
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 task = rpc_run_task(&task_setup);
688 if (IS_ERR(task))
689 ret = PTR_ERR(task);
690 else {
691 ret = task->tk_status;
692 rpc_put_task(task);
693 }
694 return ret;
695}
696
Trond Myklebustdf896452010-06-16 09:52:26 -0400697#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400698static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500699void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
701{
702}
703
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400704static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
705{
706}
707
708
Trond Myklebust14516c32010-07-31 14:29:06 -0400709static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400711{
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714#endif /* CONFIG_NFS_V4_1 */
715
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400716static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724}
725
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400734 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400736 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400738
Trond Myklebust38478b22006-05-25 01:40:57 -0400739static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700745 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400746 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700747 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000748 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400753 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400762 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100766 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400767 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100768 int rpc_status;
769 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770};
771
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400772static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
774{
775 if (err != -EINVAL)
776 return false;
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
778 return false;
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
781 return true;
782}
783
784static enum open_claim_type4
785nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
787{
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
789 return claim;
790 switch (claim) {
791 default:
792 return claim;
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
799 }
800}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801
802static void nfs4_init_opendata_res(struct nfs4_opendata *p)
803{
804 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400807 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400808 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400809 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811}
812
Al Viro82a2c1b2011-06-22 18:30:55 -0400813static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400815 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400816 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818{
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
823
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (p == NULL)
826 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p->o_arg.seqid == NULL)
829 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
841 * are cached */
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700844 }
David Howells7539bba2006-08-22 20:06:09 -0400845 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
858 break;
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
864 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500865 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500866 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400867
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500870
871 verf[0] = jiffies;
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400879 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 return p;
882err_free:
883 kfree(p);
884err:
885 dput(parent);
886 return NULL;
887}
888
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400889static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400893 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894
895 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 nfs4_put_state_owner(p->owner);
899 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400900 dput(p->dentry);
901 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500902 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kfree(p);
904}
905
906static void nfs4_opendata_put(struct nfs4_opendata *p)
907{
908 if (p != NULL)
909 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910}
911
Trond Myklebust06f814a2006-01-03 09:55:07 +0100912static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
913{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100914 int ret;
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917 return ret;
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921{
922 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923
Trond Myklebust536e43d2012-01-17 22:04:26 -0500924 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 goto out;
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400930 break;
931 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500945 if (delegation == NULL)
946 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
952 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500953 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 1;
955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100958{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
968 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
986 }
987}
988
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400990{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0700997{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 /*
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1001 */
1002 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1006 }
1007 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001012 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015static 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 -05001016{
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001022
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1027
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001032 goto no_delegation_unlock;
1033
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
Trond Myklebustb7391f42008-12-23 15:21:52 -05001039 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001041 ret = 1;
1042no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044no_delegation:
1045 rcu_read_unlock();
1046
1047 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050 }
1051
1052 return ret;
1053}
1054
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057{
1058 struct nfs_delegation *delegation;
1059
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 rcu_read_unlock();
1064 return;
1065 }
1066 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001067 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068}
1069
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1079
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 goto out_return_state;
1087 }
1088 spin_unlock(&state->owner->so_lock);
1089 }
Trond Myklebust34310432008-12-23 15:21:38 -05001090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001092 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001097 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001106 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
1109 return ERR_PTR(ret);
1110out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1113}
1114
Andy Adamsone23008e2012-10-02 21:07:32 -04001115static void
1116nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1117{
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1121
1122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1131 clp->cl_hostname);
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140}
1141
1142/*
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1145 */
1146static struct nfs4_state *
1147_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1148{
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1151 int ret;
1152
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1155 goto err;
1156 }
1157
1158 ret = -ESTALE;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1162 goto err;
1163
1164 ret = -ENOMEM;
1165 state = nfs4_get_open_state(inode, data->owner);
1166 if (state == NULL)
1167 goto err;
1168
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1170 if (ret)
1171 goto err;
1172
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1176 data->o_arg.fmode);
1177
1178 return state;
1179err:
1180 return ERR_PTR(ret);
1181
1182}
1183
1184static struct nfs4_state *
1185_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001186{
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001189 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001192 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 goto out;
1194 }
1195
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001201 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
1203 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 state = nfs4_get_open_state(inode, data->owner);
1205 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001209 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001213 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215err_put_inode:
1216 iput(inode);
1217err:
1218 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219}
1220
Andy Adamsone23008e2012-10-02 21:07:32 -04001221static struct nfs4_state *
1222nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1223{
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1227}
1228
Trond Myklebust864472e2006-01-03 09:55:15 +01001229static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1230{
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1233
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1237 continue;
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1240 return ctx;
1241 }
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1244}
1245
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001246static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001248{
1249 struct nfs4_opendata *opendata;
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1257 return opendata;
1258}
1259
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001261{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 int ret;
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001270 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 if (ret != 0)
1272 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001273 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001276 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 return 0;
1279}
1280
1281static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1282{
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 int ret;
1285
1286 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 smp_rmb();
1289 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 if (ret != 0)
1293 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 if (newstate != state)
1295 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 }
1297 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 if (ret != 0)
1301 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001302 if (newstate != state)
1303 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 }
1305 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 if (ret != 0)
1309 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 if (newstate != state)
1311 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001313 /*
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1316 */
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001319 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001322 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001323 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * OPEN_RECLAIM:
1329 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001331static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001333 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int status;
1337
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001345 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001349 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return status;
1351}
1352
Trond Myklebust539cd032007-06-05 11:46:42 -04001353static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1357 int err;
1358 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001359 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1361 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001362 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001363 break;
1364 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } while (exception.retry);
1366 return err;
1367}
1368
Trond Myklebust864472e2006-01-03 09:55:15 +01001369static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1370{
1371 struct nfs_open_context *ctx;
1372 int ret;
1373
1374 ctx = nfs4_state_find_open_context(state);
1375 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001376 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 put_nfs_open_context(ctx);
1379 return ret;
1380}
1381
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001382static 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 -07001383{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001384 switch (err) {
1385 default:
1386 printk(KERN_ERR "NFS: %s: unhandled error "
1387 "%d.\n", __func__, err);
1388 case 0:
1389 case -ENOENT:
1390 case -ESTALE:
1391 break;
1392 case -NFS4ERR_BADSESSION:
1393 case -NFS4ERR_BADSLOT:
1394 case -NFS4ERR_BAD_HIGH_SLOT:
1395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1396 case -NFS4ERR_DEADSESSION:
1397 set_bit(NFS_DELEGATED_STATE, &state->flags);
1398 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1399 return -EAGAIN;
1400 case -NFS4ERR_STALE_CLIENTID:
1401 case -NFS4ERR_STALE_STATEID:
1402 set_bit(NFS_DELEGATED_STATE, &state->flags);
1403 case -NFS4ERR_EXPIRED:
1404 /* Don't recall a delegation if it was lost */
1405 nfs4_schedule_lease_recovery(server->nfs_client);
1406 return -EAGAIN;
1407 case -NFS4ERR_DELEG_REVOKED:
1408 case -NFS4ERR_ADMIN_REVOKED:
1409 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001410 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001411 nfs_inode_find_state_and_recover(state->inode,
1412 stateid);
1413 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001414 return 0;
1415 case -NFS4ERR_DELAY:
1416 case -NFS4ERR_GRACE:
1417 set_bit(NFS_DELEGATED_STATE, &state->flags);
1418 ssleep(1);
1419 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001420 case -ENOMEM:
1421 case -NFS4ERR_DENIED:
1422 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1423 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return err;
1426}
1427
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001428int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1429{
1430 struct nfs_server *server = NFS_SERVER(state->inode);
1431 struct nfs4_opendata *opendata;
1432 int err;
1433
1434 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1435 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1436 if (IS_ERR(opendata))
1437 return PTR_ERR(opendata);
1438 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1439 err = nfs4_open_recover(opendata, state);
1440 nfs4_opendata_put(opendata);
1441 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1442}
1443
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001444static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1445{
1446 struct nfs4_opendata *data = calldata;
1447
1448 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001449 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001450 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001451 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001452 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001453 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001455}
1456
1457static void nfs4_open_confirm_release(void *calldata)
1458{
1459 struct nfs4_opendata *data = calldata;
1460 struct nfs4_state *state = NULL;
1461
1462 /* If this request hasn't been cancelled, do nothing */
1463 if (data->cancelled == 0)
1464 goto out_free;
1465 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001466 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001468 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001469 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001470 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001471out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001472 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473}
1474
1475static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476 .rpc_call_done = nfs4_open_confirm_done,
1477 .rpc_release = nfs4_open_confirm_release,
1478};
1479
1480/*
1481 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1482 */
1483static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1484{
1485 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1489 .rpc_argp = &data->c_arg,
1490 .rpc_resp = &data->c_res,
1491 .rpc_cred = data->owner->so_cred,
1492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001493 struct rpc_task_setup task_setup_data = {
1494 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001496 .callback_ops = &nfs4_open_confirm_ops,
1497 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001498 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 .flags = RPC_TASK_ASYNC,
1500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int status;
1502
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001503 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001504 data->rpc_done = 0;
1505 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001506 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001507 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001508 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001509 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001510 status = nfs4_wait_for_completion_rpc_task(task);
1511 if (status != 0) {
1512 data->cancelled = 1;
1513 smp_wmb();
1514 } else
1515 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001516 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return status;
1518}
1519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 struct nfs4_opendata *data = calldata;
1523 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001526 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527 /*
1528 * Check if we still need to send an OPEN call, or if we can use
1529 * a delegation instead.
1530 */
1531 if (data->state != NULL) {
1532 struct nfs_delegation *delegation;
1533
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001534 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001535 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001536 rcu_read_lock();
1537 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001538 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1539 can_open_delegated(delegation, data->o_arg.fmode))
1540 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001541 rcu_read_unlock();
1542 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001543 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001544 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001545 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001547 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001548 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1549 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001550 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001551 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001552 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001553 &data->o_res.seq_res,
1554 task) != 0)
1555 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001556 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001557unlock_no_action:
1558 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001559out_no_action:
1560 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001561out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001562 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001563}
1564
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565static void nfs4_open_done(struct rpc_task *task, void *calldata)
1566{
1567 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001570
Trond Myklebust14516c32010-07-31 14:29:06 -04001571 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1572 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001573
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001575 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1576 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001577 case S_IFREG:
1578 break;
1579 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001581 break;
1582 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001584 break;
1585 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001587 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001588 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001589 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001590 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1591 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001592 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001593 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596static void nfs4_open_release(void *calldata)
1597{
1598 struct nfs4_opendata *data = calldata;
1599 struct nfs4_state *state = NULL;
1600
1601 /* If this request hasn't been cancelled, do nothing */
1602 if (data->cancelled == 0)
1603 goto out_free;
1604 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001605 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 goto out_free;
1607 /* In case we need an open_confirm, no cleanup! */
1608 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1609 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001611 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001612 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001614 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615}
1616
1617static const struct rpc_call_ops nfs4_open_ops = {
1618 .rpc_call_prepare = nfs4_open_prepare,
1619 .rpc_call_done = nfs4_open_done,
1620 .rpc_release = nfs4_open_release,
1621};
1622
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624{
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_server *server = NFS_SERVER(dir);
1627 struct nfs_openargs *o_arg = &data->o_arg;
1628 struct nfs_openres *o_res = &data->o_res;
1629 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001630 struct rpc_message msg = {
1631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1632 .rpc_argp = o_arg,
1633 .rpc_resp = o_res,
1634 .rpc_cred = data->owner->so_cred,
1635 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001636 struct rpc_task_setup task_setup_data = {
1637 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001638 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001639 .callback_ops = &nfs4_open_ops,
1640 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001641 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001642 .flags = RPC_TASK_ASYNC,
1643 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001644 int status;
1645
Trond Myklebust9d12b212012-01-17 22:04:25 -05001646 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001647 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001648 data->rpc_done = 0;
1649 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001650 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001651 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001652 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001653 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001654 if (IS_ERR(task))
1655 return PTR_ERR(task);
1656 status = nfs4_wait_for_completion_rpc_task(task);
1657 if (status != 0) {
1658 data->cancelled = 1;
1659 smp_wmb();
1660 } else
1661 status = data->rpc_status;
1662 rpc_put_task(task);
1663
1664 return status;
1665}
1666
1667static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1668{
1669 struct inode *dir = data->dir->d_inode;
1670 struct nfs_openres *o_res = &data->o_res;
1671 int status;
1672
1673 status = nfs4_run_open_task(data, 1);
1674 if (status != 0 || !data->rpc_done)
1675 return status;
1676
Trond Myklebust6926afd2012-01-07 13:22:46 -05001677 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1678
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001679 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1680 status = _nfs4_proc_open_confirm(data);
1681 if (status != 0)
1682 return status;
1683 }
1684
1685 return status;
1686}
1687
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001688static int nfs4_opendata_access(struct rpc_cred *cred,
1689 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001690 struct nfs4_state *state, fmode_t fmode,
1691 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001692{
1693 struct nfs_access_entry cache;
1694 u32 mask;
1695
1696 /* access call failed or for some reason the server doesn't
1697 * support any access modes -- defer access call until later */
1698 if (opendata->o_res.access_supported == 0)
1699 return 0;
1700
1701 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001702 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001703 * write mode bits, but POSIX allows the creating process to write.
1704 * use openflags to check for exec, because fmode won't
1705 * always have FMODE_EXEC set when file open for exec. */
1706 if (openflags & __FMODE_EXEC) {
1707 /* ONLY check for exec rights */
1708 mask = MAY_EXEC;
1709 } else if (fmode & FMODE_READ)
1710 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001711
1712 cache.cred = cred;
1713 cache.jiffies = jiffies;
1714 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1715 nfs_access_add_cache(state->inode, &cache);
1716
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001717 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001718 return 0;
1719
1720 /* even though OPEN succeeded, access is denied. Close the file */
1721 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001722 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001723}
1724
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001725/*
1726 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1727 */
1728static int _nfs4_proc_open(struct nfs4_opendata *data)
1729{
1730 struct inode *dir = data->dir->d_inode;
1731 struct nfs_server *server = NFS_SERVER(dir);
1732 struct nfs_openargs *o_arg = &data->o_arg;
1733 struct nfs_openres *o_res = &data->o_res;
1734 int status;
1735
1736 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001737 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001739 if (status != 0) {
1740 if (status == -NFS4ERR_BADNAME &&
1741 !(o_arg->open_flags & O_CREAT))
1742 return -ENOENT;
1743 return status;
1744 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745
Trond Myklebust6926afd2012-01-07 13:22:46 -05001746 nfs_fattr_map_and_free_names(server, &data->f_attr);
1747
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001748 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001749 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001750 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1751 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001753 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001758 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Andy Adamsond83217c2011-03-01 01:34:17 +00001762static int nfs4_recover_expired_lease(struct nfs_server *server)
1763{
1764 return nfs4_client_recover_expired_lease(server->nfs_client);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * OPEN_EXPIRED:
1769 * reclaim state on the server after a network partition.
1770 * Assumes caller holds the appropriate lock
1771 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001772static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001774 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001775 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001777 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001778 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001779 if (IS_ERR(opendata))
1780 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001781 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001782 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001783 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001784 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001788static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001789{
Trond Myklebust539cd032007-06-05 11:46:42 -04001790 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001791 struct nfs4_exception exception = { };
1792 int err;
1793
1794 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001795 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001796 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1797 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001798 switch (err) {
1799 default:
1800 goto out;
1801 case -NFS4ERR_GRACE:
1802 case -NFS4ERR_DELAY:
1803 nfs4_handle_exception(server, err, &exception);
1804 err = 0;
1805 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001806 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001807out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001808 return err;
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Trond Myklebust864472e2006-01-03 09:55:15 +01001816 ctx = nfs4_state_find_open_context(state);
1817 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001818 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001819 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 put_nfs_open_context(ctx);
1821 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001824#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001825static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001826{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001827 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001828 nfs4_stateid *stateid = &state->stateid;
1829 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001830
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001831 /* If a state reset has been done, test_stateid is unneeded */
1832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1833 return;
1834
1835 status = nfs41_test_stateid(server, stateid);
1836 if (status != NFS_OK) {
1837 /* Free the stateid unless the server explicitly
1838 * informs us the stateid is unrecognized. */
1839 if (status != -NFS4ERR_BAD_STATEID)
1840 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001841 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001842
Bryan Schumaker69388672012-09-26 15:25:52 -04001843 write_seqlock(&state->seqlock);
1844 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1845 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001846 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1847 }
1848}
1849
1850/**
1851 * nfs41_check_open_stateid - possibly free an open stateid
1852 *
1853 * @state: NFSv4 state for an inode
1854 *
1855 * Returns NFS_OK if recovery for this stateid is now finished.
1856 * Otherwise a negative NFS4ERR value is returned.
1857 */
1858static int nfs41_check_open_stateid(struct nfs4_state *state)
1859{
1860 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001861 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 int status;
1863
1864 /* If a state reset has been done, test_stateid is unneeded */
1865 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1867 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1868 return -NFS4ERR_BAD_STATEID;
1869
1870 status = nfs41_test_stateid(server, stateid);
1871 if (status != NFS_OK) {
1872 /* Free the stateid unless the server explicitly
1873 * informs us the stateid is unrecognized. */
1874 if (status != -NFS4ERR_BAD_STATEID)
1875 nfs41_free_stateid(server, stateid);
1876
1877 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1879 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001880 }
1881 return status;
1882}
1883
1884static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1885{
Chuck Levereb64cf92012-07-11 16:30:05 -04001886 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001887
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001888 nfs41_clear_delegation_stateid(state);
1889 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001890 if (status != NFS_OK)
1891 status = nfs4_open_expired(sp, state);
1892 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001893}
1894#endif
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001897 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1898 * fields corresponding to attributes that were used to store the verifier.
1899 * Make sure we clobber those fields in the later setattr call
1900 */
1901static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1902{
1903 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1904 !(sattr->ia_valid & ATTR_ATIME_SET))
1905 sattr->ia_valid |= ATTR_ATIME;
1906
1907 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1908 !(sattr->ia_valid & ATTR_MTIME_SET))
1909 sattr->ia_valid |= ATTR_MTIME;
1910}
1911
Trond Myklebustc21443c2013-02-07 14:26:21 -05001912static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1913 fmode_t fmode,
1914 int flags,
1915 struct nfs4_state **res)
1916{
1917 struct nfs4_state_owner *sp = opendata->owner;
1918 struct nfs_server *server = sp->so_server;
1919 struct nfs4_state *state;
1920 unsigned int seq;
1921 int ret;
1922
1923 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1924
1925 ret = _nfs4_proc_open(opendata);
1926 if (ret != 0)
1927 goto out;
1928
1929 state = nfs4_opendata_to_nfs4_state(opendata);
1930 ret = PTR_ERR(state);
1931 if (IS_ERR(state))
1932 goto out;
1933 if (server->caps & NFS_CAP_POSIX_LOCK)
1934 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1935
1936 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1937 if (ret != 0)
1938 goto out;
1939
Trond Myklebust91876b12013-03-28 14:01:33 -04001940 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001941 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001942 *res = state;
1943out:
1944 return ret;
1945}
1946
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001947/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 */
Andy Adamson82be4172012-05-23 05:02:35 -04001950static int _nfs4_do_open(struct inode *dir,
1951 struct dentry *dentry,
1952 fmode_t fmode,
1953 int flags,
1954 struct iattr *sattr,
1955 struct rpc_cred *cred,
1956 struct nfs4_state **res,
1957 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct nfs4_state_owner *sp;
1960 struct nfs4_state *state = NULL;
1961 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001962 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001963 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001964 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001968 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1969 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1971 goto out_err;
1972 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001973 status = nfs4_recover_expired_lease(server);
1974 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001975 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001976 if (dentry->d_inode != NULL)
1977 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001978 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001979 if (dentry->d_inode)
1980 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001981 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001982 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001983 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001984 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Andy Adamson82be4172012-05-23 05:02:35 -04001986 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1987 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1988 if (!opendata->f_attr.mdsthreshold)
1989 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001990 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001991 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001992 if (dentry->d_inode != NULL)
1993 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001994
Trond Myklebustc21443c2013-02-07 14:26:21 -05001995 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001996 if (status != 0)
1997 goto err_opendata_put;
1998
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001999 if (opendata->o_arg.open_flags & O_EXCL) {
2000 nfs4_exclusive_attrset(opendata, sattr);
2001
2002 nfs_fattr_init(opendata->o_res.f_attr);
2003 status = nfs4_do_setattr(state->inode, cred,
2004 opendata->o_res.f_attr, sattr,
2005 state);
2006 if (status == 0)
2007 nfs_setattr_update_inode(state->inode, sattr);
2008 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2009 }
Andy Adamson82be4172012-05-23 05:02:35 -04002010
2011 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2012 *ctx_th = opendata->f_attr.mdsthreshold;
2013 else
2014 kfree(opendata->f_attr.mdsthreshold);
2015 opendata->f_attr.mdsthreshold = NULL;
2016
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002017 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *res = state;
2020 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002021err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002022 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002023 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002024err_put_state_owner:
2025 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 *res = NULL;
2028 return status;
2029}
2030
2031
Andy Adamson82be4172012-05-23 05:02:35 -04002032static struct nfs4_state *nfs4_do_open(struct inode *dir,
2033 struct dentry *dentry,
2034 fmode_t fmode,
2035 int flags,
2036 struct iattr *sattr,
2037 struct rpc_cred *cred,
2038 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002040 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 struct nfs4_exception exception = { };
2042 struct nfs4_state *res;
2043 int status;
2044
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002045 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002047 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2048 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (status == 0)
2050 break;
2051 /* NOTE: BAD_SEQID means the server and client disagree about the
2052 * book-keeping w.r.t. state-changing operations
2053 * (OPEN/CLOSE/LOCK/LOCKU...)
2054 * It is actually a sign of a bug on the client or on the server.
2055 *
2056 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002057 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * have unhashed the old state_owner for us, and that we can
2059 * therefore safely retry using a new one. We should still warn
2060 * the user though...
2061 */
2062 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002063 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002064 " returned a bad sequence-id error!\n",
2065 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 exception.retry = 1;
2067 continue;
2068 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002069 /*
2070 * BAD_STATEID on OPEN means that the server cancelled our
2071 * state before it received the OPEN_CONFIRM.
2072 * Recover by retrying the request as per the discussion
2073 * on Page 181 of RFC3530.
2074 */
2075 if (status == -NFS4ERR_BAD_STATEID) {
2076 exception.retry = 1;
2077 continue;
2078 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002079 if (status == -EAGAIN) {
2080 /* We must have found a delegation */
2081 exception.retry = 1;
2082 continue;
2083 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002084 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2085 continue;
2086 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 status, &exception));
2088 } while (exception.retry);
2089 return res;
2090}
2091
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002092static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2093 struct nfs_fattr *fattr, struct iattr *sattr,
2094 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002096 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002098 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 .iap = sattr,
2100 .server = server,
2101 .bitmask = server->attr_bitmask,
2102 };
2103 struct nfs_setattrres res = {
2104 .fattr = fattr,
2105 .server = server,
2106 };
2107 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2109 .rpc_argp = &arg,
2110 .rpc_resp = &res,
2111 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002114 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Trond Myklebust0e574af2005-10-27 22:12:38 -04002116 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Trond Myklebust5d422302013-03-14 16:57:48 -04002118 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002119 struct nfs_lockowner lockowner = {
2120 .l_owner = current->files,
2121 .l_pid = current->tgid,
2122 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002123 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002124 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002125 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2126 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002127 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002128 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Bryan Schumaker7c513052011-03-24 17:12:24 +00002131 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002132 if (status == 0 && state != NULL)
2133 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002134 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002137static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2138 struct nfs_fattr *fattr, struct iattr *sattr,
2139 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002141 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002142 struct nfs4_exception exception = {
2143 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002144 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 int err;
2147 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002148 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2149 switch (err) {
2150 case -NFS4ERR_OPENMODE:
2151 if (state && !(state->state & FMODE_WRITE)) {
2152 err = -EBADF;
2153 if (sattr->ia_valid & ATTR_OPEN)
2154 err = -EACCES;
2155 goto out;
2156 }
2157 }
2158 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002160out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return err;
2162}
2163
2164struct nfs4_closedata {
2165 struct inode *inode;
2166 struct nfs4_state *state;
2167 struct nfs_closeargs arg;
2168 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002169 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002170 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002171 bool roc;
2172 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173};
2174
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002175static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002176{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002177 struct nfs4_closedata *calldata = data;
2178 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002179 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002180
Fred Isamanf7e89172011-01-06 11:36:32 +00002181 if (calldata->roc)
2182 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002183 nfs4_put_open_state(calldata->state);
2184 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002185 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002186 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002187 kfree(calldata);
2188}
2189
Trond Myklebust88069f72009-12-08 08:33:16 -05002190static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2191 fmode_t fmode)
2192{
2193 spin_lock(&state->owner->so_lock);
2194 if (!(fmode & FMODE_READ))
2195 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2196 if (!(fmode & FMODE_WRITE))
2197 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2198 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2199 spin_unlock(&state->owner->so_lock);
2200}
2201
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002202static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002204 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct nfs_server *server = NFS_SERVER(calldata->inode);
2207
Chuck Levera3ca5652012-03-01 17:00:40 -05002208 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002209 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* hmm. we are done with the inode, and in the process of freeing
2212 * the state_owner. we keep this around to process errors
2213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 switch (task->tk_status) {
2215 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002216 if (calldata->roc)
2217 pnfs_roc_set_barrier(state->inode,
2218 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002219 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002220 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002221 nfs4_close_clear_stateid_flags(state,
2222 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002225 case -NFS4ERR_OLD_STATEID:
2226 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002228 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002231 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2232 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002234 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002235 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002236 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002239static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002241 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002242 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002243 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002244 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002245
Chuck Levera3ca5652012-03-01 17:00:40 -05002246 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002247 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002248 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002249
Trond Myklebust88069f72009-12-08 08:33:16 -05002250 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2251 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002252 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002253 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002254 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002255 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002256 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2257 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2258 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002259 }
2260 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002261 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2262 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2263 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002264 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002265 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002266 if (!nfs4_valid_open_stateid(state))
2267 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002268 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002269
2270 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002271 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002272 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002273 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002274
Fred Isamanf7e89172011-01-06 11:36:32 +00002275 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002276 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002277 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002278 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2279 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002280 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002281 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002282 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002283
Trond Myklebust516a6af2005-10-27 22:12:41 -04002284 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002285 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002286 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002287 &calldata->arg.seq_args,
2288 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002289 task) != 0)
2290 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002291 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002292 return;
2293out_no_action:
2294 task->tk_action = NULL;
2295out_wait:
2296 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002299static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002300 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002301 .rpc_call_done = nfs4_close_done,
2302 .rpc_release = nfs4_free_closedata,
2303};
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/*
2306 * It is possible for data to be read/written from a mem-mapped file
2307 * after the sys_close call (which hits the vfs layer as a flush).
2308 * This means that we can't safely call nfsv4 close on a file until
2309 * the inode is cleared. This in turn means that we are not good
2310 * NFSv4 citizens - we do not indicate to the server to update the file's
2311 * share state even when we are done with one of the three share
2312 * stateid's in the inode.
2313 *
2314 * NOTE: Caller must be holding the sp->so_owner semaphore!
2315 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002316int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002318 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002320 struct nfs4_state_owner *sp = state->owner;
2321 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002322 struct rpc_message msg = {
2323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2324 .rpc_cred = state->owner->so_cred,
2325 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002326 struct rpc_task_setup task_setup_data = {
2327 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002328 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002329 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002330 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 .flags = RPC_TASK_ASYNC,
2332 };
Trond Myklebust95121352005-10-18 14:20:12 -07002333 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002335 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002337 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002338 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002339 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002341 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002342 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002344 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002345 if (calldata->arg.seqid == NULL)
2346 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002347 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002348 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002349 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002350 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002351 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002352 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002353 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002354
Trond Myklebust1174dd12010-12-21 10:52:24 -05002355 msg.rpc_argp = &calldata->arg;
2356 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002357 task_setup_data.callback_data = calldata;
2358 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002359 if (IS_ERR(task))
2360 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002361 status = 0;
2362 if (wait)
2363 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002364 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002365 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002366out_free_calldata:
2367 kfree(calldata);
2368out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002369 nfs4_put_open_state(state);
2370 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002371 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Trond Myklebust2b484292010-09-17 10:56:51 -04002374static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002375nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct nfs4_state *state;
2378
Trond Myklebust565277f2007-10-15 18:17:53 -04002379 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002380 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2381 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002382 if (IS_ERR(state))
2383 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002384 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002385 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Trond Myklebust1185a552009-12-03 15:54:02 -05002388static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002389{
2390 if (ctx->state == NULL)
2391 return;
2392 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002393 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002394 else
Al Viro643168c2011-06-22 18:20:23 -04002395 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2399{
Benny Halevy43652ad2009-04-01 09:21:54 -04002400 struct nfs4_server_caps_arg args = {
2401 .fhandle = fhandle,
2402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct nfs4_server_caps_res res = {};
2404 struct rpc_message msg = {
2405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002406 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 .rpc_resp = &res,
2408 };
2409 int status;
2410
Bryan Schumaker7c513052011-03-24 17:12:24 +00002411 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (status == 0) {
2413 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002414 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2415 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2416 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2417 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2418 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2420 server->caps |= NFS_CAP_ACLS;
2421 if (res.has_links != 0)
2422 server->caps |= NFS_CAP_HARDLINKS;
2423 if (res.has_symlinks != 0)
2424 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002425 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2426 server->caps |= NFS_CAP_FILEID;
2427 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2428 server->caps |= NFS_CAP_MODE;
2429 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2430 server->caps |= NFS_CAP_NLINK;
2431 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2432 server->caps |= NFS_CAP_OWNER;
2433 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2434 server->caps |= NFS_CAP_OWNER_GROUP;
2435 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2436 server->caps |= NFS_CAP_ATIME;
2437 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2438 server->caps |= NFS_CAP_CTIME;
2439 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2440 server->caps |= NFS_CAP_MTIME;
2441
Trond Myklebusta65318b2009-03-11 14:10:28 -04002442 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2443 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2444 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002446 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return status;
2450}
2451
Trond Myklebust55a97592006-06-09 09:34:19 -04002452int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 struct nfs4_exception exception = { };
2455 int err;
2456 do {
2457 err = nfs4_handle_exception(server,
2458 _nfs4_server_capabilities(server, fhandle),
2459 &exception);
2460 } while (exception.retry);
2461 return err;
2462}
2463
2464static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2465 struct nfs_fsinfo *info)
2466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct nfs4_lookup_root_arg args = {
2468 .bitmask = nfs4_fattr_bitmap,
2469 };
2470 struct nfs4_lookup_res res = {
2471 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002472 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 .fh = fhandle,
2474 };
2475 struct rpc_message msg = {
2476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2477 .rpc_argp = &args,
2478 .rpc_resp = &res,
2479 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002480
Trond Myklebust0e574af2005-10-27 22:12:38 -04002481 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002482 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2486 struct nfs_fsinfo *info)
2487{
2488 struct nfs4_exception exception = { };
2489 int err;
2490 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002491 err = _nfs4_lookup_root(server, fhandle, info);
2492 switch (err) {
2493 case 0:
2494 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002495 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002496 default:
2497 err = nfs4_handle_exception(server, err, &exception);
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002500out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return err;
2502}
2503
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002504static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2505 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2506{
2507 struct rpc_auth *auth;
2508 int ret;
2509
2510 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002511 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002512 ret = -EIO;
2513 goto out;
2514 }
2515 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002516out:
2517 return ret;
2518}
2519
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002520static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002521 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002523 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002524 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002526 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002527 if (len < 0)
2528 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002529
2530 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002531 /* AUTH_UNIX is the default flavor if none was specified,
2532 * thus has already been tried. */
2533 if (flav_array[i] == RPC_AUTH_UNIX)
2534 continue;
2535
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002536 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002537 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002538 continue;
2539 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002540 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002541 /*
2542 * -EACCESS could mean that the user doesn't have correct permissions
2543 * to access the mount. It could also mean that we tried to mount
2544 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2545 * existing mount programs don't handle -EACCES very well so it should
2546 * be mapped to -EPERM instead.
2547 */
2548 if (status == -EACCES)
2549 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002550 return status;
2551}
2552
2553/*
2554 * get the file handle for the "/" directory on the server
2555 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002556int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2557 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002558{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002559 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002560 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002561 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2562 /*
2563 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2564 * by nfs4_map_errors() as this function exits.
2565 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002566 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (status == 0)
2568 status = nfs4_server_capabilities(server, fhandle);
2569 if (status == 0)
2570 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002571 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Bryan Schumakerbae36242012-05-10 15:07:31 -04002574static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2575 struct nfs_fsinfo *info)
2576{
2577 int error;
2578 struct nfs_fattr *fattr = info->fattr;
2579
2580 error = nfs4_server_capabilities(server, mntfh);
2581 if (error < 0) {
2582 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2583 return error;
2584 }
2585
2586 error = nfs4_proc_getattr(server, mntfh, fattr);
2587 if (error < 0) {
2588 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2589 return error;
2590 }
2591
2592 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2593 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2594 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2595
2596 return error;
2597}
2598
Manoj Naik6b97fd32006-06-09 09:34:29 -04002599/*
2600 * Get locations and (maybe) other attributes of a referral.
2601 * Note that we'll actually follow the referral later when
2602 * we detect fsid mismatch in inode revalidation
2603 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002604static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2605 const struct qstr *name, struct nfs_fattr *fattr,
2606 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002607{
2608 int status = -ENOMEM;
2609 struct page *page = NULL;
2610 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002611
2612 page = alloc_page(GFP_KERNEL);
2613 if (page == NULL)
2614 goto out;
2615 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2616 if (locations == NULL)
2617 goto out;
2618
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002619 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002620 if (status != 0)
2621 goto out;
2622 /* Make sure server returned a different fsid for the referral */
2623 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002624 dprintk("%s: server did not return a different fsid for"
2625 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002626 status = -EIO;
2627 goto out;
2628 }
Andy Adamson533eb462011-06-13 18:25:56 -04002629 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2630 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631
Andy Adamson533eb462011-06-13 18:25:56 -04002632 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002633 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002634 memset(fhandle, 0, sizeof(struct nfs_fh));
2635out:
2636 if (page)
2637 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002638 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002639 return status;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2643{
2644 struct nfs4_getattr_arg args = {
2645 .fh = fhandle,
2646 .bitmask = server->attr_bitmask,
2647 };
2648 struct nfs4_getattr_res res = {
2649 .fattr = fattr,
2650 .server = server,
2651 };
2652 struct rpc_message msg = {
2653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2654 .rpc_argp = &args,
2655 .rpc_resp = &res,
2656 };
2657
Trond Myklebust0e574af2005-10-27 22:12:38 -04002658 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002659 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
2662static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2663{
2664 struct nfs4_exception exception = { };
2665 int err;
2666 do {
2667 err = nfs4_handle_exception(server,
2668 _nfs4_proc_getattr(server, fhandle, fattr),
2669 &exception);
2670 } while (exception.retry);
2671 return err;
2672}
2673
2674/*
2675 * The file is not closed if it is opened due to the a request to change
2676 * the size of the file. The open call will not be needed once the
2677 * VFS layer lookup-intents are implemented.
2678 *
2679 * Close is called when the inode is destroyed.
2680 * If we haven't opened the file for O_WRONLY, we
2681 * need to in the size_change case to obtain a stateid.
2682 *
2683 * Got race?
2684 * Because OPEN is always done by name in nfsv4, it is
2685 * possible that we opened a different file by the same
2686 * name. We can recognize this race condition, but we
2687 * can't do anything about it besides returning an error.
2688 *
2689 * This will be fixed with VFS changes (lookup-intent).
2690 */
2691static int
2692nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2693 struct iattr *sattr)
2694{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002695 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002696 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002697 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 int status;
2699
Benny Halevy8a1636c2010-07-14 15:43:57 -04002700 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002701 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002702
Trond Myklebust0e574af2005-10-27 22:12:38 -04002703 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Andy Adamson26699402012-05-30 16:12:24 -04002705 /* Deal with open(O_TRUNC) */
2706 if (sattr->ia_valid & ATTR_OPEN)
2707 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2708
2709 /* Optimization: if the end result is no change, don't RPC */
2710 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2711 return 0;
2712
Trond Myklebustd5308382005-11-04 15:33:38 -05002713 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002714 if (sattr->ia_valid & ATTR_FILE) {
2715 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002716
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002717 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002718 if (ctx) {
2719 cred = ctx->cred;
2720 state = ctx->state;
2721 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002722 }
2723
2724 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002725 if (status == 0)
2726 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return status;
2728}
2729
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002730static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2731 const struct qstr *name, struct nfs_fh *fhandle,
2732 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002733{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002734 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002735 int status;
2736 struct nfs4_lookup_arg args = {
2737 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002738 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002739 .name = name,
2740 };
2741 struct nfs4_lookup_res res = {
2742 .server = server,
2743 .fattr = fattr,
2744 .fh = fhandle,
2745 };
2746 struct rpc_message msg = {
2747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2748 .rpc_argp = &args,
2749 .rpc_resp = &res,
2750 };
2751
2752 nfs_fattr_init(fattr);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002755 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 dprintk("NFS reply lookup: %d\n", status);
2757 return status;
2758}
2759
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002760static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002761{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002762 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002763 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002764 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2765 fattr->nlink = 2;
2766}
2767
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002768static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2769 struct qstr *name, struct nfs_fh *fhandle,
2770 struct nfs_fattr *fattr)
2771{
2772 struct nfs4_exception exception = { };
2773 struct rpc_clnt *client = *clnt;
2774 int err;
2775 do {
2776 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2777 switch (err) {
2778 case -NFS4ERR_BADNAME:
2779 err = -ENOENT;
2780 goto out;
2781 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002782 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002783 goto out;
2784 case -NFS4ERR_WRONGSEC:
2785 err = -EPERM;
2786 if (client != *clnt)
2787 goto out;
2788
2789 client = nfs4_create_sec_client(client, dir, name);
2790 if (IS_ERR(client))
2791 return PTR_ERR(client);
2792
2793 exception.retry = 1;
2794 break;
2795 default:
2796 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2797 }
2798 } while (exception.retry);
2799
2800out:
2801 if (err == 0)
2802 *clnt = client;
2803 else if (client != *clnt)
2804 rpc_shutdown_client(client);
2805
2806 return err;
2807}
2808
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002809static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002810 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002812 int status;
2813 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002814
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002815 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2816 if (client != NFS_CLIENT(dir)) {
2817 rpc_shutdown_client(client);
2818 nfs_fixup_secinfo_attributes(fattr);
2819 }
2820 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002823struct rpc_clnt *
2824nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2825 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2826{
2827 int status;
2828 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2829
2830 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2831 if (status < 0) {
2832 rpc_shutdown_client(client);
2833 return ERR_PTR(status);
2834 }
2835 return client;
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2839{
Trond Myklebust76b32992007-08-10 17:45:11 -04002840 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 struct nfs4_accessargs args = {
2842 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002843 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002845 struct nfs4_accessres res = {
2846 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct rpc_message msg = {
2849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2850 .rpc_argp = &args,
2851 .rpc_resp = &res,
2852 .rpc_cred = entry->cred,
2853 };
2854 int mode = entry->mask;
2855 int status;
2856
2857 /*
2858 * Determine which access bits we want to ask for...
2859 */
2860 if (mode & MAY_READ)
2861 args.access |= NFS4_ACCESS_READ;
2862 if (S_ISDIR(inode->i_mode)) {
2863 if (mode & MAY_WRITE)
2864 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2865 if (mode & MAY_EXEC)
2866 args.access |= NFS4_ACCESS_LOOKUP;
2867 } else {
2868 if (mode & MAY_WRITE)
2869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2870 if (mode & MAY_EXEC)
2871 args.access |= NFS4_ACCESS_EXECUTE;
2872 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002873
2874 res.fattr = nfs_alloc_fattr();
2875 if (res.fattr == NULL)
2876 return -ENOMEM;
2877
Bryan Schumaker7c513052011-03-24 17:12:24 +00002878 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002880 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002881 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002883 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return status;
2885}
2886
2887static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2888{
2889 struct nfs4_exception exception = { };
2890 int err;
2891 do {
2892 err = nfs4_handle_exception(NFS_SERVER(inode),
2893 _nfs4_proc_access(inode, entry),
2894 &exception);
2895 } while (exception.retry);
2896 return err;
2897}
2898
2899/*
2900 * TODO: For the time being, we don't try to get any attributes
2901 * along with any of the zero-copy operations READ, READDIR,
2902 * READLINK, WRITE.
2903 *
2904 * In the case of the first three, we want to put the GETATTR
2905 * after the read-type operation -- this is because it is hard
2906 * to predict the length of a GETATTR response in v4, and thus
2907 * align the READ data correctly. This means that the GETATTR
2908 * may end up partially falling into the page cache, and we should
2909 * shift it into the 'tail' of the xdr_buf before processing.
2910 * To do this efficiently, we need to know the total length
2911 * of data received, which doesn't seem to be available outside
2912 * of the RPC layer.
2913 *
2914 * In the case of WRITE, we also want to put the GETATTR after
2915 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002916 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 *
2918 * Both of these changes to the XDR layer would in fact be quite
2919 * minor, but I decided to leave them for a subsequent patch.
2920 */
2921static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2922 unsigned int pgbase, unsigned int pglen)
2923{
2924 struct nfs4_readlink args = {
2925 .fh = NFS_FH(inode),
2926 .pgbase = pgbase,
2927 .pglen = pglen,
2928 .pages = &page,
2929 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002930 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2933 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002934 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 };
2936
Bryan Schumaker7c513052011-03-24 17:12:24 +00002937 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 -07002938}
2939
2940static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2941 unsigned int pgbase, unsigned int pglen)
2942{
2943 struct nfs4_exception exception = { };
2944 int err;
2945 do {
2946 err = nfs4_handle_exception(NFS_SERVER(inode),
2947 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2948 &exception);
2949 } while (exception.retry);
2950 return err;
2951}
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002954 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956static int
2957nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002958 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002960 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 int status = 0;
2963
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002964 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2965 if (IS_ERR(ctx))
2966 return PTR_ERR(ctx);
2967
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002968 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002969 state = nfs4_do_open(dir, dentry, ctx->mode,
2970 flags, sattr, ctx->cred,
2971 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002972 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (IS_ERR(state)) {
2974 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002977 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002978 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002979 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002981 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return status;
2983}
2984
2985static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2986{
Trond Myklebust16e42952005-10-27 22:12:44 -04002987 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002988 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002990 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002992 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002993 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2997 .rpc_argp = &args,
2998 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003000 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003001
Bryan Schumaker7c513052011-03-24 17:12:24 +00003002 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003003 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003004 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return status;
3006}
3007
3008static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3009{
3010 struct nfs4_exception exception = { };
3011 int err;
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(dir),
3014 _nfs4_proc_remove(dir, name),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003020static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003022 struct nfs_server *server = NFS_SERVER(dir);
3023 struct nfs_removeargs *args = msg->rpc_argp;
3024 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003026 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003028 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003031static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3032{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003033 nfs4_setup_sequence(NFS_SERVER(data->dir),
3034 &data->args.seq_args,
3035 &data->res.seq_res,
3036 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003039static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003041 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3042
Trond Myklebust14516c32010-07-31 14:29:06 -04003043 if (!nfs4_sequence_done(task, &res->seq_res))
3044 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003045 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003046 return 0;
3047 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003048 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Jeff Laytond3d41522010-09-17 17:31:57 -04003051static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3052{
3053 struct nfs_server *server = NFS_SERVER(dir);
3054 struct nfs_renameargs *arg = msg->rpc_argp;
3055 struct nfs_renameres *res = msg->rpc_resp;
3056
3057 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003058 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003059 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003060}
3061
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003062static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3063{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003064 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3065 &data->args.seq_args,
3066 &data->res.seq_res,
3067 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003068}
3069
3070static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3071 struct inode *new_dir)
3072{
3073 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3074
3075 if (!nfs4_sequence_done(task, &res->seq_res))
3076 return 0;
3077 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3078 return 0;
3079
3080 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003081 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003082 return 1;
3083}
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3086 struct inode *new_dir, struct qstr *new_name)
3087{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003088 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003089 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 .old_dir = NFS_FH(old_dir),
3091 .new_dir = NFS_FH(new_dir),
3092 .old_name = old_name,
3093 .new_name = new_name,
3094 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003095 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003096 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct rpc_message msg = {
3099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3100 .rpc_argp = &arg,
3101 .rpc_resp = &res,
3102 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003103 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Bryan Schumaker7c513052011-03-24 17:12:24 +00003105 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (!status) {
3107 update_changeattr(old_dir, &res.old_cinfo);
3108 update_changeattr(new_dir, &res.new_cinfo);
3109 }
3110 return status;
3111}
3112
3113static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3114 struct inode *new_dir, struct qstr *new_name)
3115{
3116 struct nfs4_exception exception = { };
3117 int err;
3118 do {
3119 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3120 _nfs4_proc_rename(old_dir, old_name,
3121 new_dir, new_name),
3122 &exception);
3123 } while (exception.retry);
3124 return err;
3125}
3126
3127static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3128{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003129 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct nfs4_link_arg arg = {
3131 .fh = NFS_FH(inode),
3132 .dir_fh = NFS_FH(dir),
3133 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003134 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003136 struct nfs4_link_res res = {
3137 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct rpc_message msg = {
3140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3141 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003142 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003144 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Trond Myklebust136f2622010-04-16 16:22:49 -04003146 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003147 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003148 goto out;
3149
Bryan Schumaker7c513052011-03-24 17:12:24 +00003150 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 if (!status) {
3152 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003153 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003154 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003155out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003156 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return status;
3158}
3159
3160static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3161{
3162 struct nfs4_exception exception = { };
3163 int err;
3164 do {
3165 err = nfs4_handle_exception(NFS_SERVER(inode),
3166 _nfs4_proc_link(inode, dir, name),
3167 &exception);
3168 } while (exception.retry);
3169 return err;
3170}
3171
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172struct nfs4_createdata {
3173 struct rpc_message msg;
3174 struct nfs4_create_arg arg;
3175 struct nfs4_create_res res;
3176 struct nfs_fh fh;
3177 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003178};
3179
3180static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3181 struct qstr *name, struct iattr *sattr, u32 ftype)
3182{
3183 struct nfs4_createdata *data;
3184
3185 data = kzalloc(sizeof(*data), GFP_KERNEL);
3186 if (data != NULL) {
3187 struct nfs_server *server = NFS_SERVER(dir);
3188
3189 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3190 data->msg.rpc_argp = &data->arg;
3191 data->msg.rpc_resp = &data->res;
3192 data->arg.dir_fh = NFS_FH(dir);
3193 data->arg.server = server;
3194 data->arg.name = name;
3195 data->arg.attrs = sattr;
3196 data->arg.ftype = ftype;
3197 data->arg.bitmask = server->attr_bitmask;
3198 data->res.server = server;
3199 data->res.fh = &data->fh;
3200 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003201 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003202 }
3203 return data;
3204}
3205
3206static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3207{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003208 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003209 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 if (status == 0) {
3211 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003212 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3213 }
3214 return status;
3215}
3216
3217static void nfs4_free_createdata(struct nfs4_createdata *data)
3218{
3219 kfree(data);
3220}
3221
Chuck Lever4f390c12006-08-22 20:06:22 -04003222static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003223 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003225 struct nfs4_createdata *data;
3226 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Chuck Lever94a6d752006-08-22 20:06:23 -04003228 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003230
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003231 status = -ENOMEM;
3232 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3233 if (data == NULL)
3234 goto out;
3235
3236 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3237 data->arg.u.symlink.pages = &page;
3238 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 status = nfs4_do_create(dir, dentry, data);
3241
3242 nfs4_free_createdata(data);
3243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return status;
3245}
3246
Chuck Lever4f390c12006-08-22 20:06:22 -04003247static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003248 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
3250 struct nfs4_exception exception = { };
3251 int err;
3252 do {
3253 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003254 _nfs4_proc_symlink(dir, dentry, page,
3255 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 &exception);
3257 } while (exception.retry);
3258 return err;
3259}
3260
3261static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3262 struct iattr *sattr)
3263{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003264 struct nfs4_createdata *data;
3265 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3268 if (data == NULL)
3269 goto out;
3270
3271 status = nfs4_do_create(dir, dentry, data);
3272
3273 nfs4_free_createdata(data);
3274out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return status;
3276}
3277
3278static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr)
3280{
3281 struct nfs4_exception exception = { };
3282 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003283
3284 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 do {
3286 err = nfs4_handle_exception(NFS_SERVER(dir),
3287 _nfs4_proc_mkdir(dir, dentry, sattr),
3288 &exception);
3289 } while (exception.retry);
3290 return err;
3291}
3292
3293static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003294 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
3296 struct inode *dir = dentry->d_inode;
3297 struct nfs4_readdir_arg args = {
3298 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003299 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .pgbase = 0,
3301 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003302 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003303 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 };
3305 struct nfs4_readdir_res res;
3306 struct rpc_message msg = {
3307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3308 .rpc_argp = &args,
3309 .rpc_resp = &res,
3310 .rpc_cred = cred,
3311 };
3312 int status;
3313
Harvey Harrison3110ff82008-05-02 13:42:44 -07003314 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003315 dentry->d_parent->d_name.name,
3316 dentry->d_name.name,
3317 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003318 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003320 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 -05003321 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003322 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003323 status += args.pgbase;
3324 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003325
3326 nfs_invalidate_atime(dir);
3327
Harvey Harrison3110ff82008-05-02 13:42:44 -07003328 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return status;
3330}
3331
3332static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003333 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 struct nfs4_exception exception = { };
3336 int err;
3337 do {
3338 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3339 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003340 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 &exception);
3342 } while (exception.retry);
3343 return err;
3344}
3345
3346static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3347 struct iattr *sattr, dev_t rdev)
3348{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003349 struct nfs4_createdata *data;
3350 int mode = sattr->ia_mode;
3351 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003353 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3354 if (data == NULL)
3355 goto out;
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003358 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003360 data->arg.ftype = NF4BLK;
3361 data->arg.u.device.specdata1 = MAJOR(rdev);
3362 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003365 data->arg.ftype = NF4CHR;
3366 data->arg.u.device.specdata1 = MAJOR(rdev);
3367 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003368 } else if (!S_ISSOCK(mode)) {
3369 status = -EINVAL;
3370 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003374out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003375 nfs4_free_createdata(data);
3376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return status;
3378}
3379
3380static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3381 struct iattr *sattr, dev_t rdev)
3382{
3383 struct nfs4_exception exception = { };
3384 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003385
3386 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 do {
3388 err = nfs4_handle_exception(NFS_SERVER(dir),
3389 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3390 &exception);
3391 } while (exception.retry);
3392 return err;
3393}
3394
3395static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3396 struct nfs_fsstat *fsstat)
3397{
3398 struct nfs4_statfs_arg args = {
3399 .fh = fhandle,
3400 .bitmask = server->attr_bitmask,
3401 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003402 struct nfs4_statfs_res res = {
3403 .fsstat = fsstat,
3404 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3407 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003408 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 };
3410
Trond Myklebust0e574af2005-10-27 22:12:38 -04003411 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003412 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
3415static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3416{
3417 struct nfs4_exception exception = { };
3418 int err;
3419 do {
3420 err = nfs4_handle_exception(server,
3421 _nfs4_proc_statfs(server, fhandle, fsstat),
3422 &exception);
3423 } while (exception.retry);
3424 return err;
3425}
3426
3427static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3428 struct nfs_fsinfo *fsinfo)
3429{
3430 struct nfs4_fsinfo_arg args = {
3431 .fh = fhandle,
3432 .bitmask = server->attr_bitmask,
3433 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003434 struct nfs4_fsinfo_res res = {
3435 .fsinfo = fsinfo,
3436 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 struct rpc_message msg = {
3438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3439 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003440 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 };
3442
Bryan Schumaker7c513052011-03-24 17:12:24 +00003443 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
3446static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3447{
3448 struct nfs4_exception exception = { };
3449 int err;
3450
3451 do {
3452 err = nfs4_handle_exception(server,
3453 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3454 &exception);
3455 } while (exception.retry);
3456 return err;
3457}
3458
3459static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3460{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003461 int error;
3462
Trond Myklebust0e574af2005-10-27 22:12:38 -04003463 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003464 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003465 if (error == 0) {
3466 /* block layout checks this! */
3467 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003468 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003469 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003470
3471 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
3474static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3475 struct nfs_pathconf *pathconf)
3476{
3477 struct nfs4_pathconf_arg args = {
3478 .fh = fhandle,
3479 .bitmask = server->attr_bitmask,
3480 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003481 struct nfs4_pathconf_res res = {
3482 .pathconf = pathconf,
3483 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 struct rpc_message msg = {
3485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3486 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003487 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 };
3489
3490 /* None of the pathconf attributes are mandatory to implement */
3491 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3492 memset(pathconf, 0, sizeof(*pathconf));
3493 return 0;
3494 }
3495
Trond Myklebust0e574af2005-10-27 22:12:38 -04003496 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003497 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
3500static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3501 struct nfs_pathconf *pathconf)
3502{
3503 struct nfs4_exception exception = { };
3504 int err;
3505
3506 do {
3507 err = nfs4_handle_exception(server,
3508 _nfs4_proc_pathconf(server, fhandle, pathconf),
3509 &exception);
3510 } while (exception.retry);
3511 return err;
3512}
3513
Trond Myklebust5521abf2013-03-16 20:54:34 -04003514int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003515 const struct nfs_open_context *ctx,
3516 const struct nfs_lock_context *l_ctx,
3517 fmode_t fmode)
3518{
3519 const struct nfs_lockowner *lockowner = NULL;
3520
3521 if (l_ctx != NULL)
3522 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003523 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003524}
3525EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3526
Trond Myklebust5521abf2013-03-16 20:54:34 -04003527static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3528 const struct nfs_open_context *ctx,
3529 const struct nfs_lock_context *l_ctx,
3530 fmode_t fmode)
3531{
3532 nfs4_stateid current_stateid;
3533
3534 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3535 return false;
3536 return nfs4_stateid_match(stateid, &current_stateid);
3537}
3538
3539static bool nfs4_error_stateid_expired(int err)
3540{
3541 switch (err) {
3542 case -NFS4ERR_DELEG_REVOKED:
3543 case -NFS4ERR_ADMIN_REVOKED:
3544 case -NFS4ERR_BAD_STATEID:
3545 case -NFS4ERR_STALE_STATEID:
3546 case -NFS4ERR_OLD_STATEID:
3547 case -NFS4ERR_OPENMODE:
3548 case -NFS4ERR_EXPIRED:
3549 return true;
3550 }
3551 return false;
3552}
3553
Benny Halevyd20581a2011-05-22 19:52:03 +03003554void __nfs4_read_done_cb(struct nfs_read_data *data)
3555{
Fred Isamancd841602012-04-20 14:47:44 -04003556 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003557}
3558
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003559static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Fred Isamancd841602012-04-20 14:47:44 -04003561 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003563 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003564 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003565 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003567
Benny Halevyd20581a2011-05-22 19:52:03 +03003568 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003570 renew_lease(server, data->timestamp);
3571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Trond Myklebust5521abf2013-03-16 20:54:34 -04003574static bool nfs4_read_stateid_changed(struct rpc_task *task,
3575 struct nfs_readargs *args)
3576{
3577
3578 if (!nfs4_error_stateid_expired(task->tk_status) ||
3579 nfs4_stateid_is_current(&args->stateid,
3580 args->context,
3581 args->lock_context,
3582 FMODE_READ))
3583 return false;
3584 rpc_restart_call_prepare(task);
3585 return true;
3586}
3587
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003588static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3589{
3590
3591 dprintk("--> %s\n", __func__);
3592
3593 if (!nfs4_sequence_done(task, &data->res.seq_res))
3594 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003595 if (nfs4_read_stateid_changed(task, &data->args))
3596 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003597 return data->read_done_cb ? data->read_done_cb(task, data) :
3598 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003599}
3600
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003601static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003604 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003605 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003606 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003609static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3610{
Trond Myklebust9b206142013-03-17 15:52:00 -04003611 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003612 &data->args.seq_args,
3613 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003614 task))
3615 return;
3616 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3617 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Fred Isamanb029bc92011-03-03 15:13:42 +00003620static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Fred Isamancd841602012-04-20 14:47:44 -04003622 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003624 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003625 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003626 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003628 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003630 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003631 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Trond Myklebust5521abf2013-03-16 20:54:34 -04003635static bool nfs4_write_stateid_changed(struct rpc_task *task,
3636 struct nfs_writeargs *args)
3637{
3638
3639 if (!nfs4_error_stateid_expired(task->tk_status) ||
3640 nfs4_stateid_is_current(&args->stateid,
3641 args->context,
3642 args->lock_context,
3643 FMODE_WRITE))
3644 return false;
3645 rpc_restart_call_prepare(task);
3646 return true;
3647}
3648
Fred Isamanb029bc92011-03-03 15:13:42 +00003649static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3650{
3651 if (!nfs4_sequence_done(task, &data->res.seq_res))
3652 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003653 if (nfs4_write_stateid_changed(task, &data->args))
3654 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003655 return data->write_done_cb ? data->write_done_cb(task, data) :
3656 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003657}
3658
Trond Myklebust5a37f852012-04-28 14:55:16 -04003659static
3660bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003661{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003662 const struct nfs_pgio_header *hdr = data->header;
3663
3664 /* Don't request attributes for pNFS or O_DIRECT writes */
3665 if (data->ds_clp != NULL || hdr->dreq != NULL)
3666 return false;
3667 /* Otherwise, request attributes if and only if we don't hold
3668 * a delegation
3669 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003670 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003671}
Fred Isamana69aef12011-03-03 15:13:47 +00003672
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003673static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Fred Isamancd841602012-04-20 14:47:44 -04003675 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003676
Trond Myklebust5a37f852012-04-28 14:55:16 -04003677 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003678 data->args.bitmask = NULL;
3679 data->res.fattr = NULL;
3680 } else
3681 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003682
Fred Isamanb029bc92011-03-03 15:13:42 +00003683 if (!data->write_done_cb)
3684 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003685 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 data->timestamp = jiffies;
3687
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003688 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003689 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003692static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3693{
Trond Myklebust9b206142013-03-17 15:52:00 -04003694 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003695 &data->args.seq_args,
3696 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003697 task))
3698 return;
3699 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3700 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Fred Isaman0b7c0152012-04-20 14:47:39 -04003703static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3704{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003705 nfs4_setup_sequence(NFS_SERVER(data->inode),
3706 &data->args.seq_args,
3707 &data->res.seq_res,
3708 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
Fred Isaman0b7c0152012-04-20 14:47:39 -04003711static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003714
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003715 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003716 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003717 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Fred Isaman0b7c0152012-04-20 14:47:39 -04003722static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003723{
3724 if (!nfs4_sequence_done(task, &data->res.seq_res))
3725 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003726 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003727}
3728
Fred Isaman0b7c0152012-04-20 14:47:39 -04003729static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003731 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003732
Fred Isaman0b7c0152012-04-20 14:47:39 -04003733 if (data->commit_done_cb == NULL)
3734 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003735 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003736 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003737 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738}
3739
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003740struct nfs4_renewdata {
3741 struct nfs_client *client;
3742 unsigned long timestamp;
3743};
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745/*
3746 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3747 * standalone procedure for queueing an asynchronous RENEW.
3748 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003749static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003750{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003751 struct nfs4_renewdata *data = calldata;
3752 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003753
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003754 if (atomic_read(&clp->cl_count) > 1)
3755 nfs4_schedule_state_renewal(clp);
3756 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003758}
3759
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003760static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003762 struct nfs4_renewdata *data = calldata;
3763 struct nfs_client *clp = data->client;
3764 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003767 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003768 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3769 return;
3770 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003771 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003772 return;
3773 }
3774 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003776 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003779static const struct rpc_call_ops nfs4_renew_ops = {
3780 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003781 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003782};
3783
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003784static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3788 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003789 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003791 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003793 if (renew_flags == 0)
3794 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003795 if (!atomic_inc_not_zero(&clp->cl_count))
3796 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003797 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003798 if (data == NULL)
3799 return -ENOMEM;
3800 data->client = clp;
3801 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003802 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003803 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003806static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3810 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003811 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 };
3813 unsigned long now = jiffies;
3814 int status;
3815
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003816 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 if (status < 0)
3818 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003819 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return 0;
3821}
3822
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003823static inline int nfs4_server_supports_acls(struct nfs_server *server)
3824{
3825 return (server->caps & NFS_CAP_ACLS)
3826 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3827 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3828}
3829
Trond Myklebust21f498c2012-08-24 10:59:25 -04003830/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3831 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003832 * the stack.
3833 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003834#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003835
Neil Hormane9e3d722011-03-04 19:26:03 -05003836static int buf_to_pages_noslab(const void *buf, size_t buflen,
3837 struct page **pages, unsigned int *pgbase)
3838{
3839 struct page *newpage, **spages;
3840 int rc = 0;
3841 size_t len;
3842 spages = pages;
3843
3844 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003845 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003846 newpage = alloc_page(GFP_KERNEL);
3847
3848 if (newpage == NULL)
3849 goto unwind;
3850 memcpy(page_address(newpage), buf, len);
3851 buf += len;
3852 buflen -= len;
3853 *pages++ = newpage;
3854 rc++;
3855 } while (buflen != 0);
3856
3857 return rc;
3858
3859unwind:
3860 for(; rc > 0; rc--)
3861 __free_page(spages[rc-1]);
3862 return -ENOMEM;
3863}
3864
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865struct nfs4_cached_acl {
3866 int cached;
3867 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003868 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003869};
3870
3871static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003872{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003873 struct nfs_inode *nfsi = NFS_I(inode);
3874
3875 spin_lock(&inode->i_lock);
3876 kfree(nfsi->nfs4_acl);
3877 nfsi->nfs4_acl = acl;
3878 spin_unlock(&inode->i_lock);
3879}
3880
3881static void nfs4_zap_acl_attr(struct inode *inode)
3882{
3883 nfs4_set_cached_acl(inode, NULL);
3884}
3885
3886static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3887{
3888 struct nfs_inode *nfsi = NFS_I(inode);
3889 struct nfs4_cached_acl *acl;
3890 int ret = -ENOENT;
3891
3892 spin_lock(&inode->i_lock);
3893 acl = nfsi->nfs4_acl;
3894 if (acl == NULL)
3895 goto out;
3896 if (buf == NULL) /* user is just asking for length */
3897 goto out_len;
3898 if (acl->cached == 0)
3899 goto out;
3900 ret = -ERANGE; /* see getxattr(2) man page */
3901 if (acl->len > buflen)
3902 goto out;
3903 memcpy(buf, acl->data, acl->len);
3904out_len:
3905 ret = acl->len;
3906out:
3907 spin_unlock(&inode->i_lock);
3908 return ret;
3909}
3910
Sachin Prabhu5794d212012-04-17 14:36:40 +01003911static 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 +00003912{
3913 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003914 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003915
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003916 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003917 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003918 if (acl == NULL)
3919 goto out;
3920 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003921 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003922 } else {
3923 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3924 if (acl == NULL)
3925 goto out;
3926 acl->cached = 0;
3927 }
3928 acl->len = acl_len;
3929out:
3930 nfs4_set_cached_acl(inode, acl);
3931}
3932
Andy Adamsonbf118a32011-12-07 11:55:27 -05003933/*
3934 * The getxattr API returns the required buffer length when called with a
3935 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3936 * the required buf. On a NULL buf, we send a page of data to the server
3937 * guessing that the ACL request can be serviced by a page. If so, we cache
3938 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3939 * the cache. If not so, we throw away the page, and cache the required
3940 * length. The next getxattr call will then produce another round trip to
3941 * the server, this time with the input buf of the required size.
3942 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003943static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003944{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003945 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003946 struct nfs_getaclargs args = {
3947 .fh = NFS_FH(inode),
3948 .acl_pages = pages,
3949 .acl_len = buflen,
3950 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003951 struct nfs_getaclres res = {
3952 .acl_len = buflen,
3953 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3956 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003957 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003958 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003959 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3960 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003961
Andy Adamsonbf118a32011-12-07 11:55:27 -05003962 /* As long as we're doing a round trip to the server anyway,
3963 * let's be prepared for a page of acl data. */
3964 if (npages == 0)
3965 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003966 if (npages > ARRAY_SIZE(pages))
3967 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003968
Andy Adamsonbf118a32011-12-07 11:55:27 -05003969 for (i = 0; i < npages; i++) {
3970 pages[i] = alloc_page(GFP_KERNEL);
3971 if (!pages[i])
3972 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003974
3975 /* for decoding across pages */
3976 res.acl_scratch = alloc_page(GFP_KERNEL);
3977 if (!res.acl_scratch)
3978 goto out_free;
3979
Andy Adamsonbf118a32011-12-07 11:55:27 -05003980 args.acl_len = npages * PAGE_SIZE;
3981 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003982
Peng Taode040be2012-01-10 22:42:47 +08003983 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003984 __func__, buf, buflen, npages, args.acl_len);
3985 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3986 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003987 if (ret)
3988 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003989
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003990 /* Handle the case where the passed-in buffer is too short */
3991 if (res.acl_flags & NFS4_ACL_TRUNC) {
3992 /* Did the user only issue a request for the acl length? */
3993 if (buf == NULL)
3994 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003995 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003996 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003997 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003998 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003999 if (buf) {
4000 if (res.acl_len > buflen) {
4001 ret = -ERANGE;
4002 goto out_free;
4003 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004004 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004005 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004006out_ok:
4007 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004008out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004009 for (i = 0; i < npages; i++)
4010 if (pages[i])
4011 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004012 if (res.acl_scratch)
4013 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004014 return ret;
4015}
4016
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004017static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4018{
4019 struct nfs4_exception exception = { };
4020 ssize_t ret;
4021 do {
4022 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4023 if (ret >= 0)
4024 break;
4025 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4026 } while (exception.retry);
4027 return ret;
4028}
4029
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004030static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4031{
4032 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004033 int ret;
4034
4035 if (!nfs4_server_supports_acls(server))
4036 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004037 ret = nfs_revalidate_inode(server, inode);
4038 if (ret < 0)
4039 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004040 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4041 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004042 ret = nfs4_read_cached_acl(inode, buf, buflen);
4043 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004044 /* -ENOENT is returned if there is no ACL or if there is an ACL
4045 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004046 return ret;
4047 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004048}
4049
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004050static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004051{
4052 struct nfs_server *server = NFS_SERVER(inode);
4053 struct page *pages[NFS4ACL_MAXPAGES];
4054 struct nfs_setaclargs arg = {
4055 .fh = NFS_FH(inode),
4056 .acl_pages = pages,
4057 .acl_len = buflen,
4058 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004059 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004060 struct rpc_message msg = {
4061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4062 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004063 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004064 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004065 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004066 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004067
4068 if (!nfs4_server_supports_acls(server))
4069 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004070 if (npages > ARRAY_SIZE(pages))
4071 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004072 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4073 if (i < 0)
4074 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004075 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004076 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004077
4078 /*
4079 * Free each page after tx, so the only ref left is
4080 * held by the network stack
4081 */
4082 for (; i > 0; i--)
4083 put_page(pages[i-1]);
4084
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004085 /*
4086 * Acl update can result in inode attribute update.
4087 * so mark the attribute cache invalid.
4088 */
4089 spin_lock(&inode->i_lock);
4090 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4091 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004092 nfs_access_zap_cache(inode);
4093 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004094 return ret;
4095}
4096
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004097static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4098{
4099 struct nfs4_exception exception = { };
4100 int err;
4101 do {
4102 err = nfs4_handle_exception(NFS_SERVER(inode),
4103 __nfs4_proc_set_acl(inode, buf, buflen),
4104 &exception);
4105 } while (exception.retry);
4106 return err;
4107}
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004110nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004112 struct nfs_client *clp = server->nfs_client;
4113
4114 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 return 0;
4116 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004117 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004118 case -NFS4ERR_ADMIN_REVOKED:
4119 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004120 if (state == NULL)
4121 break;
4122 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004123 case -NFS4ERR_OPENMODE:
4124 if (state == NULL)
4125 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004126 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4127 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004128 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004129 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004130 if (state != NULL) {
4131 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4132 goto stateid_invalid;
4133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004135 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004136 nfs4_schedule_lease_recovery(clp);
4137 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004138#if defined(CONFIG_NFS_V4_1)
4139 case -NFS4ERR_BADSESSION:
4140 case -NFS4ERR_BADSLOT:
4141 case -NFS4ERR_BAD_HIGH_SLOT:
4142 case -NFS4ERR_DEADSESSION:
4143 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4144 case -NFS4ERR_SEQ_FALSE_RETRY:
4145 case -NFS4ERR_SEQ_MISORDERED:
4146 dprintk("%s ERROR %d, Reset session\n", __func__,
4147 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004148 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004149 task->tk_status = 0;
4150 return -EAGAIN;
4151#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004153 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004154 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4156 task->tk_status = 0;
4157 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004158 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case -NFS4ERR_OLD_STATEID:
4160 task->tk_status = 0;
4161 return -EAGAIN;
4162 }
4163 task->tk_status = nfs4_map_errors(task->tk_status);
4164 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004165stateid_invalid:
4166 task->tk_status = -EIO;
4167 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004168wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004169 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004170 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4171 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4172 task->tk_status = 0;
4173 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
Chuck Leverf0920752012-05-21 22:45:41 -04004176static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4177 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004178{
4179 __be32 verf[2];
4180
Chuck Lever2c820d92012-05-21 22:45:33 -04004181 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4182 /* An impossible timestamp guarantees this value
4183 * will never match a generated boot time. */
4184 verf[0] = 0;
4185 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4186 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004187 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4188 verf[0] = (__be32)nn->boot_time.tv_sec;
4189 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004190 }
Chuck Levercd937102012-03-02 17:14:31 -05004191 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4192}
4193
Chuck Levere984a552012-09-14 17:24:21 -04004194static unsigned int
4195nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4196 char *buf, size_t len)
4197{
4198 unsigned int result;
4199
4200 rcu_read_lock();
4201 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4202 clp->cl_ipaddr,
4203 rpc_peeraddr2str(clp->cl_rpcclient,
4204 RPC_DISPLAY_ADDR),
4205 rpc_peeraddr2str(clp->cl_rpcclient,
4206 RPC_DISPLAY_PROTO));
4207 rcu_read_unlock();
4208 return result;
4209}
4210
4211static unsigned int
4212nfs4_init_uniform_client_string(const struct nfs_client *clp,
4213 char *buf, size_t len)
4214{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004215 char *nodename = clp->cl_rpcclient->cl_nodename;
4216
4217 if (nfs4_client_id_uniquifier[0] != '\0')
4218 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004219 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4220 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004221 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004222}
4223
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004224/**
4225 * nfs4_proc_setclientid - Negotiate client ID
4226 * @clp: state data structure
4227 * @program: RPC program for NFSv4 callback service
4228 * @port: IP port number for NFS4 callback service
4229 * @cred: RPC credential to use for this call
4230 * @res: where to place the result
4231 *
4232 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4233 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004234int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4235 unsigned short port, struct rpc_cred *cred,
4236 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 nfs4_verifier sc_verifier;
4239 struct nfs4_setclientid setclientid = {
4240 .sc_verifier = &sc_verifier,
4241 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004242 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 };
4244 struct rpc_message msg = {
4245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4246 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004247 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004248 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004250 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Chuck Leverde734832012-07-11 16:30:50 -04004252 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004253 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004254 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4255 setclientid.sc_name_len =
4256 nfs4_init_uniform_client_string(clp,
4257 setclientid.sc_name,
4258 sizeof(setclientid.sc_name));
4259 else
4260 setclientid.sc_name_len =
4261 nfs4_init_nonuniform_client_string(clp,
4262 setclientid.sc_name,
4263 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004264 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004265 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004266 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004267 sizeof(setclientid.sc_netid),
4268 rpc_peeraddr2str(clp->cl_rpcclient,
4269 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004270 rcu_read_unlock();
4271 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004272 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 clp->cl_ipaddr, port >> 8, port & 255);
4274
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004275 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4276 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4277 setclientid.sc_name_len, setclientid.sc_name);
4278 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4279 dprintk("NFS reply setclientid: %d\n", status);
4280 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004283/**
4284 * nfs4_proc_setclientid_confirm - Confirm client ID
4285 * @clp: state data structure
4286 * @res: result of a previous SETCLIENTID
4287 * @cred: RPC credential to use for this call
4288 *
4289 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4290 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004291int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004292 struct nfs4_setclientid_res *arg,
4293 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295 struct nfs_fsinfo fsinfo;
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004298 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004300 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 };
4302 unsigned long now;
4303 int status;
4304
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004305 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4306 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4307 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004309 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 if (status == 0) {
4311 spin_lock(&clp->cl_lock);
4312 clp->cl_lease_time = fsinfo.lease_time * HZ;
4313 clp->cl_last_renewal = now;
4314 spin_unlock(&clp->cl_lock);
4315 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004316 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return status;
4318}
4319
Trond Myklebustfe650402006-01-03 09:55:18 +01004320struct nfs4_delegreturndata {
4321 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004322 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004323 struct nfs_fh fh;
4324 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004325 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004326 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004327 int rpc_status;
4328};
4329
Trond Myklebustfe650402006-01-03 09:55:18 +01004330static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4331{
4332 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004333
Trond Myklebust14516c32010-07-31 14:29:06 -04004334 if (!nfs4_sequence_done(task, &data->res.seq_res))
4335 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004336
Ricardo Labiaga79708862009-12-07 09:23:21 -05004337 switch (task->tk_status) {
4338 case -NFS4ERR_STALE_STATEID:
4339 case -NFS4ERR_EXPIRED:
4340 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004341 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004342 break;
4343 default:
4344 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4345 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004346 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004347 return;
4348 }
4349 }
4350 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004351}
4352
4353static void nfs4_delegreturn_release(void *calldata)
4354{
Trond Myklebustfe650402006-01-03 09:55:18 +01004355 kfree(calldata);
4356}
4357
Andy Adamson938e1012009-04-01 09:22:28 -04004358#if defined(CONFIG_NFS_V4_1)
4359static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4360{
4361 struct nfs4_delegreturndata *d_data;
4362
4363 d_data = (struct nfs4_delegreturndata *)data;
4364
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004365 nfs4_setup_sequence(d_data->res.server,
4366 &d_data->args.seq_args,
4367 &d_data->res.seq_res,
4368 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004369}
4370#endif /* CONFIG_NFS_V4_1 */
4371
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004372static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004373#if defined(CONFIG_NFS_V4_1)
4374 .rpc_call_prepare = nfs4_delegreturn_prepare,
4375#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004376 .rpc_call_done = nfs4_delegreturn_done,
4377 .rpc_release = nfs4_delegreturn_release,
4378};
4379
Trond Myklebuste6f81072008-01-24 18:14:34 -05004380static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004381{
4382 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004383 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004384 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004385 struct rpc_message msg = {
4386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4387 .rpc_cred = cred,
4388 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004389 struct rpc_task_setup task_setup_data = {
4390 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004391 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004392 .callback_ops = &nfs4_delegreturn_ops,
4393 .flags = RPC_TASK_ASYNC,
4394 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004395 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004396
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004397 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004398 if (data == NULL)
4399 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004400 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004401 data->args.fhandle = &data->fh;
4402 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004403 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004404 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004405 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004406 data->res.fattr = &data->fattr;
4407 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004408 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004409 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004410 data->rpc_status = 0;
4411
Trond Myklebustc970aa82007-07-14 15:39:59 -04004412 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004413 msg.rpc_argp = &data->args;
4414 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004415 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004416 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004417 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004418 if (!issync)
4419 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004420 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004421 if (status != 0)
4422 goto out;
4423 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004424 if (status == 0)
4425 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4426 else
4427 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004428out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004429 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004430 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
Trond Myklebuste6f81072008-01-24 18:14:34 -05004433int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
4435 struct nfs_server *server = NFS_SERVER(inode);
4436 struct nfs4_exception exception = { };
4437 int err;
4438 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004439 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 switch (err) {
4441 case -NFS4ERR_STALE_STATEID:
4442 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 case 0:
4444 return 0;
4445 }
4446 err = nfs4_handle_exception(server, err, &exception);
4447 } while (exception.retry);
4448 return err;
4449}
4450
4451#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4452#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4453
4454/*
4455 * sleep, with exponential backoff, and retry the LOCK operation.
4456 */
4457static unsigned long
4458nfs4_set_lock_task_retry(unsigned long timeout)
4459{
Jeff Laytond3103102011-12-01 22:44:39 +01004460 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 timeout <<= 1;
4462 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4463 return NFS4_LOCK_MAXTIMEOUT;
4464 return timeout;
4465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4468{
4469 struct inode *inode = state->inode;
4470 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004471 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004472 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004476 struct nfs_lockt_res res = {
4477 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 };
4479 struct rpc_message msg = {
4480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4481 .rpc_argp = &arg,
4482 .rpc_resp = &res,
4483 .rpc_cred = state->owner->so_cred,
4484 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct nfs4_lock_state *lsp;
4486 int status;
4487
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004488 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004489 status = nfs4_set_lock_state(state, request);
4490 if (status != 0)
4491 goto out;
4492 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004493 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004494 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004495 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004496 switch (status) {
4497 case 0:
4498 request->fl_type = F_UNLCK;
4499 break;
4500 case -NFS4ERR_DENIED:
4501 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004503 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004504out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return status;
4506}
4507
4508static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4509{
4510 struct nfs4_exception exception = { };
4511 int err;
4512
4513 do {
4514 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4515 _nfs4_proc_getlk(state, cmd, request),
4516 &exception);
4517 } while (exception.retry);
4518 return err;
4519}
4520
4521static int do_vfs_lock(struct file *file, struct file_lock *fl)
4522{
4523 int res = 0;
4524 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4525 case FL_POSIX:
4526 res = posix_lock_file_wait(file, fl);
4527 break;
4528 case FL_FLOCK:
4529 res = flock_lock_file_wait(file, fl);
4530 break;
4531 default:
4532 BUG();
4533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return res;
4535}
4536
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004537struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004538 struct nfs_locku_args arg;
4539 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004540 struct nfs4_lock_state *lsp;
4541 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004542 struct file_lock fl;
4543 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004544 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004545};
4546
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004547static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4548 struct nfs_open_context *ctx,
4549 struct nfs4_lock_state *lsp,
4550 struct nfs_seqid *seqid)
4551{
4552 struct nfs4_unlockdata *p;
4553 struct inode *inode = lsp->ls_state->inode;
4554
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004555 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004556 if (p == NULL)
4557 return NULL;
4558 p->arg.fh = NFS_FH(inode);
4559 p->arg.fl = &p->fl;
4560 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004561 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004562 p->arg.stateid = &lsp->ls_stateid;
4563 p->lsp = lsp;
4564 atomic_inc(&lsp->ls_count);
4565 /* Ensure we don't close file until we're done freeing locks! */
4566 p->ctx = get_nfs_open_context(ctx);
4567 memcpy(&p->fl, fl, sizeof(p->fl));
4568 p->server = NFS_SERVER(inode);
4569 return p;
4570}
4571
Trond Myklebust06f814a2006-01-03 09:55:07 +01004572static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004573{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004574 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004575 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004576 nfs4_put_lock_state(calldata->lsp);
4577 put_nfs_open_context(calldata->ctx);
4578 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004579}
4580
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004581static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004582{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004583 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004584
Trond Myklebust14516c32010-07-31 14:29:06 -04004585 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4586 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004587 switch (task->tk_status) {
4588 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004589 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4590 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004591 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004592 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004593 case -NFS4ERR_BAD_STATEID:
4594 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004595 case -NFS4ERR_STALE_STATEID:
4596 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004597 break;
4598 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004599 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004600 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004602 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004603}
4604
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004605static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004606{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004607 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004609 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004610 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004611 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004612 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004613 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004614 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004615 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004616 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004617 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004618 &calldata->res.seq_res,
4619 task) != 0)
4620 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004621 return;
4622out_no_action:
4623 task->tk_action = NULL;
4624out_wait:
4625 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004628static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004629 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004630 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004631 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004632};
4633
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004634static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4635 struct nfs_open_context *ctx,
4636 struct nfs4_lock_state *lsp,
4637 struct nfs_seqid *seqid)
4638{
4639 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004640 struct rpc_message msg = {
4641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4642 .rpc_cred = ctx->cred,
4643 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004644 struct rpc_task_setup task_setup_data = {
4645 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004646 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004647 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004648 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004649 .flags = RPC_TASK_ASYNC,
4650 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651
Frank Filz137d6ac2007-07-09 15:32:29 -07004652 /* Ensure this is an unlock - when canceling a lock, the
4653 * canceled lock is passed in, and it won't be an unlock.
4654 */
4655 fl->fl_type = F_UNLCK;
4656
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004657 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4658 if (data == NULL) {
4659 nfs_free_seqid(seqid);
4660 return ERR_PTR(-ENOMEM);
4661 }
4662
Trond Myklebust9d12b212012-01-17 22:04:25 -05004663 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004664 msg.rpc_argp = &data->arg;
4665 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004666 task_setup_data.callback_data = data;
4667 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668}
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4671{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004672 struct inode *inode = state->inode;
4673 struct nfs4_state_owner *sp = state->owner;
4674 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004675 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004676 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004677 struct rpc_task *task;
4678 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004679 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Trond Myklebust9b073572006-06-29 16:38:34 -04004681 status = nfs4_set_lock_state(state, request);
4682 /* Unlock _before_ we do the RPC call */
4683 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004684 /* Exclude nfs_delegation_claim_locks() */
4685 mutex_lock(&sp->so_delegreturn_mutex);
4686 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004687 down_read(&nfsi->rwsem);
4688 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4689 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004690 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004691 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004692 }
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 if (status != 0)
4696 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004697 /* Is this a delegated lock? */
4698 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004699 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004700 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004701 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004702 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004703 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004704 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004705 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004706 status = PTR_ERR(task);
4707 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004708 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004709 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004710 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004711out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004712 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004713 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004716struct nfs4_lockdata {
4717 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004718 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004719 struct nfs4_lock_state *lsp;
4720 struct nfs_open_context *ctx;
4721 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004722 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 int rpc_status;
4724 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004725 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726};
4727
4728static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004729 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4730 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004731{
4732 struct nfs4_lockdata *p;
4733 struct inode *inode = lsp->ls_state->inode;
4734 struct nfs_server *server = NFS_SERVER(inode);
4735
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004736 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004737 if (p == NULL)
4738 return NULL;
4739
4740 p->arg.fh = NFS_FH(inode);
4741 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004742 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004743 if (p->arg.open_seqid == NULL)
4744 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004745 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004747 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004749 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004750 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004751 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004752 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004754 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004755 atomic_inc(&lsp->ls_count);
4756 p->ctx = get_nfs_open_context(ctx);
4757 memcpy(&p->fl, fl, sizeof(p->fl));
4758 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004759out_free_seqid:
4760 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761out_free:
4762 kfree(p);
4763 return NULL;
4764}
4765
4766static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4767{
4768 struct nfs4_lockdata *data = calldata;
4769 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Harvey Harrison3110ff82008-05-02 13:42:44 -07004771 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004772 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004773 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004774 /* Do we need to do an open_to_lock_owner? */
4775 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004776 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004777 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004778 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004779 data->arg.open_stateid = &state->stateid;
4780 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004781 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004782 } else
4783 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004784 if (!nfs4_valid_open_stateid(state)) {
4785 data->rpc_status = -EBADF;
4786 task->tk_action = NULL;
4787 goto out_release_open_seqid;
4788 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004789 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004790 if (nfs4_setup_sequence(data->server,
4791 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004792 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004793 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004794 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004795out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004796 nfs_release_seqid(data->arg.open_seqid);
4797out_release_lock_seqid:
4798 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004799out_wait:
4800 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004801 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004802}
4803
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004804static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4805{
4806 struct nfs4_lockdata *data = calldata;
4807
Harvey Harrison3110ff82008-05-02 13:42:44 -07004808 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004809
Trond Myklebust14516c32010-07-31 14:29:06 -04004810 if (!nfs4_sequence_done(task, &data->res.seq_res))
4811 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004812
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004813 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004814 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004815 if (data->rpc_status == 0)
4816 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4817 else
4818 goto out;
4819 }
4820 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004821 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004822 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004823 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004824 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004826 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004827}
4828
4829static void nfs4_lock_release(void *calldata)
4830{
4831 struct nfs4_lockdata *data = calldata;
4832
Harvey Harrison3110ff82008-05-02 13:42:44 -07004833 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004834 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004835 if (data->cancelled != 0) {
4836 struct rpc_task *task;
4837 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4838 data->arg.lock_seqid);
4839 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004840 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004841 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842 } else
4843 nfs_free_seqid(data->arg.lock_seqid);
4844 nfs4_put_lock_state(data->lsp);
4845 put_nfs_open_context(data->ctx);
4846 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004847 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004848}
4849
4850static const struct rpc_call_ops nfs4_lock_ops = {
4851 .rpc_call_prepare = nfs4_lock_prepare,
4852 .rpc_call_done = nfs4_lock_done,
4853 .rpc_release = nfs4_lock_release,
4854};
4855
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004856static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4857{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004858 switch (error) {
4859 case -NFS4ERR_ADMIN_REVOKED:
4860 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004861 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004862 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004863 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004864 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004865 break;
4866 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004867 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004868 case -NFS4ERR_EXPIRED:
4869 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004870 };
4871}
4872
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004873static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004874{
4875 struct nfs4_lockdata *data;
4876 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004877 struct rpc_message msg = {
4878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4879 .rpc_cred = state->owner->so_cred,
4880 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004881 struct rpc_task_setup task_setup_data = {
4882 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004883 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004884 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004885 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004886 .flags = RPC_TASK_ASYNC,
4887 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004888 int ret;
4889
Harvey Harrison3110ff82008-05-02 13:42:44 -07004890 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004891 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004892 fl->fl_u.nfs4_fl.owner,
4893 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004894 if (data == NULL)
4895 return -ENOMEM;
4896 if (IS_SETLKW(cmd))
4897 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004898 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004899 msg.rpc_argp = &data->arg;
4900 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004901 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004902 if (recovery_type > NFS_LOCK_NEW) {
4903 if (recovery_type == NFS_LOCK_RECLAIM)
4904 data->arg.reclaim = NFS_LOCK_RECLAIM;
4905 nfs4_set_sequence_privileged(&data->arg.seq_args);
4906 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004907 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004908 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004909 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004910 ret = nfs4_wait_for_completion_rpc_task(task);
4911 if (ret == 0) {
4912 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004913 if (ret)
4914 nfs4_handle_setlk_error(data->server, data->lsp,
4915 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004916 } else
4917 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004918 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004919 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
4923static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4924{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004925 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004926 struct nfs4_exception exception = {
4927 .inode = state->inode,
4928 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004929 int err;
4930
4931 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004932 /* Cache the lock if possible... */
4933 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4934 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004935 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004936 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004937 break;
4938 nfs4_handle_exception(server, err, &exception);
4939 } while (exception.retry);
4940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
4943static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4944{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004945 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004946 struct nfs4_exception exception = {
4947 .inode = state->inode,
4948 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004949 int err;
4950
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004951 err = nfs4_set_lock_state(state, request);
4952 if (err != 0)
4953 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004954 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4956 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004957 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004958 switch (err) {
4959 default:
4960 goto out;
4961 case -NFS4ERR_GRACE:
4962 case -NFS4ERR_DELAY:
4963 nfs4_handle_exception(server, err, &exception);
4964 err = 0;
4965 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004966 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004967out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969}
4970
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004971#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004972/**
4973 * nfs41_check_expired_locks - possibly free a lock stateid
4974 *
4975 * @state: NFSv4 state for an inode
4976 *
4977 * Returns NFS_OK if recovery for this stateid is now finished.
4978 * Otherwise a negative NFS4ERR value is returned.
4979 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004980static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004981{
Chuck Levereb64cf92012-07-11 16:30:05 -04004982 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004983 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004984 struct nfs_server *server = NFS_SERVER(state->inode);
4985
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004986 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004987 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004988 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4989 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004990 /* Free the stateid unless the server
4991 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004992 if (status != -NFS4ERR_BAD_STATEID)
4993 nfs41_free_stateid(server,
4994 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004995 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004996 ret = status;
4997 }
4998 }
4999 };
5000
5001 return ret;
5002}
5003
5004static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5005{
5006 int status = NFS_OK;
5007
5008 if (test_bit(LK_STATE_IN_USE, &state->flags))
5009 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005010 if (status != NFS_OK)
5011 status = nfs4_lock_expired(state, request);
5012 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005013}
5014#endif
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5017{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005018 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005019 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005020 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005021 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005022 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005024 if ((fl_flags & FL_POSIX) &&
5025 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5026 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005027 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005028 status = nfs4_set_lock_state(state, request);
5029 if (status != 0)
5030 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005031 request->fl_flags |= FL_ACCESS;
5032 status = do_vfs_lock(request->fl_file, request);
5033 if (status < 0)
5034 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005035 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005036 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005037 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005038 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005039 request->fl_flags = fl_flags & ~FL_SLEEP;
5040 status = do_vfs_lock(request->fl_file, request);
5041 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005042 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005043 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5044 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005045 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005046 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005047 goto out;
5048 down_read(&nfsi->rwsem);
5049 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5050 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005051 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005052 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005053 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005054 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005055 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005056 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5057 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005059 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005060out:
5061 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return status;
5063}
5064
5065static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5066{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005067 struct nfs4_exception exception = {
5068 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005069 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 int err;
5072
5073 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005074 err = _nfs4_proc_setlk(state, cmd, request);
5075 if (err == -NFS4ERR_DENIED)
5076 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005078 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 } while (exception.retry);
5080 return err;
5081}
5082
5083static int
5084nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5085{
5086 struct nfs_open_context *ctx;
5087 struct nfs4_state *state;
5088 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5089 int status;
5090
5091 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005092 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 state = ctx->state;
5094
5095 if (request->fl_start < 0 || request->fl_end < 0)
5096 return -EINVAL;
5097
Trond Myklebustd9531262009-07-21 19:22:38 -04005098 if (IS_GETLK(cmd)) {
5099 if (state != NULL)
5100 return nfs4_proc_getlk(state, F_GETLK, request);
5101 return 0;
5102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5105 return -EINVAL;
5106
Trond Myklebustd9531262009-07-21 19:22:38 -04005107 if (request->fl_type == F_UNLCK) {
5108 if (state != NULL)
5109 return nfs4_proc_unlck(state, cmd, request);
5110 return 0;
5111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Trond Myklebustd9531262009-07-21 19:22:38 -04005113 if (state == NULL)
5114 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005115 /*
5116 * Don't rely on the VFS having checked the file open mode,
5117 * since it won't do this for flock() locks.
5118 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005119 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005120 case F_RDLCK:
5121 if (!(filp->f_mode & FMODE_READ))
5122 return -EBADF;
5123 break;
5124 case F_WRLCK:
5125 if (!(filp->f_mode & FMODE_WRITE))
5126 return -EBADF;
5127 }
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 do {
5130 status = nfs4_proc_setlk(state, cmd, request);
5131 if ((status != -EAGAIN) || IS_SETLK(cmd))
5132 break;
5133 timeout = nfs4_set_lock_task_retry(timeout);
5134 status = -ERESTARTSYS;
5135 if (signalled())
5136 break;
5137 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 return status;
5139}
5140
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005141int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005142{
5143 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005144 int err;
5145
5146 err = nfs4_set_lock_state(state, fl);
5147 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005148 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005149 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005150 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005151}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005152
Trond Myklebustcf470c32012-03-07 13:49:12 -05005153struct nfs_release_lockowner_data {
5154 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005155 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005156 struct nfs_release_lockowner_args args;
5157};
5158
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005159static void nfs4_release_lockowner_release(void *calldata)
5160{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005161 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005162 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005163 kfree(calldata);
5164}
5165
Trond Myklebust17280172012-03-11 13:11:00 -04005166static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005167 .rpc_release = nfs4_release_lockowner_release,
5168};
5169
Trond Myklebustcf470c32012-03-07 13:49:12 -05005170int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005171{
5172 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005173 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005174 struct rpc_message msg = {
5175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5176 };
5177
5178 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005179 return -EINVAL;
5180 data = kmalloc(sizeof(*data), GFP_NOFS);
5181 if (!data)
5182 return -ENOMEM;
5183 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005184 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005185 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5186 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5187 data->args.lock_owner.s_dev = server->s_dev;
5188 msg.rpc_argp = &data->args;
5189 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5190 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005191}
5192
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005193#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5194
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005195static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5196 const void *buf, size_t buflen,
5197 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005198{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005199 if (strcmp(key, "") != 0)
5200 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005201
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005202 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005203}
5204
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005205static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5206 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005207{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005208 if (strcmp(key, "") != 0)
5209 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005210
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005211 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005212}
5213
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005214static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5215 size_t list_len, const char *name,
5216 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005217{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005218 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005219
J. Bruce Fields096455a2006-03-20 23:23:42 -05005220 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5221 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005222
5223 if (list && len <= list_len)
5224 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005225 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005226}
5227
Andy Adamson533eb462011-06-13 18:25:56 -04005228/*
5229 * nfs_fhget will use either the mounted_on_fileid or the fileid
5230 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005231static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5232{
Andy Adamson533eb462011-06-13 18:25:56 -04005233 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5234 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5235 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005236 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005237 return;
5238
5239 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005240 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005241 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5242 fattr->nlink = 2;
5243}
5244
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005245static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5246 const struct qstr *name,
5247 struct nfs4_fs_locations *fs_locations,
5248 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005249{
5250 struct nfs_server *server = NFS_SERVER(dir);
5251 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005252 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005253 };
5254 struct nfs4_fs_locations_arg args = {
5255 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005256 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005257 .page = page,
5258 .bitmask = bitmask,
5259 };
Benny Halevy22958462009-04-01 09:22:02 -04005260 struct nfs4_fs_locations_res res = {
5261 .fs_locations = fs_locations,
5262 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5265 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005266 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005267 };
5268 int status;
5269
Harvey Harrison3110ff82008-05-02 13:42:44 -07005270 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005271
5272 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5273 * is not supported */
5274 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5275 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5276 else
5277 bitmask[0] |= FATTR4_WORD0_FILEID;
5278
Trond Myklebustc228fd32007-01-13 02:28:11 -05005279 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005280 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005281 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005282 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005283 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005284 return status;
5285}
5286
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005287int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5288 const struct qstr *name,
5289 struct nfs4_fs_locations *fs_locations,
5290 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005291{
5292 struct nfs4_exception exception = { };
5293 int err;
5294 do {
5295 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005296 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005297 &exception);
5298 } while (exception.retry);
5299 return err;
5300}
5301
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005302static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5303{
5304 int status;
5305 struct nfs4_secinfo_arg args = {
5306 .dir_fh = NFS_FH(dir),
5307 .name = name,
5308 };
5309 struct nfs4_secinfo_res res = {
5310 .flavors = flavors,
5311 };
5312 struct rpc_message msg = {
5313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5314 .rpc_argp = &args,
5315 .rpc_resp = &res,
5316 };
5317
5318 dprintk("NFS call secinfo %s\n", name->name);
5319 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5320 dprintk("NFS reply secinfo: %d\n", status);
5321 return status;
5322}
5323
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005324int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5325 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005326{
5327 struct nfs4_exception exception = { };
5328 int err;
5329 do {
5330 err = nfs4_handle_exception(NFS_SERVER(dir),
5331 _nfs4_proc_secinfo(dir, name, flavors),
5332 &exception);
5333 } while (exception.retry);
5334 return err;
5335}
5336
Andy Adamson557134a2009-04-01 09:21:53 -04005337#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005338/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005339 * Check the exchange flags returned by the server for invalid flags, having
5340 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5341 * DS flags set.
5342 */
5343static int nfs4_check_cl_exchange_flags(u32 flags)
5344{
5345 if (flags & ~EXCHGID4_FLAG_MASK_R)
5346 goto out_inval;
5347 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5348 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5349 goto out_inval;
5350 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5351 goto out_inval;
5352 return NFS_OK;
5353out_inval:
5354 return -NFS4ERR_INVAL;
5355}
5356
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005357static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005358nfs41_same_server_scope(struct nfs41_server_scope *a,
5359 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005360{
5361 if (a->server_scope_sz == b->server_scope_sz &&
5362 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5363 return true;
5364
5365 return false;
5366}
5367
Andy Adamson357f54d2010-12-14 10:11:57 -05005368/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005369 * nfs4_proc_bind_conn_to_session()
5370 *
5371 * The 4.1 client currently uses the same TCP connection for the
5372 * fore and backchannel.
5373 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005374int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005375{
5376 int status;
5377 struct nfs41_bind_conn_to_session_res res;
5378 struct rpc_message msg = {
5379 .rpc_proc =
5380 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5381 .rpc_argp = clp,
5382 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005383 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005384 };
5385
5386 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005387
5388 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5389 if (unlikely(res.session == NULL)) {
5390 status = -ENOMEM;
5391 goto out;
5392 }
5393
5394 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5395 if (status == 0) {
5396 if (memcmp(res.session->sess_id.data,
5397 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5398 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5399 status = -EIO;
5400 goto out_session;
5401 }
5402 if (res.dir != NFS4_CDFS4_BOTH) {
5403 dprintk("NFS: %s: Unexpected direction from server\n",
5404 __func__);
5405 status = -EIO;
5406 goto out_session;
5407 }
5408 if (res.use_conn_in_rdma_mode) {
5409 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5410 __func__);
5411 status = -EIO;
5412 goto out_session;
5413 }
5414 }
5415out_session:
5416 kfree(res.session);
5417out:
5418 dprintk("<-- %s status= %d\n", __func__, status);
5419 return status;
5420}
5421
5422/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005423 * nfs4_proc_exchange_id()
5424 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005425 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5426 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005427 * Since the clientid has expired, all compounds using sessions
5428 * associated with the stale clientid will be returning
5429 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5430 * be in some phase of session reset.
5431 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005432int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005433{
5434 nfs4_verifier verifier;
5435 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005436 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005437 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005438 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005439 };
5440 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005441 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005442 };
5443 int status;
5444 struct rpc_message msg = {
5445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5446 .rpc_argp = &args,
5447 .rpc_resp = &res,
5448 .rpc_cred = cred,
5449 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005450
Chuck Leverf0920752012-05-21 22:45:41 -04005451 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005452 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5453 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005454 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5455 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5456 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005457
Chuck Leveracdeb692012-05-21 22:46:16 -04005458 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005459 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005460 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005461 status = -ENOMEM;
5462 goto out;
5463 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005464
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005465 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005466 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005467 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005468 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005469 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005470 }
5471
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005472 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005473 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005474 status = -ENOMEM;
5475 goto out_server_scope;
5476 }
5477
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005478 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005479 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005480 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005481
Chuck Lever177313f2012-05-21 22:44:58 -04005482 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005483 clp->cl_clientid = res.clientid;
5484 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5485 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5486 clp->cl_seqid = res.seqid;
5487
Chuck Leveracdeb692012-05-21 22:46:16 -04005488 kfree(clp->cl_serverowner);
5489 clp->cl_serverowner = res.server_owner;
5490 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005491
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005492 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005493 kfree(clp->cl_implid);
5494 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005495
Chuck Lever177313f2012-05-21 22:44:58 -04005496 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005497 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005498 res.server_scope)) {
5499 dprintk("%s: server_scope mismatch detected\n",
5500 __func__);
5501 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005502 kfree(clp->cl_serverscope);
5503 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005504 }
5505
Chuck Lever177313f2012-05-21 22:44:58 -04005506 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005507 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005508 goto out;
5509 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005510 } else
5511 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005512
Chuck Leveracdeb692012-05-21 22:46:16 -04005513out_server_owner:
5514 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005515out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005516 kfree(res.server_scope);
5517out:
Chuck Lever177313f2012-05-21 22:44:58 -04005518 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005519 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005520 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005521 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005522 clp->cl_implid->date.seconds,
5523 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005524 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005525 return status;
5526}
5527
Trond Myklebust66245532012-05-25 17:18:09 -04005528static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5529 struct rpc_cred *cred)
5530{
5531 struct rpc_message msg = {
5532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5533 .rpc_argp = clp,
5534 .rpc_cred = cred,
5535 };
5536 int status;
5537
5538 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5539 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005540 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005541 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5542 return status;
5543}
5544
5545static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5546 struct rpc_cred *cred)
5547{
5548 unsigned int loop;
5549 int ret;
5550
5551 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5552 ret = _nfs4_proc_destroy_clientid(clp, cred);
5553 switch (ret) {
5554 case -NFS4ERR_DELAY:
5555 case -NFS4ERR_CLIENTID_BUSY:
5556 ssleep(1);
5557 break;
5558 default:
5559 return ret;
5560 }
5561 }
5562 return 0;
5563}
5564
5565int nfs4_destroy_clientid(struct nfs_client *clp)
5566{
5567 struct rpc_cred *cred;
5568 int ret = 0;
5569
5570 if (clp->cl_mvops->minor_version < 1)
5571 goto out;
5572 if (clp->cl_exchange_flags == 0)
5573 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005574 if (clp->cl_preserve_clid)
5575 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005576 cred = nfs4_get_exchange_id_cred(clp);
5577 ret = nfs4_proc_destroy_clientid(clp, cred);
5578 if (cred)
5579 put_rpccred(cred);
5580 switch (ret) {
5581 case 0:
5582 case -NFS4ERR_STALE_CLIENTID:
5583 clp->cl_exchange_flags = 0;
5584 }
5585out:
5586 return ret;
5587}
5588
Andy Adamson2050f0c2009-04-01 09:22:30 -04005589struct nfs4_get_lease_time_data {
5590 struct nfs4_get_lease_time_args *args;
5591 struct nfs4_get_lease_time_res *res;
5592 struct nfs_client *clp;
5593};
5594
5595static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5596 void *calldata)
5597{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005598 struct nfs4_get_lease_time_data *data =
5599 (struct nfs4_get_lease_time_data *)calldata;
5600
5601 dprintk("--> %s\n", __func__);
5602 /* just setup sequence, do not trigger session recovery
5603 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005604 nfs41_setup_sequence(data->clp->cl_session,
5605 &data->args->la_seq_args,
5606 &data->res->lr_seq_res,
5607 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005608 dprintk("<-- %s\n", __func__);
5609}
5610
5611/*
5612 * Called from nfs4_state_manager thread for session setup, so don't recover
5613 * from sequence operation or clientid errors.
5614 */
5615static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5616{
5617 struct nfs4_get_lease_time_data *data =
5618 (struct nfs4_get_lease_time_data *)calldata;
5619
5620 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005621 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5622 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005623 switch (task->tk_status) {
5624 case -NFS4ERR_DELAY:
5625 case -NFS4ERR_GRACE:
5626 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5627 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5628 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005629 /* fall through */
5630 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005631 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005632 return;
5633 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005634 dprintk("<-- %s\n", __func__);
5635}
5636
Trond Myklebust17280172012-03-11 13:11:00 -04005637static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005638 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5639 .rpc_call_done = nfs4_get_lease_time_done,
5640};
5641
5642int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5643{
5644 struct rpc_task *task;
5645 struct nfs4_get_lease_time_args args;
5646 struct nfs4_get_lease_time_res res = {
5647 .lr_fsinfo = fsinfo,
5648 };
5649 struct nfs4_get_lease_time_data data = {
5650 .args = &args,
5651 .res = &res,
5652 .clp = clp,
5653 };
5654 struct rpc_message msg = {
5655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5656 .rpc_argp = &args,
5657 .rpc_resp = &res,
5658 };
5659 struct rpc_task_setup task_setup = {
5660 .rpc_client = clp->cl_rpcclient,
5661 .rpc_message = &msg,
5662 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005663 .callback_data = &data,
5664 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005665 };
5666 int status;
5667
Trond Myklebust9d12b212012-01-17 22:04:25 -05005668 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005669 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005670 dprintk("--> %s\n", __func__);
5671 task = rpc_run_task(&task_setup);
5672
5673 if (IS_ERR(task))
5674 status = PTR_ERR(task);
5675 else {
5676 status = task->tk_status;
5677 rpc_put_task(task);
5678 }
5679 dprintk("<-- %s return %d\n", __func__, status);
5680
5681 return status;
5682}
5683
Andy Adamsonfc931582009-04-01 09:22:31 -04005684/*
5685 * Initialize the values to be used by the client in CREATE_SESSION
5686 * If nfs4_init_session set the fore channel request and response sizes,
5687 * use them.
5688 *
5689 * Set the back channel max_resp_sz_cached to zero to force the client to
5690 * always set csa_cachethis to FALSE because the current implementation
5691 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5692 */
5693static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5694{
5695 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005696 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5697 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005698
5699 if (mxrqst_sz == 0)
5700 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5701 if (mxresp_sz == 0)
5702 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5703 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005704 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5705 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005706 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005707 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005708
5709 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005710 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005711 __func__,
5712 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005713 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005714
5715 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005716 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5717 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5718 args->bc_attrs.max_resp_sz_cached = 0;
5719 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5720 args->bc_attrs.max_reqs = 1;
5721
5722 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5723 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5724 __func__,
5725 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5726 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5727 args->bc_attrs.max_reqs);
5728}
5729
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005730static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005731{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005732 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5733 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5734
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005735 if (rcvd->max_resp_sz > sent->max_resp_sz)
5736 return -EINVAL;
5737 /*
5738 * Our requested max_ops is the minimum we need; we're not
5739 * prepared to break up compounds into smaller pieces than that.
5740 * So, no point even trying to continue if the server won't
5741 * cooperate:
5742 */
5743 if (rcvd->max_ops < sent->max_ops)
5744 return -EINVAL;
5745 if (rcvd->max_reqs == 0)
5746 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005747 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5748 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005749 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005750}
5751
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005752static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5753{
5754 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5755 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005756
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005757 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5758 return -EINVAL;
5759 if (rcvd->max_resp_sz < sent->max_resp_sz)
5760 return -EINVAL;
5761 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5762 return -EINVAL;
5763 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005764 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005765 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005766 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005767 return -EINVAL;
5768 return 0;
5769}
Andy Adamson8d353012009-04-01 09:22:32 -04005770
Andy Adamson8d353012009-04-01 09:22:32 -04005771static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5772 struct nfs4_session *session)
5773{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005775
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005776 ret = nfs4_verify_fore_channel_attrs(args, session);
5777 if (ret)
5778 return ret;
5779 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005780}
5781
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005782static int _nfs4_proc_create_session(struct nfs_client *clp,
5783 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005784{
5785 struct nfs4_session *session = clp->cl_session;
5786 struct nfs41_create_session_args args = {
5787 .client = clp,
5788 .cb_program = NFS4_CALLBACK,
5789 };
5790 struct nfs41_create_session_res res = {
5791 .client = clp,
5792 };
5793 struct rpc_message msg = {
5794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5795 .rpc_argp = &args,
5796 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005797 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005798 };
5799 int status;
5800
5801 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005802 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005803
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005804 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005805
Trond Myklebust43095d32012-11-20 11:13:12 -05005806 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005807 /* Verify the session's negotiated channel_attrs values */
5808 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005809 /* Increment the clientid slot sequence id */
5810 clp->cl_seqid++;
5811 }
5812
5813 return status;
5814}
5815
5816/*
5817 * Issues a CREATE_SESSION operation to the server.
5818 * It is the responsibility of the caller to verify the session is
5819 * expired before calling this routine.
5820 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005821int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005822{
5823 int status;
5824 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005825 struct nfs4_session *session = clp->cl_session;
5826
5827 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5828
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005829 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005830 if (status)
5831 goto out;
5832
Andy Adamsonaacd5532011-11-09 13:58:21 -05005833 /* Init or reset the session slot tables */
5834 status = nfs4_setup_session_slot_tables(session);
5835 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005836 if (status)
5837 goto out;
5838
5839 ptr = (unsigned *)&session->sess_id.data[0];
5840 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5841 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005842out:
5843 dprintk("<-- %s\n", __func__);
5844 return status;
5845}
5846
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005847/*
5848 * Issue the over-the-wire RPC DESTROY_SESSION.
5849 * The caller must serialize access to this routine.
5850 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005851int nfs4_proc_destroy_session(struct nfs4_session *session,
5852 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005853{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005854 struct rpc_message msg = {
5855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5856 .rpc_argp = session,
5857 .rpc_cred = cred,
5858 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005859 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005860
5861 dprintk("--> nfs4_proc_destroy_session\n");
5862
5863 /* session is still being setup */
5864 if (session->clp->cl_cons_state != NFS_CS_READY)
5865 return status;
5866
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005867 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005868
5869 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005870 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005871 "Session has been destroyed regardless...\n", status);
5872
5873 dprintk("<-- nfs4_proc_destroy_session\n");
5874 return status;
5875}
5876
Trond Myklebust7b38c362012-05-23 13:23:31 -04005877/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005878 * Renew the cl_session lease.
5879 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005880struct nfs4_sequence_data {
5881 struct nfs_client *clp;
5882 struct nfs4_sequence_args args;
5883 struct nfs4_sequence_res res;
5884};
5885
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005886static void nfs41_sequence_release(void *data)
5887{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005888 struct nfs4_sequence_data *calldata = data;
5889 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005890
Alexandros Batsakis71358402010-02-05 03:45:05 -08005891 if (atomic_read(&clp->cl_count) > 1)
5892 nfs4_schedule_state_renewal(clp);
5893 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005894 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005895}
5896
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005897static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5898{
5899 switch(task->tk_status) {
5900 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005901 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5902 return -EAGAIN;
5903 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005904 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005905 }
5906 return 0;
5907}
5908
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005909static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005910{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005911 struct nfs4_sequence_data *calldata = data;
5912 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005913
Trond Myklebust14516c32010-07-31 14:29:06 -04005914 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5915 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005916
5917 if (task->tk_status < 0) {
5918 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005919 if (atomic_read(&clp->cl_count) == 1)
5920 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005921
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005922 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5923 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005924 return;
5925 }
5926 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005927 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005928out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005929 dprintk("<-- %s\n", __func__);
5930}
5931
5932static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5933{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005934 struct nfs4_sequence_data *calldata = data;
5935 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005936 struct nfs4_sequence_args *args;
5937 struct nfs4_sequence_res *res;
5938
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005939 args = task->tk_msg.rpc_argp;
5940 res = task->tk_msg.rpc_resp;
5941
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005942 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005943}
5944
5945static const struct rpc_call_ops nfs41_sequence_ops = {
5946 .rpc_call_done = nfs41_sequence_call_done,
5947 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005948 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005949};
5950
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005951static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5952 struct rpc_cred *cred,
5953 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005955 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956 struct rpc_message msg = {
5957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5958 .rpc_cred = cred,
5959 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005960 struct rpc_task_setup task_setup_data = {
5961 .rpc_client = clp->cl_rpcclient,
5962 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005963 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005964 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005965 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966
Alexandros Batsakis71358402010-02-05 03:45:05 -08005967 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005968 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005969 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005970 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005971 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005972 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005973 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005974 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005975 if (is_privileged)
5976 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005977 msg.rpc_argp = &calldata->args;
5978 msg.rpc_resp = &calldata->res;
5979 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005980 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005982 return rpc_run_task(&task_setup_data);
5983}
5984
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005985static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005986{
5987 struct rpc_task *task;
5988 int ret = 0;
5989
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005990 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5991 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005992 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005993 if (IS_ERR(task))
5994 ret = PTR_ERR(task);
5995 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005996 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997 dprintk("<-- %s status=%d\n", __func__, ret);
5998 return ret;
5999}
6000
6001static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6002{
6003 struct rpc_task *task;
6004 int ret;
6005
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006006 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006007 if (IS_ERR(task)) {
6008 ret = PTR_ERR(task);
6009 goto out;
6010 }
6011 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006012 if (!ret) {
6013 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6014
6015 if (task->tk_status == 0)
6016 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006017 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006018 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006019 rpc_put_task(task);
6020out:
6021 dprintk("<-- %s status=%d\n", __func__, ret);
6022 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006023}
6024
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006025struct nfs4_reclaim_complete_data {
6026 struct nfs_client *clp;
6027 struct nfs41_reclaim_complete_args arg;
6028 struct nfs41_reclaim_complete_res res;
6029};
6030
6031static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6032{
6033 struct nfs4_reclaim_complete_data *calldata = data;
6034
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006035 nfs41_setup_sequence(calldata->clp->cl_session,
6036 &calldata->arg.seq_args,
6037 &calldata->res.seq_res,
6038 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006039}
6040
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006041static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6042{
6043 switch(task->tk_status) {
6044 case 0:
6045 case -NFS4ERR_COMPLETE_ALREADY:
6046 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6047 break;
6048 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006049 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006050 /* fall through */
6051 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006052 return -EAGAIN;
6053 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006054 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006055 }
6056 return 0;
6057}
6058
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006059static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6060{
6061 struct nfs4_reclaim_complete_data *calldata = data;
6062 struct nfs_client *clp = calldata->clp;
6063 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6064
6065 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006066 if (!nfs41_sequence_done(task, res))
6067 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006068
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006069 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6070 rpc_restart_call_prepare(task);
6071 return;
6072 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006073 dprintk("<-- %s\n", __func__);
6074}
6075
6076static void nfs4_free_reclaim_complete_data(void *data)
6077{
6078 struct nfs4_reclaim_complete_data *calldata = data;
6079
6080 kfree(calldata);
6081}
6082
6083static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6084 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6085 .rpc_call_done = nfs4_reclaim_complete_done,
6086 .rpc_release = nfs4_free_reclaim_complete_data,
6087};
6088
6089/*
6090 * Issue a global reclaim complete.
6091 */
6092static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6093{
6094 struct nfs4_reclaim_complete_data *calldata;
6095 struct rpc_task *task;
6096 struct rpc_message msg = {
6097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6098 };
6099 struct rpc_task_setup task_setup_data = {
6100 .rpc_client = clp->cl_rpcclient,
6101 .rpc_message = &msg,
6102 .callback_ops = &nfs4_reclaim_complete_call_ops,
6103 .flags = RPC_TASK_ASYNC,
6104 };
6105 int status = -ENOMEM;
6106
6107 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006108 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006109 if (calldata == NULL)
6110 goto out;
6111 calldata->clp = clp;
6112 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006113
Trond Myklebust9d12b212012-01-17 22:04:25 -05006114 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006115 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006116 msg.rpc_argp = &calldata->arg;
6117 msg.rpc_resp = &calldata->res;
6118 task_setup_data.callback_data = calldata;
6119 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006120 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006121 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006122 goto out;
6123 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006124 status = nfs4_wait_for_completion_rpc_task(task);
6125 if (status == 0)
6126 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006127 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006128 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006129out:
6130 dprintk("<-- %s status=%d\n", __func__, status);
6131 return status;
6132}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006133
6134static void
6135nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6136{
6137 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006138 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006139 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140
6141 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006142 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6143 * right now covering the LAYOUTGET we are about to send.
6144 * However, that is not so catastrophic, and there seems
6145 * to be no way to prevent it completely.
6146 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006147 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006148 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006149 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006150 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6151 NFS_I(lgp->args.inode)->layout,
6152 lgp->args.ctx->state)) {
6153 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006154 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006155}
6156
6157static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6158{
6159 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006160 struct inode *inode = lgp->args.inode;
6161 struct nfs_server *server = NFS_SERVER(inode);
6162 struct pnfs_layout_hdr *lo;
6163 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006164 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006165
6166 dprintk("--> %s\n", __func__);
6167
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006168 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006169 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006170
6171 switch (task->tk_status) {
6172 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006173 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006174 case -NFS4ERR_LAYOUTTRYLATER:
6175 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006176 timeo = rpc_get_timeout(task->tk_client);
6177 giveup = lgp->args.timestamp + timeo;
6178 if (time_after(giveup, jiffies))
6179 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006180 break;
6181 case -NFS4ERR_EXPIRED:
6182 case -NFS4ERR_BAD_STATEID:
6183 spin_lock(&inode->i_lock);
6184 lo = NFS_I(inode)->layout;
6185 if (!lo || list_empty(&lo->plh_segs)) {
6186 spin_unlock(&inode->i_lock);
6187 /* If the open stateid was bad, then recover it. */
6188 state = lgp->args.ctx->state;
6189 } else {
6190 LIST_HEAD(head);
6191
6192 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6193 spin_unlock(&inode->i_lock);
6194 /* Mark the bad layout state as invalid, then
6195 * retry using the open stateid. */
6196 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006197 }
6198 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006199 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6200 rpc_restart_call_prepare(task);
6201out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006202 dprintk("<-- %s\n", __func__);
6203}
6204
Idan Kedar85541162012-08-02 11:47:10 +03006205static size_t max_response_pages(struct nfs_server *server)
6206{
6207 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6208 return nfs_page_array_len(0, max_resp_sz);
6209}
6210
6211static void nfs4_free_pages(struct page **pages, size_t size)
6212{
6213 int i;
6214
6215 if (!pages)
6216 return;
6217
6218 for (i = 0; i < size; i++) {
6219 if (!pages[i])
6220 break;
6221 __free_page(pages[i]);
6222 }
6223 kfree(pages);
6224}
6225
6226static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6227{
6228 struct page **pages;
6229 int i;
6230
6231 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6232 if (!pages) {
6233 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6234 return NULL;
6235 }
6236
6237 for (i = 0; i < size; i++) {
6238 pages[i] = alloc_page(gfp_flags);
6239 if (!pages[i]) {
6240 dprintk("%s: failed to allocate page\n", __func__);
6241 nfs4_free_pages(pages, size);
6242 return NULL;
6243 }
6244 }
6245
6246 return pages;
6247}
6248
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006249static void nfs4_layoutget_release(void *calldata)
6250{
6251 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006252 struct inode *inode = lgp->args.inode;
6253 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006254 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006255
6256 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006257 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006258 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006259 put_nfs_open_context(lgp->args.ctx);
6260 kfree(calldata);
6261 dprintk("<-- %s\n", __func__);
6262}
6263
6264static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6265 .rpc_call_prepare = nfs4_layoutget_prepare,
6266 .rpc_call_done = nfs4_layoutget_done,
6267 .rpc_release = nfs4_layoutget_release,
6268};
6269
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006270struct pnfs_layout_segment *
6271nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006272{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006273 struct inode *inode = lgp->args.inode;
6274 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006275 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276 struct rpc_task *task;
6277 struct rpc_message msg = {
6278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6279 .rpc_argp = &lgp->args,
6280 .rpc_resp = &lgp->res,
6281 };
6282 struct rpc_task_setup task_setup_data = {
6283 .rpc_client = server->client,
6284 .rpc_message = &msg,
6285 .callback_ops = &nfs4_layoutget_call_ops,
6286 .callback_data = lgp,
6287 .flags = RPC_TASK_ASYNC,
6288 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006289 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006290 int status = 0;
6291
6292 dprintk("--> %s\n", __func__);
6293
Idan Kedar85541162012-08-02 11:47:10 +03006294 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6295 if (!lgp->args.layout.pages) {
6296 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006297 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006298 }
6299 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006300 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006301
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006302 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006303 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006304 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006305
6306 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6307 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6308
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006309 task = rpc_run_task(&task_setup_data);
6310 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006311 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006312 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006313 if (status == 0)
6314 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006315 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6316 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006317 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006318 rpc_put_task(task);
6319 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006320 if (status)
6321 return ERR_PTR(status);
6322 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323}
6324
Benny Halevycbe82602011-05-22 19:52:37 +03006325static void
6326nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6327{
6328 struct nfs4_layoutreturn *lrp = calldata;
6329
6330 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006331 nfs41_setup_sequence(lrp->clp->cl_session,
6332 &lrp->args.seq_args,
6333 &lrp->res.seq_res,
6334 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006335}
6336
6337static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6338{
6339 struct nfs4_layoutreturn *lrp = calldata;
6340 struct nfs_server *server;
6341
6342 dprintk("--> %s\n", __func__);
6343
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006344 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006345 return;
6346
6347 server = NFS_SERVER(lrp->args.inode);
6348 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006349 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006350 return;
6351 }
Benny Halevycbe82602011-05-22 19:52:37 +03006352 dprintk("<-- %s\n", __func__);
6353}
6354
6355static void nfs4_layoutreturn_release(void *calldata)
6356{
6357 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006358 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006359
6360 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006361 spin_lock(&lo->plh_inode->i_lock);
6362 if (lrp->res.lrs_present)
6363 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6364 lo->plh_block_lgets--;
6365 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006366 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006367 kfree(calldata);
6368 dprintk("<-- %s\n", __func__);
6369}
6370
6371static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6372 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6373 .rpc_call_done = nfs4_layoutreturn_done,
6374 .rpc_release = nfs4_layoutreturn_release,
6375};
6376
6377int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6378{
6379 struct rpc_task *task;
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6382 .rpc_argp = &lrp->args,
6383 .rpc_resp = &lrp->res,
6384 };
6385 struct rpc_task_setup task_setup_data = {
6386 .rpc_client = lrp->clp->cl_rpcclient,
6387 .rpc_message = &msg,
6388 .callback_ops = &nfs4_layoutreturn_call_ops,
6389 .callback_data = lrp,
6390 };
6391 int status;
6392
6393 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006394 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006395 task = rpc_run_task(&task_setup_data);
6396 if (IS_ERR(task))
6397 return PTR_ERR(task);
6398 status = task->tk_status;
6399 dprintk("<-- %s status=%d\n", __func__, status);
6400 rpc_put_task(task);
6401 return status;
6402}
6403
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006404/*
6405 * Retrieve the list of Data Server devices from the MDS.
6406 */
6407static int _nfs4_getdevicelist(struct nfs_server *server,
6408 const struct nfs_fh *fh,
6409 struct pnfs_devicelist *devlist)
6410{
6411 struct nfs4_getdevicelist_args args = {
6412 .fh = fh,
6413 .layoutclass = server->pnfs_curr_ld->id,
6414 };
6415 struct nfs4_getdevicelist_res res = {
6416 .devlist = devlist,
6417 };
6418 struct rpc_message msg = {
6419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6420 .rpc_argp = &args,
6421 .rpc_resp = &res,
6422 };
6423 int status;
6424
6425 dprintk("--> %s\n", __func__);
6426 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6427 &res.seq_res, 0);
6428 dprintk("<-- %s status=%d\n", __func__, status);
6429 return status;
6430}
6431
6432int nfs4_proc_getdevicelist(struct nfs_server *server,
6433 const struct nfs_fh *fh,
6434 struct pnfs_devicelist *devlist)
6435{
6436 struct nfs4_exception exception = { };
6437 int err;
6438
6439 do {
6440 err = nfs4_handle_exception(server,
6441 _nfs4_getdevicelist(server, fh, devlist),
6442 &exception);
6443 } while (exception.retry);
6444
6445 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6446 err, devlist->num_devs);
6447
6448 return err;
6449}
6450EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6451
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006452static int
6453_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6454{
6455 struct nfs4_getdeviceinfo_args args = {
6456 .pdev = pdev,
6457 };
6458 struct nfs4_getdeviceinfo_res res = {
6459 .pdev = pdev,
6460 };
6461 struct rpc_message msg = {
6462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6463 .rpc_argp = &args,
6464 .rpc_resp = &res,
6465 };
6466 int status;
6467
6468 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006469 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006470 dprintk("<-- %s status=%d\n", __func__, status);
6471
6472 return status;
6473}
6474
6475int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6476{
6477 struct nfs4_exception exception = { };
6478 int err;
6479
6480 do {
6481 err = nfs4_handle_exception(server,
6482 _nfs4_proc_getdeviceinfo(server, pdev),
6483 &exception);
6484 } while (exception.retry);
6485 return err;
6486}
6487EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6488
Andy Adamson863a3c62011-03-23 13:27:54 +00006489static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6490{
6491 struct nfs4_layoutcommit_data *data = calldata;
6492 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006493 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006494
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006495 nfs41_setup_sequence(session,
6496 &data->args.seq_args,
6497 &data->res.seq_res,
6498 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006499}
6500
6501static void
6502nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6503{
6504 struct nfs4_layoutcommit_data *data = calldata;
6505 struct nfs_server *server = NFS_SERVER(data->args.inode);
6506
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006507 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006508 return;
6509
6510 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006511 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6512 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6513 case -NFS4ERR_BADLAYOUT: /* no layout */
6514 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006515 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006516 break;
6517 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006518 nfs_post_op_update_inode_force_wcc(data->args.inode,
6519 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006520 break;
6521 default:
6522 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6523 rpc_restart_call_prepare(task);
6524 return;
6525 }
6526 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006527}
6528
6529static void nfs4_layoutcommit_release(void *calldata)
6530{
6531 struct nfs4_layoutcommit_data *data = calldata;
6532
Andy Adamsondb29c082011-07-30 20:52:38 -04006533 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006534 put_rpccred(data->cred);
6535 kfree(data);
6536}
6537
6538static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6539 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6540 .rpc_call_done = nfs4_layoutcommit_done,
6541 .rpc_release = nfs4_layoutcommit_release,
6542};
6543
6544int
Andy Adamsonef311532011-03-12 02:58:10 -05006545nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006546{
6547 struct rpc_message msg = {
6548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6549 .rpc_argp = &data->args,
6550 .rpc_resp = &data->res,
6551 .rpc_cred = data->cred,
6552 };
6553 struct rpc_task_setup task_setup_data = {
6554 .task = &data->task,
6555 .rpc_client = NFS_CLIENT(data->args.inode),
6556 .rpc_message = &msg,
6557 .callback_ops = &nfs4_layoutcommit_ops,
6558 .callback_data = data,
6559 .flags = RPC_TASK_ASYNC,
6560 };
6561 struct rpc_task *task;
6562 int status = 0;
6563
6564 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6565 "lbw: %llu inode %lu\n",
6566 data->task.tk_pid, sync,
6567 data->args.lastbytewritten,
6568 data->args.inode->i_ino);
6569
Trond Myklebust9d12b212012-01-17 22:04:25 -05006570 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006571 task = rpc_run_task(&task_setup_data);
6572 if (IS_ERR(task))
6573 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006574 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006575 goto out;
6576 status = nfs4_wait_for_completion_rpc_task(task);
6577 if (status != 0)
6578 goto out;
6579 status = task->tk_status;
6580out:
6581 dprintk("%s: status %d\n", __func__, status);
6582 rpc_put_task(task);
6583 return status;
6584}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006585
6586static int
6587_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6588 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6589{
6590 struct nfs41_secinfo_no_name_args args = {
6591 .style = SECINFO_STYLE_CURRENT_FH,
6592 };
6593 struct nfs4_secinfo_res res = {
6594 .flavors = flavors,
6595 };
6596 struct rpc_message msg = {
6597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6598 .rpc_argp = &args,
6599 .rpc_resp = &res,
6600 };
6601 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6602}
6603
6604static int
6605nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6606 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6607{
6608 struct nfs4_exception exception = { };
6609 int err;
6610 do {
6611 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6612 switch (err) {
6613 case 0:
6614 case -NFS4ERR_WRONGSEC:
6615 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006616 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006617 default:
6618 err = nfs4_handle_exception(server, err, &exception);
6619 }
6620 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006621out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006622 return err;
6623}
6624
6625static int
6626nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6627 struct nfs_fsinfo *info)
6628{
6629 int err;
6630 struct page *page;
6631 rpc_authflavor_t flavor;
6632 struct nfs4_secinfo_flavors *flavors;
6633
6634 page = alloc_page(GFP_KERNEL);
6635 if (!page) {
6636 err = -ENOMEM;
6637 goto out;
6638 }
6639
6640 flavors = page_address(page);
6641 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6642
6643 /*
6644 * Fall back on "guess and check" method if
6645 * the server doesn't support SECINFO_NO_NAME
6646 */
6647 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6648 err = nfs4_find_root_sec(server, fhandle, info);
6649 goto out_freepage;
6650 }
6651 if (err)
6652 goto out_freepage;
6653
6654 flavor = nfs_find_best_sec(flavors);
6655 if (err == 0)
6656 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6657
6658out_freepage:
6659 put_page(page);
6660 if (err == -EACCES)
6661 return -EPERM;
6662out:
6663 return err;
6664}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006665
6666static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006667{
6668 int status;
6669 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006670 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006671 };
6672 struct nfs41_test_stateid_res res;
6673 struct rpc_message msg = {
6674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6675 .rpc_argp = &args,
6676 .rpc_resp = &res,
6677 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006678
Chuck Lever38527b12012-07-11 16:30:23 -04006679 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006680 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006681 nfs4_set_sequence_privileged(&args.seq_args);
6682 status = nfs4_call_sync_sequence(server->client, server, &msg,
6683 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006684 if (status != NFS_OK) {
6685 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006686 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006687 }
6688 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006689 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006690}
6691
Chuck Lever38527b12012-07-11 16:30:23 -04006692/**
6693 * nfs41_test_stateid - perform a TEST_STATEID operation
6694 *
6695 * @server: server / transport on which to perform the operation
6696 * @stateid: state ID to test
6697 *
6698 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6699 * Otherwise a negative NFS4ERR value is returned if the operation
6700 * failed or the state ID is not currently valid.
6701 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006702static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006703{
6704 struct nfs4_exception exception = { };
6705 int err;
6706 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006707 err = _nfs41_test_stateid(server, stateid);
6708 if (err != -NFS4ERR_DELAY)
6709 break;
6710 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006711 } while (exception.retry);
6712 return err;
6713}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006714
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006715static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006716{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006717 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006718 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006719 };
6720 struct nfs41_free_stateid_res res;
6721 struct rpc_message msg = {
6722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6723 .rpc_argp = &args,
6724 .rpc_resp = &res,
6725 };
Chuck Lever38527b12012-07-11 16:30:23 -04006726 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006727
Chuck Lever38527b12012-07-11 16:30:23 -04006728 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006729 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006730 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006731 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006732 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006733 dprintk("NFS reply free_stateid: %d\n", status);
6734 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006735}
6736
Chuck Lever38527b12012-07-11 16:30:23 -04006737/**
6738 * nfs41_free_stateid - perform a FREE_STATEID operation
6739 *
6740 * @server: server / transport on which to perform the operation
6741 * @stateid: state ID to release
6742 *
6743 * Returns NFS_OK if the server freed "stateid". Otherwise a
6744 * negative NFS4ERR value is returned.
6745 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006746static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006747{
6748 struct nfs4_exception exception = { };
6749 int err;
6750 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006751 err = _nfs4_free_stateid(server, stateid);
6752 if (err != -NFS4ERR_DELAY)
6753 break;
6754 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006755 } while (exception.retry);
6756 return err;
6757}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006758
6759static bool nfs41_match_stateid(const nfs4_stateid *s1,
6760 const nfs4_stateid *s2)
6761{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006762 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006763 return false;
6764
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006765 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006766 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006767 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006768 return true;
6769
6770 return false;
6771}
6772
Andy Adamson557134a2009-04-01 09:21:53 -04006773#endif /* CONFIG_NFS_V4_1 */
6774
Trond Myklebust36281ca2012-03-04 18:13:56 -05006775static bool nfs4_match_stateid(const nfs4_stateid *s1,
6776 const nfs4_stateid *s2)
6777{
Trond Myklebustf597c532012-03-04 18:13:56 -05006778 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006779}
6780
6781
Trond Myklebust17280172012-03-11 13:11:00 -04006782static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006783 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006784 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 .recover_open = nfs4_open_reclaim,
6786 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006787 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006788 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006789 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790};
6791
Andy Adamson591d71c2009-04-01 09:22:47 -04006792#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006793static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006794 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6795 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6796 .recover_open = nfs4_open_reclaim,
6797 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006798 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006799 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006800 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006801 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006802};
6803#endif /* CONFIG_NFS_V4_1 */
6804
Trond Myklebust17280172012-03-11 13:11:00 -04006805static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006806 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006807 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 .recover_open = nfs4_open_expired,
6809 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006810 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006811 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812};
6813
Andy Adamson591d71c2009-04-01 09:22:47 -04006814#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006815static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006816 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6817 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006818 .recover_open = nfs41_open_expired,
6819 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006820 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006821 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006822};
6823#endif /* CONFIG_NFS_V4_1 */
6824
Trond Myklebust17280172012-03-11 13:11:00 -04006825static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006826 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006827 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006828 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006829};
6830
6831#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006832static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006833 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006834 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006835 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006836};
6837#endif
6838
Trond Myklebust97dc1352010-06-16 09:52:26 -04006839static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6840 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006841 .init_caps = NFS_CAP_READDIRPLUS
6842 | NFS_CAP_ATOMIC_OPEN
6843 | NFS_CAP_CHANGE_ATTR
6844 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006845 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006846 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006847 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006848 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6849 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6850 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006851};
6852
6853#if defined(CONFIG_NFS_V4_1)
6854static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6855 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006856 .init_caps = NFS_CAP_READDIRPLUS
6857 | NFS_CAP_ATOMIC_OPEN
6858 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006859 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006860 | NFS_CAP_STATEID_NFSV41
6861 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006862 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006863 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006864 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006865 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6866 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6867 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006868};
6869#endif
6870
Trond Myklebust97dc1352010-06-16 09:52:26 -04006871const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6872 [0] = &nfs_v4_0_minor_ops,
6873#if defined(CONFIG_NFS_V4_1)
6874 [1] = &nfs_v4_1_minor_ops,
6875#endif
6876};
6877
Bryan Schumaker73a79702012-07-16 16:39:12 -04006878const struct inode_operations nfs4_dir_inode_operations = {
6879 .create = nfs_create,
6880 .lookup = nfs_lookup,
6881 .atomic_open = nfs_atomic_open,
6882 .link = nfs_link,
6883 .unlink = nfs_unlink,
6884 .symlink = nfs_symlink,
6885 .mkdir = nfs_mkdir,
6886 .rmdir = nfs_rmdir,
6887 .mknod = nfs_mknod,
6888 .rename = nfs_rename,
6889 .permission = nfs_permission,
6890 .getattr = nfs_getattr,
6891 .setattr = nfs_setattr,
6892 .getxattr = generic_getxattr,
6893 .setxattr = generic_setxattr,
6894 .listxattr = generic_listxattr,
6895 .removexattr = generic_removexattr,
6896};
6897
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006898static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006899 .permission = nfs_permission,
6900 .getattr = nfs_getattr,
6901 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006902 .getxattr = generic_getxattr,
6903 .setxattr = generic_setxattr,
6904 .listxattr = generic_listxattr,
6905 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006906};
6907
David Howells509de812006-08-22 20:06:11 -04006908const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 .version = 4, /* protocol version */
6910 .dentry_ops = &nfs4_dentry_operations,
6911 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006912 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006913 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006915 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006916 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 .getattr = nfs4_proc_getattr,
6918 .setattr = nfs4_proc_setattr,
6919 .lookup = nfs4_proc_lookup,
6920 .access = nfs4_proc_access,
6921 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 .create = nfs4_proc_create,
6923 .remove = nfs4_proc_remove,
6924 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006925 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 .unlink_done = nfs4_proc_unlink_done,
6927 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006928 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006929 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006930 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 .link = nfs4_proc_link,
6932 .symlink = nfs4_proc_symlink,
6933 .mkdir = nfs4_proc_mkdir,
6934 .rmdir = nfs4_proc_remove,
6935 .readdir = nfs4_proc_readdir,
6936 .mknod = nfs4_proc_mknod,
6937 .statfs = nfs4_proc_statfs,
6938 .fsinfo = nfs4_proc_fsinfo,
6939 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006940 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 .decode_dirent = nfs4_decode_dirent,
6942 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006943 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006944 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006945 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006947 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006948 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006949 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006951 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006952 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006954 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006955 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006956 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006957 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006958 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006959 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006960 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006961 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006962 .create_server = nfs4_create_server,
6963 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964};
6965
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006966static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6967 .prefix = XATTR_NAME_NFSV4_ACL,
6968 .list = nfs4_xattr_list_nfs4_acl,
6969 .get = nfs4_xattr_get_nfs4_acl,
6970 .set = nfs4_xattr_set_nfs4_acl,
6971};
6972
6973const struct xattr_handler *nfs4_xattr_handlers[] = {
6974 &nfs4_xattr_nfs4_acl_handler,
6975 NULL
6976};
6977
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978/*
6979 * Local variables:
6980 * c-basic-offset: 8
6981 * End:
6982 */