blob: 7bbb06f280fcec778c2c56c13b846fe99afef9b5 [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 Myklebust52567b02009-10-23 14:46:42 -0400110 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700112 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
119 * This is our standard bitmap for GETATTR requests.
120 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400121const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 FATTR4_WORD0_TYPE
123 | FATTR4_WORD0_CHANGE
124 | FATTR4_WORD0_SIZE
125 | FATTR4_WORD0_FSID
126 | FATTR4_WORD0_FILEID,
127 FATTR4_WORD1_MODE
128 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER
130 | FATTR4_WORD1_OWNER_GROUP
131 | FATTR4_WORD1_RAWDEV
132 | FATTR4_WORD1_SPACE_USED
133 | FATTR4_WORD1_TIME_ACCESS
134 | FATTR4_WORD1_TIME_METADATA
135 | FATTR4_WORD1_TIME_MODIFY
136};
137
Trond Myklebust1549210f2012-06-05 09:16:47 -0400138static const u32 nfs4_pnfs_open_bitmap[3] = {
139 FATTR4_WORD0_TYPE
140 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_SIZE
142 | FATTR4_WORD0_FSID
143 | FATTR4_WORD0_FILEID,
144 FATTR4_WORD1_MODE
145 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY,
153 FATTR4_WORD2_MDSTHRESHOLD
154};
155
Andy Adamsone23008e2012-10-02 21:07:32 -0400156static const u32 nfs4_open_noattr_bitmap[3] = {
157 FATTR4_WORD0_TYPE
158 | FATTR4_WORD0_CHANGE
159 | FATTR4_WORD0_FILEID,
160};
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162const u32 nfs4_statfs_bitmap[2] = {
163 FATTR4_WORD0_FILES_AVAIL
164 | FATTR4_WORD0_FILES_FREE
165 | FATTR4_WORD0_FILES_TOTAL,
166 FATTR4_WORD1_SPACE_AVAIL
167 | FATTR4_WORD1_SPACE_FREE
168 | FATTR4_WORD1_SPACE_TOTAL
169};
170
Trond Myklebust4ce79712005-06-22 17:16:21 +0000171const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 FATTR4_WORD0_MAXLINK
173 | FATTR4_WORD0_MAXNAME,
174 0
175};
176
Fred Isamandae100c2011-07-30 20:52:37 -0400177const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 | FATTR4_WORD0_MAXREAD
179 | FATTR4_WORD0_MAXWRITE
180 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700181 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400182 | FATTR4_WORD1_FS_LAYOUT_TYPES,
183 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Manoj Naik830b8e32006-06-09 09:34:25 -0400186const u32 nfs4_fs_locations_bitmap[2] = {
187 FATTR4_WORD0_TYPE
188 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_SIZE
190 | FATTR4_WORD0_FSID
191 | FATTR4_WORD0_FILEID
192 | FATTR4_WORD0_FS_LOCATIONS,
193 FATTR4_WORD1_MODE
194 | FATTR4_WORD1_NUMLINKS
195 | FATTR4_WORD1_OWNER
196 | FATTR4_WORD1_OWNER_GROUP
197 | FATTR4_WORD1_RAWDEV
198 | FATTR4_WORD1_SPACE_USED
199 | FATTR4_WORD1_TIME_ACCESS
200 | FATTR4_WORD1_TIME_METADATA
201 | FATTR4_WORD1_TIME_MODIFY
202 | FATTR4_WORD1_MOUNTED_ON_FILEID
203};
204
Al Virobc4785c2006-10-19 23:28:51 -0700205static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct nfs4_readdir_arg *readdir)
207{
Al Viro0dbb4c62006-10-19 23:28:49 -0700208 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000211 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
213 return;
214 }
215
216 readdir->cookie = 0;
217 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
218 if (cookie == 2)
219 return;
220
221 /*
222 * NFSv4 servers do not return entries for '.' and '..'
223 * Therefore, we fake these entries here. We let '.'
224 * have cookie 0 and '..' have cookie 1. Note that
225 * when talking to the server, we always send cookie 0
226 * instead of 1 or 2.
227 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800228 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (cookie == 0) {
231 *p++ = xdr_one; /* next */
232 *p++ = xdr_zero; /* cookie, first word */
233 *p++ = xdr_one; /* cookie, second word */
234 *p++ = xdr_one; /* entry len */
235 memcpy(p, ".\0\0\0", 4); /* entry */
236 p++;
237 *p++ = xdr_one; /* bitmap length */
238 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
239 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400240 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 *p++ = xdr_one; /* next */
244 *p++ = xdr_zero; /* cookie, first word */
245 *p++ = xdr_two; /* cookie, second word */
246 *p++ = xdr_two; /* entry len */
247 memcpy(p, "..\0\0", 4); /* entry */
248 p++;
249 *p++ = xdr_one; /* bitmap length */
250 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
251 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400252 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 readdir->pgbase = (char *)p - (char *)start;
255 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800256 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Trond Myklebust65de8722008-12-23 15:21:44 -0500259static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
260{
261 int res = 0;
262
263 might_sleep();
264
265 if (*timeout <= 0)
266 *timeout = NFS4_POLL_RETRY_MIN;
267 if (*timeout > NFS4_POLL_RETRY_MAX)
268 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100269 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500270 if (fatal_signal_pending(current))
271 res = -ERESTARTSYS;
272 *timeout <<= 1;
273 return res;
274}
275
276/* This is the error handling routine for processes that are allowed
277 * to sleep.
278 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800279static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500280{
281 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500282 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500283 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500284 int ret = errorcode;
285
286 exception->retry = 0;
287 switch(errorcode) {
288 case 0:
289 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500290 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400291 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400292 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500293 exception->retry = 1;
294 return 0;
295 }
296 if (state == NULL)
297 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400298 ret = nfs4_schedule_stateid_recovery(server, state);
299 if (ret < 0)
300 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500301 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500302 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500303 case -NFS4ERR_ADMIN_REVOKED:
304 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 if (state == NULL)
306 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400307 nfs_remove_bad_delegation(state->inode);
Trond Myklebust5d422302013-03-14 16:57:48 -0400308 ret = nfs4_schedule_stateid_recovery(server, state);
309 if (ret < 0)
310 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500311 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400312 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400313 if (state != NULL) {
314 ret = nfs4_schedule_stateid_recovery(server, state);
315 if (ret < 0)
316 break;
317 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500318 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500319 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500320 nfs4_schedule_lease_recovery(clp);
321 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500322#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400323 case -NFS4ERR_BADSESSION:
324 case -NFS4ERR_BADSLOT:
325 case -NFS4ERR_BAD_HIGH_SLOT:
326 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
327 case -NFS4ERR_DEADSESSION:
328 case -NFS4ERR_SEQ_FALSE_RETRY:
329 case -NFS4ERR_SEQ_MISORDERED:
330 dprintk("%s ERROR: %d Reset session\n", __func__,
331 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400332 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400333 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500334#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500335 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500336 if (exception->timeout > HZ) {
337 /* We have retried a decent amount, time to
338 * fail
339 */
340 ret = -EBUSY;
341 break;
342 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 case -NFS4ERR_GRACE:
344 case -NFS4ERR_DELAY:
345 ret = nfs4_delay(server->client, &exception->timeout);
346 if (ret != 0)
347 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400348 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500349 case -NFS4ERR_OLD_STATEID:
350 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800351 break;
352 case -NFS4ERR_BADOWNER:
353 /* The following works around a Linux server bug! */
354 case -NFS4ERR_BADNAME:
355 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
356 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
357 exception->retry = 1;
358 printk(KERN_WARNING "NFS: v4 server %s "
359 "does not accept raw "
360 "uid/gids. "
361 "Reenabling the idmapper.\n",
362 server->nfs_client->cl_hostname);
363 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500364 }
365 /* We failed to handle the error */
366 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500367wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500368 ret = nfs4_wait_clnt_recover(clp);
369 if (ret == 0)
370 exception->retry = 1;
371 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500372}
373
374
Trond Myklebust452e9352010-07-31 14:29:06 -0400375static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_lock(&clp->cl_lock);
378 if (time_before(clp->cl_last_renewal,timestamp))
379 clp->cl_last_renewal = timestamp;
380 spin_unlock(&clp->cl_lock);
381}
382
Trond Myklebust452e9352010-07-31 14:29:06 -0400383static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
384{
385 do_renew_lease(server->nfs_client, timestamp);
386}
387
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400388#if defined(CONFIG_NFS_V4_1)
389
Trond Myklebustd185a332010-06-16 09:52:25 -0400390static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400391{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500392 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400393 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500394 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400395
Benny Halevydfb4f3092010-09-24 09:17:01 -0400396 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400397 /* just wake up the next guy waiting since
398 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500399 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500400 return;
Andy Adamson13615872009-04-01 09:22:17 -0400401 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500402 tbl = res->sr_slot->table;
403 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500404
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500405 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500406 /* Be nice to the server: try to ensure that the last transmitted
407 * value for highest_user_slotid <= target_highest_slotid
408 */
409 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
410 send_new_highest_used_slotid = true;
411
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500412 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
413 send_new_highest_used_slotid = false;
414 goto out_unlock;
415 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500416 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500417
418 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
419 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500420out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500421 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400422 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500423 if (send_new_highest_used_slotid)
424 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400425}
426
Trond Myklebust14516c32010-07-31 14:29:06 -0400427static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500429 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500430 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400431 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500432 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500433 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434
Bryan Schumaker468f8612011-04-18 15:57:32 -0400435 /* don't increment the sequence number if the task wasn't sent */
436 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 goto out;
438
Trond Myklebust933602e2012-11-16 12:12:38 -0500439 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500440 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500441
Trond Myklebustac20d162012-12-15 15:36:07 -0500442 if (slot->interrupted) {
443 slot->interrupted = 0;
444 interrupted = true;
445 }
446
Andy Adamson691daf32009-12-04 15:55:39 -0500447 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400448 switch (res->sr_status) {
449 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400450 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500451 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500452 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500453 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500454 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500455 if (res->sr_status_flags != 0)
456 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500457 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500459 case 1:
460 /*
461 * sr_status remains 1 if an RPC level error occurred.
462 * The server may or may not have processed the sequence
463 * operation..
464 * Mark the slot as having hosted an interrupted RPC call.
465 */
466 slot->interrupted = 1;
467 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 case -NFS4ERR_DELAY:
469 /* The server detected a resend of the RPC call and
470 * returned NFS4ERR_DELAY as per Section 2.10.6.2
471 * of RFC5661.
472 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500473 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400474 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500475 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500476 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500478 case -NFS4ERR_BADSLOT:
479 /*
480 * The slot id we used was probably retired. Try again
481 * using a different slot id.
482 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500483 goto retry_nowait;
484 case -NFS4ERR_SEQ_MISORDERED:
485 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500486 * Was the last operation on this sequence interrupted?
487 * If so, retry after bumping the sequence number.
488 */
489 if (interrupted) {
490 ++slot->seq_nr;
491 goto retry_nowait;
492 }
493 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500494 * Could this slot have been previously retired?
495 * If so, then the server may be expecting seq_nr = 1!
496 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500497 if (slot->seq_nr != 1) {
498 slot->seq_nr = 1;
499 goto retry_nowait;
500 }
501 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500502 case -NFS4ERR_SEQ_FALSE_RETRY:
503 ++slot->seq_nr;
504 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400505 default:
506 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500507 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400508 }
509out:
510 /* The session may be reset by one of the error handlers. */
511 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400512 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500513 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500514retry_nowait:
515 if (rpc_restart_call_prepare(task)) {
516 task->tk_status = 0;
517 ret = 0;
518 }
519 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400520out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400521 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400522 goto out;
523 rpc_delay(task, NFS4_POLL_RETRY_MAX);
524 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400525}
526
Trond Myklebust14516c32010-07-31 14:29:06 -0400527static int nfs4_sequence_done(struct rpc_task *task,
528 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400529{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500530 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400531 return 1;
532 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400533}
534
Trond Myklebust9d12b212012-01-17 22:04:25 -0500535static void nfs41_init_sequence(struct nfs4_sequence_args *args,
536 struct nfs4_sequence_res *res, int cache_reply)
537{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500538 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500539 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400540 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541 if (cache_reply)
542 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500543 res->sr_slot = NULL;
544}
545
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400546static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
547{
548 args->sa_privileged = 1;
549}
550
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000551int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 struct nfs4_sequence_args *args,
553 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct rpc_task *task)
555{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558
559 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400561 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400562 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564 tbl = &session->fc_slot_table;
565
Trond Myklebust69d206b2012-11-22 13:21:02 -0500566 task->tk_timeout = 0;
567
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400568 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400569 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400570 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500571 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500572 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400573 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400574 }
575
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500576 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500577 if (IS_ERR(slot)) {
578 /* If out of memory, try again in 1/4 second */
579 if (slot == ERR_PTR(-ENOMEM))
580 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400582 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 }
584 spin_unlock(&tbl->slot_tbl_lock);
585
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500586 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500588 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
589 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590
Benny Halevydfb4f3092010-09-24 09:17:01 -0400591 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500592 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400593 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 /*
595 * sr_status is only set in decode_sequence, and so will remain
596 * set to 1 if an rpc level failure occurs.
597 */
598 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400599out_success:
600 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400602out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400603 /* Privileged tasks are queued with top priority */
604 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400605 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
606 NULL, RPC_PRIORITY_PRIVILEGED);
607 else
608 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400609 spin_unlock(&tbl->slot_tbl_lock);
610 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000612EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613
Trond Myklebust035168a2010-06-16 09:52:26 -0400614int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct rpc_task *task)
618{
Trond Myklebust035168a2010-06-16 09:52:26 -0400619 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 int ret = 0;
621
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400622 if (session == NULL) {
623 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400625 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400626
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500627 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400628 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500629 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400630
Trond Myklebust9d12b212012-01-17 22:04:25 -0500631 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632out:
633 dprintk("<-- %s status=%d\n", __func__, ret);
634 return ret;
635}
636
637struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400638 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 struct nfs4_sequence_args *seq_args;
640 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641};
642
643static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
644{
645 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400646 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647
Trond Myklebust035168a2010-06-16 09:52:26 -0400648 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
649
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400650 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651}
652
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
654{
655 struct nfs41_call_sync_data *data = calldata;
656
Trond Myklebust14516c32010-07-31 14:29:06 -0400657 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400658}
659
Trond Myklebust17280172012-03-11 13:11:00 -0400660static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400662 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663};
664
Bryan Schumaker7c513052011-03-24 17:12:24 +0000665static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
666 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 struct rpc_message *msg,
668 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400669 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670{
671 int ret;
672 struct rpc_task *task;
673 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400674 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 .seq_args = args,
676 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 };
678 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000679 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .rpc_message = msg,
681 .callback_ops = &nfs41_call_sync_ops,
682 .callback_data = &data
683 };
684
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 task = rpc_run_task(&task_setup);
686 if (IS_ERR(task))
687 ret = PTR_ERR(task);
688 else {
689 ret = task->tk_status;
690 rpc_put_task(task);
691 }
692 return ret;
693}
694
Trond Myklebustdf896452010-06-16 09:52:26 -0400695#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400696static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697void nfs41_init_sequence(struct nfs4_sequence_args *args,
698 struct nfs4_sequence_res *res, int cache_reply)
699{
700}
701
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400702static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
703{
704}
705
706
Trond Myklebust14516c32010-07-31 14:29:06 -0400707static int nfs4_sequence_done(struct rpc_task *task,
708 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400709{
Trond Myklebust14516c32010-07-31 14:29:06 -0400710 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400711}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400712#endif /* CONFIG_NFS_V4_1 */
713
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400714static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715int _nfs4_call_sync(struct rpc_clnt *clnt,
716 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717 struct rpc_message *msg,
718 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400719 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400720{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722}
723
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400724static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000725int nfs4_call_sync(struct rpc_clnt *clnt,
726 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000727 struct rpc_message *msg,
728 struct nfs4_sequence_args *args,
729 struct nfs4_sequence_res *res,
730 int cache_reply)
731{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400732 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000733 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400734 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000735}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736
Trond Myklebust38478b22006-05-25 01:40:57 -0400737static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Trond Myklebust38478b22006-05-25 01:40:57 -0400739 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400742 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700743 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400744 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700745 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000746 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400751 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 struct nfs_openargs o_arg;
753 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100754 struct nfs_open_confirmargs c_arg;
755 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500756 struct nfs4_string owner_name;
757 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400760 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400762 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100764 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400765 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100766 int rpc_status;
767 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768};
769
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400770
771static void nfs4_init_opendata_res(struct nfs4_opendata *p)
772{
773 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400774 p->o_res.seqid = p->o_arg.seqid;
775 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400776 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400777 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400778 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500779 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780}
781
Al Viro82a2c1b2011-06-22 18:30:55 -0400782static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500783 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400784 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400785 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787{
Al Viro82a2c1b2011-06-22 18:30:55 -0400788 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789 struct inode *dir = parent->d_inode;
790 struct nfs_server *server = NFS_SERVER(dir);
791 struct nfs4_opendata *p;
792
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400793 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 if (p == NULL)
795 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400796 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 if (p->o_arg.seqid == NULL)
798 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400799 nfs_sb_active(dentry->d_sb);
800 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 p->dir = parent;
802 p->owner = sp;
803 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500804 p->o_arg.open_flags = flags;
805 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700806 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
807 * will return permission denied for all bits until close */
808 if (!(flags & O_EXCL)) {
809 /* ask server to check for all possible rights as results
810 * are cached */
811 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
812 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700813 }
David Howells7539bba2006-08-22 20:06:09 -0400814 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400815 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
816 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400817 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->o_arg.server = server;
819 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400820 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400821 p->o_arg.claim = claim;
822 switch (claim) {
823 case NFS4_OPEN_CLAIM_NULL:
824 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
825 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
826 p->o_arg.fh = NFS_FH(dir);
827 break;
828 case NFS4_OPEN_CLAIM_PREVIOUS:
829 case NFS4_OPEN_CLAIM_FH:
830 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
831 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
832 p->o_arg.fh = NFS_FH(dentry->d_inode);
833 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500834 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500835 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400836
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->o_arg.u.attrs = &p->attrs;
838 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500839
840 verf[0] = jiffies;
841 verf[1] = current->pid;
842 memcpy(p->o_arg.u.verifier.data, verf,
843 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100845 p->c_arg.fh = &p->o_res.fh;
846 p->c_arg.stateid = &p->o_res.stateid;
847 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400848 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 return p;
851err_free:
852 kfree(p);
853err:
854 dput(parent);
855 return NULL;
856}
857
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400860 struct nfs4_opendata *p = container_of(kref,
861 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400862 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863
864 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400865 if (p->state != NULL)
866 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400867 nfs4_put_state_owner(p->owner);
868 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400869 dput(p->dentry);
870 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500871 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872 kfree(p);
873}
874
875static void nfs4_opendata_put(struct nfs4_opendata *p)
876{
877 if (p != NULL)
878 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879}
880
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
882{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100883 int ret;
884
Trond Myklebust06f814a2006-01-03 09:55:07 +0100885 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100886 return ret;
887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890{
891 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892
Trond Myklebust536e43d2012-01-17 22:04:26 -0500893 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 goto out;
895 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400896 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500897 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
898 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400899 break;
900 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500901 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
902 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400903 break;
904 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500905 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
906 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400907 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400909 return ret;
910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500914 if (delegation == NULL)
915 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500918 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500920 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
921 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500922 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400923 return 1;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100927{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100929 case FMODE_WRITE:
930 state->n_wronly++;
931 break;
932 case FMODE_READ:
933 state->n_rdonly++;
934 break;
935 case FMODE_READ|FMODE_WRITE:
936 state->n_rdwr++;
937 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400942{
943 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500944 nfs4_stateid_copy(&state->stateid, stateid);
945 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400947 case FMODE_READ:
948 set_bit(NFS_O_RDONLY_STATE, &state->flags);
949 break;
950 case FMODE_WRITE:
951 set_bit(NFS_O_WRONLY_STATE, &state->flags);
952 break;
953 case FMODE_READ|FMODE_WRITE:
954 set_bit(NFS_O_RDWR_STATE, &state->flags);
955 }
956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400959{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400962 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400963}
964
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965static 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 -0700966{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400967 /*
968 * Protect the call to nfs4_state_set_mode_locked and
969 * serialise the stateid update
970 */
971 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400972 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500973 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400974 set_bit(NFS_DELEGATED_STATE, &state->flags);
975 }
976 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400978 write_sequnlock(&state->seqlock);
979 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700981 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984static 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 -0500985{
986 struct nfs_inode *nfsi = NFS_I(state->inode);
987 struct nfs_delegation *deleg_cur;
988 int ret = 0;
989
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500991
992 rcu_read_lock();
993 deleg_cur = rcu_dereference(nfsi->delegation);
994 if (deleg_cur == NULL)
995 goto no_delegation;
996
997 spin_lock(&deleg_cur->lock);
998 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500999 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001001 goto no_delegation_unlock;
1002
1003 if (delegation == NULL)
1004 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001005 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001006 goto no_delegation_unlock;
1007
Trond Myklebustb7391f42008-12-23 15:21:52 -05001008 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001010 ret = 1;
1011no_delegation_unlock:
1012 spin_unlock(&deleg_cur->lock);
1013no_delegation:
1014 rcu_read_unlock();
1015
1016 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001018 ret = 1;
1019 }
1020
1021 return ret;
1022}
1023
1024
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026{
1027 struct nfs_delegation *delegation;
1028
1029 rcu_read_lock();
1030 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 rcu_read_unlock();
1033 return;
1034 }
1035 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001036 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037}
1038
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040{
1041 struct nfs4_state *state = opendata->state;
1042 struct nfs_inode *nfsi = NFS_I(state->inode);
1043 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001044 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 nfs4_stateid stateid;
1047 int ret = -EAGAIN;
1048
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001051 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 if (can_open_cached(state, fmode, open_mode)) {
1053 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001054 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001055 goto out_return_state;
1056 }
1057 spin_unlock(&state->owner->so_lock);
1058 }
Trond Myklebust34310432008-12-23 15:21:38 -05001059 rcu_read_lock();
1060 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001061 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001062 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001064 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001066 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001067 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001068 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 if (ret != 0)
1070 goto out;
1071 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001072
1073 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001074 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001075 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077out:
1078 return ERR_PTR(ret);
1079out_return_state:
1080 atomic_inc(&state->count);
1081 return state;
1082}
1083
Andy Adamsone23008e2012-10-02 21:07:32 -04001084static void
1085nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1086{
1087 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1088 struct nfs_delegation *delegation;
1089 int delegation_flags = 0;
1090
1091 rcu_read_lock();
1092 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1093 if (delegation)
1094 delegation_flags = delegation->flags;
1095 rcu_read_unlock();
1096 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1097 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1098 "returning a delegation for "
1099 "OPEN(CLAIM_DELEGATE_CUR)\n",
1100 clp->cl_hostname);
1101 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1102 nfs_inode_set_delegation(state->inode,
1103 data->owner->so_cred,
1104 &data->o_res);
1105 else
1106 nfs_inode_reclaim_delegation(state->inode,
1107 data->owner->so_cred,
1108 &data->o_res);
1109}
1110
1111/*
1112 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1113 * and update the nfs4_state.
1114 */
1115static struct nfs4_state *
1116_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1117{
1118 struct inode *inode = data->state->inode;
1119 struct nfs4_state *state = data->state;
1120 int ret;
1121
1122 if (!data->rpc_done) {
1123 ret = data->rpc_status;
1124 goto err;
1125 }
1126
1127 ret = -ESTALE;
1128 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1129 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1130 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1131 goto err;
1132
1133 ret = -ENOMEM;
1134 state = nfs4_get_open_state(inode, data->owner);
1135 if (state == NULL)
1136 goto err;
1137
1138 ret = nfs_refresh_inode(inode, &data->f_attr);
1139 if (ret)
1140 goto err;
1141
1142 if (data->o_res.delegation_type != 0)
1143 nfs4_opendata_check_deleg(data, state);
1144 update_open_stateid(state, &data->o_res.stateid, NULL,
1145 data->o_arg.fmode);
1146
1147 return state;
1148err:
1149 return ERR_PTR(ret);
1150
1151}
1152
1153static struct nfs4_state *
1154_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001155{
1156 struct inode *inode;
1157 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001158 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001159
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001161 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001162 goto out;
1163 }
1164
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001165 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001166 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001167 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001168 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001169 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001170 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001171 goto err;
1172 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001173 state = nfs4_get_open_state(inode, data->owner);
1174 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001175 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001176 if (data->o_res.delegation_type != 0)
1177 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001178 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001180 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001181out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001182 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001183 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001184err_put_inode:
1185 iput(inode);
1186err:
1187 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001188}
1189
Andy Adamsone23008e2012-10-02 21:07:32 -04001190static struct nfs4_state *
1191nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1192{
1193 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1194 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1195 return _nfs4_opendata_to_nfs4_state(data);
1196}
1197
Trond Myklebust864472e2006-01-03 09:55:15 +01001198static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1199{
1200 struct nfs_inode *nfsi = NFS_I(state->inode);
1201 struct nfs_open_context *ctx;
1202
1203 spin_lock(&state->inode->i_lock);
1204 list_for_each_entry(ctx, &nfsi->open_files, list) {
1205 if (ctx->state != state)
1206 continue;
1207 get_nfs_open_context(ctx);
1208 spin_unlock(&state->inode->i_lock);
1209 return ctx;
1210 }
1211 spin_unlock(&state->inode->i_lock);
1212 return ERR_PTR(-ENOENT);
1213}
1214
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001215static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1216 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001217{
1218 struct nfs4_opendata *opendata;
1219
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001220 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1221 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001222 if (opendata == NULL)
1223 return ERR_PTR(-ENOMEM);
1224 opendata->state = state;
1225 atomic_inc(&state->count);
1226 return opendata;
1227}
1228
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001229static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001230{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 int ret;
1233
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001234 opendata->o_arg.open_flags = 0;
1235 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001236 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1237 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1238 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001239 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 if (ret != 0)
1241 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001242 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001243 if (IS_ERR(newstate))
1244 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001245 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001246 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 return 0;
1248}
1249
1250static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1251{
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 int ret;
1254
1255 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001256 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 smp_rmb();
1258 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001259 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001260 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 if (ret != 0)
1262 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001263 if (newstate != state)
1264 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 }
1266 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001267 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001268 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 if (ret != 0)
1270 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001271 if (newstate != state)
1272 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 }
1274 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001275 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001276 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 if (ret != 0)
1278 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001279 if (newstate != state)
1280 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001281 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001282 /*
1283 * We may have performed cached opens for all three recoveries.
1284 * Check if we need to update the current stateid.
1285 */
1286 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001287 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001288 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001289 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001290 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001291 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001292 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 return 0;
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/*
1297 * OPEN_RECLAIM:
1298 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001300static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001302 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 int status;
1306
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001307 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1308 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001311 rcu_read_lock();
1312 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001313 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001314 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001315 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 opendata->o_arg.u.delegation_type = delegation_type;
1317 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return status;
1320}
1321
Trond Myklebust539cd032007-06-05 11:46:42 -04001322static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct nfs_server *server = NFS_SERVER(state->inode);
1325 struct nfs4_exception exception = { };
1326 int err;
1327 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001328 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001329 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001330 break;
1331 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 } while (exception.retry);
1333 return err;
1334}
1335
Trond Myklebust864472e2006-01-03 09:55:15 +01001336static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1337{
1338 struct nfs_open_context *ctx;
1339 int ret;
1340
1341 ctx = nfs4_state_find_open_context(state);
1342 if (IS_ERR(ctx))
1343 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001344 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001345 put_nfs_open_context(ctx);
1346 return ret;
1347}
1348
Trond Myklebust13437e12007-07-06 15:10:43 -04001349static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001351 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001354 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1355 NFS4_OPEN_CLAIM_DELEGATE_CUR);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356 if (IS_ERR(opendata))
1357 return PTR_ERR(opendata);
Trond Myklebustf597c532012-03-04 18:13:56 -05001358 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001359 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001360 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Trond Myklebust13437e12007-07-06 15:10:43 -04001364int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001367 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int err;
1369 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001370 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 switch (err) {
1372 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001373 case -ENOENT:
1374 case -ESTALE:
1375 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001376 case -NFS4ERR_BADSESSION:
1377 case -NFS4ERR_BADSLOT:
1378 case -NFS4ERR_BAD_HIGH_SLOT:
1379 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1380 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001381 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001382 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001383 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 case -NFS4ERR_STALE_CLIENTID:
1386 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001387 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 case -NFS4ERR_EXPIRED:
1389 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001390 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001391 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001392 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001393 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001394 case -NFS4ERR_ADMIN_REVOKED:
1395 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001396 nfs_inode_find_state_and_recover(state->inode,
1397 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001398 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001399 case -ENOMEM:
1400 err = 0;
1401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001403 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 err = nfs4_handle_exception(server, err, &exception);
1405 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001406out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return err;
1408}
1409
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1411{
1412 struct nfs4_opendata *data = calldata;
1413
1414 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001415 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001416 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001417 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001418 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001419 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001420 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421}
1422
1423static void nfs4_open_confirm_release(void *calldata)
1424{
1425 struct nfs4_opendata *data = calldata;
1426 struct nfs4_state *state = NULL;
1427
1428 /* If this request hasn't been cancelled, do nothing */
1429 if (data->cancelled == 0)
1430 goto out_free;
1431 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001432 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001433 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001435 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001436 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001438 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001439}
1440
1441static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001442 .rpc_call_done = nfs4_open_confirm_done,
1443 .rpc_release = nfs4_open_confirm_release,
1444};
1445
1446/*
1447 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1448 */
1449static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1450{
1451 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1452 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 struct rpc_message msg = {
1454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1455 .rpc_argp = &data->c_arg,
1456 .rpc_resp = &data->c_res,
1457 .rpc_cred = data->owner->so_cred,
1458 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001459 struct rpc_task_setup task_setup_data = {
1460 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001462 .callback_ops = &nfs4_open_confirm_ops,
1463 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001464 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001465 .flags = RPC_TASK_ASYNC,
1466 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int status;
1468
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001469 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001470 data->rpc_done = 0;
1471 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001473 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001474 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001475 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476 status = nfs4_wait_for_completion_rpc_task(task);
1477 if (status != 0) {
1478 data->cancelled = 1;
1479 smp_wmb();
1480 } else
1481 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001482 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return status;
1484}
1485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 struct nfs4_opendata *data = calldata;
1489 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001492 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001493 /*
1494 * Check if we still need to send an OPEN call, or if we can use
1495 * a delegation instead.
1496 */
1497 if (data->state != NULL) {
1498 struct nfs_delegation *delegation;
1499
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001501 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502 rcu_read_lock();
1503 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001504 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1505 can_open_delegated(delegation, data->o_arg.fmode))
1506 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001507 rcu_read_unlock();
1508 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001509 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001510 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001511 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001512 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001513 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001514 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1515 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001516 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001517 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001518 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001519 &data->o_res.seq_res,
1520 task) != 0)
1521 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001522 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001523unlock_no_action:
1524 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001525out_no_action:
1526 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001527out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001528 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001529}
1530
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531static void nfs4_open_done(struct rpc_task *task, void *calldata)
1532{
1533 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001536
Trond Myklebust14516c32010-07-31 14:29:06 -04001537 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1538 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001539
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001541 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1542 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001543 case S_IFREG:
1544 break;
1545 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001547 break;
1548 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001550 break;
1551 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001553 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001554 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001555 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001556 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1557 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001558 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001559 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001561
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562static void nfs4_open_release(void *calldata)
1563{
1564 struct nfs4_opendata *data = calldata;
1565 struct nfs4_state *state = NULL;
1566
1567 /* If this request hasn't been cancelled, do nothing */
1568 if (data->cancelled == 0)
1569 goto out_free;
1570 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001571 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572 goto out_free;
1573 /* In case we need an open_confirm, no cleanup! */
1574 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1575 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001576 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001577 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001578 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001580 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001581}
1582
1583static const struct rpc_call_ops nfs4_open_ops = {
1584 .rpc_call_prepare = nfs4_open_prepare,
1585 .rpc_call_done = nfs4_open_done,
1586 .rpc_release = nfs4_open_release,
1587};
1588
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590{
1591 struct inode *dir = data->dir->d_inode;
1592 struct nfs_server *server = NFS_SERVER(dir);
1593 struct nfs_openargs *o_arg = &data->o_arg;
1594 struct nfs_openres *o_res = &data->o_res;
1595 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001596 struct rpc_message msg = {
1597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1598 .rpc_argp = o_arg,
1599 .rpc_resp = o_res,
1600 .rpc_cred = data->owner->so_cred,
1601 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001602 struct rpc_task_setup task_setup_data = {
1603 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001604 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001605 .callback_ops = &nfs4_open_ops,
1606 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001607 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001608 .flags = RPC_TASK_ASYNC,
1609 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 int status;
1611
Trond Myklebust9d12b212012-01-17 22:04:25 -05001612 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001613 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001614 data->rpc_done = 0;
1615 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001616 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001617 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001618 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001619 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001620 if (IS_ERR(task))
1621 return PTR_ERR(task);
1622 status = nfs4_wait_for_completion_rpc_task(task);
1623 if (status != 0) {
1624 data->cancelled = 1;
1625 smp_wmb();
1626 } else
1627 status = data->rpc_status;
1628 rpc_put_task(task);
1629
1630 return status;
1631}
1632
1633static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1634{
1635 struct inode *dir = data->dir->d_inode;
1636 struct nfs_openres *o_res = &data->o_res;
1637 int status;
1638
1639 status = nfs4_run_open_task(data, 1);
1640 if (status != 0 || !data->rpc_done)
1641 return status;
1642
Trond Myklebust6926afd2012-01-07 13:22:46 -05001643 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1644
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001645 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1646 status = _nfs4_proc_open_confirm(data);
1647 if (status != 0)
1648 return status;
1649 }
1650
1651 return status;
1652}
1653
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001654static int nfs4_opendata_access(struct rpc_cred *cred,
1655 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001656 struct nfs4_state *state, fmode_t fmode,
1657 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001658{
1659 struct nfs_access_entry cache;
1660 u32 mask;
1661
1662 /* access call failed or for some reason the server doesn't
1663 * support any access modes -- defer access call until later */
1664 if (opendata->o_res.access_supported == 0)
1665 return 0;
1666
1667 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001668 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001669 * write mode bits, but POSIX allows the creating process to write.
1670 * use openflags to check for exec, because fmode won't
1671 * always have FMODE_EXEC set when file open for exec. */
1672 if (openflags & __FMODE_EXEC) {
1673 /* ONLY check for exec rights */
1674 mask = MAY_EXEC;
1675 } else if (fmode & FMODE_READ)
1676 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001677
1678 cache.cred = cred;
1679 cache.jiffies = jiffies;
1680 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1681 nfs_access_add_cache(state->inode, &cache);
1682
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001683 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001684 return 0;
1685
1686 /* even though OPEN succeeded, access is denied. Close the file */
1687 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001688 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001689}
1690
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001691/*
1692 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1693 */
1694static int _nfs4_proc_open(struct nfs4_opendata *data)
1695{
1696 struct inode *dir = data->dir->d_inode;
1697 struct nfs_server *server = NFS_SERVER(dir);
1698 struct nfs_openargs *o_arg = &data->o_arg;
1699 struct nfs_openres *o_res = &data->o_res;
1700 int status;
1701
1702 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001703 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001705 if (status != 0) {
1706 if (status == -NFS4ERR_BADNAME &&
1707 !(o_arg->open_flags & O_CREAT))
1708 return -ENOENT;
1709 return status;
1710 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001711
Trond Myklebust6926afd2012-01-07 13:22:46 -05001712 nfs_fattr_map_and_free_names(server, &data->f_attr);
1713
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001714 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001715 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001716 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1717 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001719 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001721 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001724 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Andy Adamsond83217c2011-03-01 01:34:17 +00001728static int nfs4_recover_expired_lease(struct nfs_server *server)
1729{
1730 return nfs4_client_recover_expired_lease(server->nfs_client);
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/*
1734 * OPEN_EXPIRED:
1735 * reclaim state on the server after a network partition.
1736 * Assumes caller holds the appropriate lock
1737 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001738static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001740 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001741 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001743 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1744 NFS4_OPEN_CLAIM_NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001745 if (IS_ERR(opendata))
1746 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001747 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001748 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001749 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001750 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001754static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001755{
Trond Myklebust539cd032007-06-05 11:46:42 -04001756 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001757 struct nfs4_exception exception = { };
1758 int err;
1759
1760 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001761 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001762 switch (err) {
1763 default:
1764 goto out;
1765 case -NFS4ERR_GRACE:
1766 case -NFS4ERR_DELAY:
1767 nfs4_handle_exception(server, err, &exception);
1768 err = 0;
1769 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001770 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001771out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001772 return err;
1773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1776{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001778 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 ctx = nfs4_state_find_open_context(state);
1781 if (IS_ERR(ctx))
1782 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001783 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001784 put_nfs_open_context(ctx);
1785 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001788#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001789static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001790{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001791 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001792 nfs4_stateid *stateid = &state->stateid;
1793 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001794
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001795 /* If a state reset has been done, test_stateid is unneeded */
1796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1797 return;
1798
1799 status = nfs41_test_stateid(server, stateid);
1800 if (status != NFS_OK) {
1801 /* Free the stateid unless the server explicitly
1802 * informs us the stateid is unrecognized. */
1803 if (status != -NFS4ERR_BAD_STATEID)
1804 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001805 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001806
Bryan Schumaker69388672012-09-26 15:25:52 -04001807 write_seqlock(&state->seqlock);
1808 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1809 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001810 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1811 }
1812}
1813
1814/**
1815 * nfs41_check_open_stateid - possibly free an open stateid
1816 *
1817 * @state: NFSv4 state for an inode
1818 *
1819 * Returns NFS_OK if recovery for this stateid is now finished.
1820 * Otherwise a negative NFS4ERR value is returned.
1821 */
1822static int nfs41_check_open_stateid(struct nfs4_state *state)
1823{
1824 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001825 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001826 int status;
1827
1828 /* If a state reset has been done, test_stateid is unneeded */
1829 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1830 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1831 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1832 return -NFS4ERR_BAD_STATEID;
1833
1834 status = nfs41_test_stateid(server, stateid);
1835 if (status != NFS_OK) {
1836 /* Free the stateid unless the server explicitly
1837 * informs us the stateid is unrecognized. */
1838 if (status != -NFS4ERR_BAD_STATEID)
1839 nfs41_free_stateid(server, stateid);
1840
1841 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1842 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1843 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001844 }
1845 return status;
1846}
1847
1848static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1849{
Chuck Levereb64cf92012-07-11 16:30:05 -04001850 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001851
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001852 nfs41_clear_delegation_stateid(state);
1853 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001854 if (status != NFS_OK)
1855 status = nfs4_open_expired(sp, state);
1856 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001857}
1858#endif
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001861 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1862 * fields corresponding to attributes that were used to store the verifier.
1863 * Make sure we clobber those fields in the later setattr call
1864 */
1865static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1866{
1867 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1868 !(sattr->ia_valid & ATTR_ATIME_SET))
1869 sattr->ia_valid |= ATTR_ATIME;
1870
1871 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1872 !(sattr->ia_valid & ATTR_MTIME_SET))
1873 sattr->ia_valid |= ATTR_MTIME;
1874}
1875
Trond Myklebustc21443c2013-02-07 14:26:21 -05001876static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1877 fmode_t fmode,
1878 int flags,
1879 struct nfs4_state **res)
1880{
1881 struct nfs4_state_owner *sp = opendata->owner;
1882 struct nfs_server *server = sp->so_server;
1883 struct nfs4_state *state;
1884 unsigned int seq;
1885 int ret;
1886
1887 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1888
1889 ret = _nfs4_proc_open(opendata);
1890 if (ret != 0)
1891 goto out;
1892
1893 state = nfs4_opendata_to_nfs4_state(opendata);
1894 ret = PTR_ERR(state);
1895 if (IS_ERR(state))
1896 goto out;
1897 if (server->caps & NFS_CAP_POSIX_LOCK)
1898 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1899
1900 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1901 if (ret != 0)
1902 goto out;
1903
1904 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1905 nfs4_schedule_stateid_recovery(server, state);
1906 nfs4_wait_clnt_recover(server->nfs_client);
1907 }
1908 *res = state;
1909out:
1910 return ret;
1911}
1912
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001913/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001914 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Andy Adamson82be4172012-05-23 05:02:35 -04001916static int _nfs4_do_open(struct inode *dir,
1917 struct dentry *dentry,
1918 fmode_t fmode,
1919 int flags,
1920 struct iattr *sattr,
1921 struct rpc_cred *cred,
1922 struct nfs4_state **res,
1923 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct nfs4_state_owner *sp;
1926 struct nfs4_state *state = NULL;
1927 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001928 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001929 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001933 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1934 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1936 goto out_err;
1937 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001938 status = nfs4_recover_expired_lease(server);
1939 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001940 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001941 if (dentry->d_inode != NULL)
1942 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001943 status = -ENOMEM;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001944 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
1945 NFS4_OPEN_CLAIM_NULL,
1946 GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001947 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001948 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Andy Adamson82be4172012-05-23 05:02:35 -04001950 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1951 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1952 if (!opendata->f_attr.mdsthreshold)
1953 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001954 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001955 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001956 if (dentry->d_inode != NULL)
1957 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001958
Trond Myklebustc21443c2013-02-07 14:26:21 -05001959 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001960 if (status != 0)
1961 goto err_opendata_put;
1962
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001963 if (opendata->o_arg.open_flags & O_EXCL) {
1964 nfs4_exclusive_attrset(opendata, sattr);
1965
1966 nfs_fattr_init(opendata->o_res.f_attr);
1967 status = nfs4_do_setattr(state->inode, cred,
1968 opendata->o_res.f_attr, sattr,
1969 state);
1970 if (status == 0)
1971 nfs_setattr_update_inode(state->inode, sattr);
1972 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1973 }
Andy Adamson82be4172012-05-23 05:02:35 -04001974
1975 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1976 *ctx_th = opendata->f_attr.mdsthreshold;
1977 else
1978 kfree(opendata->f_attr.mdsthreshold);
1979 opendata->f_attr.mdsthreshold = NULL;
1980
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001981 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 *res = state;
1984 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001985err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001986 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001988err_put_state_owner:
1989 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *res = NULL;
1992 return status;
1993}
1994
1995
Andy Adamson82be4172012-05-23 05:02:35 -04001996static struct nfs4_state *nfs4_do_open(struct inode *dir,
1997 struct dentry *dentry,
1998 fmode_t fmode,
1999 int flags,
2000 struct iattr *sattr,
2001 struct rpc_cred *cred,
2002 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct nfs4_exception exception = { };
2005 struct nfs4_state *res;
2006 int status;
2007
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002008 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002010 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2011 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (status == 0)
2013 break;
2014 /* NOTE: BAD_SEQID means the server and client disagree about the
2015 * book-keeping w.r.t. state-changing operations
2016 * (OPEN/CLOSE/LOCK/LOCKU...)
2017 * It is actually a sign of a bug on the client or on the server.
2018 *
2019 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002020 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * have unhashed the old state_owner for us, and that we can
2022 * therefore safely retry using a new one. We should still warn
2023 * the user though...
2024 */
2025 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002026 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002027 " returned a bad sequence-id error!\n",
2028 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 exception.retry = 1;
2030 continue;
2031 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002032 /*
2033 * BAD_STATEID on OPEN means that the server cancelled our
2034 * state before it received the OPEN_CONFIRM.
2035 * Recover by retrying the request as per the discussion
2036 * on Page 181 of RFC3530.
2037 */
2038 if (status == -NFS4ERR_BAD_STATEID) {
2039 exception.retry = 1;
2040 continue;
2041 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002042 if (status == -EAGAIN) {
2043 /* We must have found a delegation */
2044 exception.retry = 1;
2045 continue;
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2048 status, &exception));
2049 } while (exception.retry);
2050 return res;
2051}
2052
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002053static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2054 struct nfs_fattr *fattr, struct iattr *sattr,
2055 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002057 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002059 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 .iap = sattr,
2061 .server = server,
2062 .bitmask = server->attr_bitmask,
2063 };
2064 struct nfs_setattrres res = {
2065 .fattr = fattr,
2066 .server = server,
2067 };
2068 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2070 .rpc_argp = &arg,
2071 .rpc_resp = &res,
2072 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002074 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002075 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Trond Myklebust0e574af2005-10-27 22:12:38 -04002077 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Trond Myklebust5d422302013-03-14 16:57:48 -04002079 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002080 struct nfs_lockowner lockowner = {
2081 .l_owner = current->files,
2082 .l_pid = current->tgid,
2083 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002084 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002085 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002086 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2087 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002088 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002089 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002090 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Bryan Schumaker7c513052011-03-24 17:12:24 +00002092 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002093 if (status == 0 && state != NULL)
2094 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002095 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002098static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2099 struct nfs_fattr *fattr, struct iattr *sattr,
2100 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002102 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002103 struct nfs4_exception exception = {
2104 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002105 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 int err;
2108 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002109 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2110 switch (err) {
2111 case -NFS4ERR_OPENMODE:
2112 if (state && !(state->state & FMODE_WRITE)) {
2113 err = -EBADF;
2114 if (sattr->ia_valid & ATTR_OPEN)
2115 err = -EACCES;
2116 goto out;
2117 }
2118 }
2119 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002121out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return err;
2123}
2124
2125struct nfs4_closedata {
2126 struct inode *inode;
2127 struct nfs4_state *state;
2128 struct nfs_closeargs arg;
2129 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002130 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002132 bool roc;
2133 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134};
2135
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002136static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002137{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138 struct nfs4_closedata *calldata = data;
2139 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002140 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002141
Fred Isamanf7e89172011-01-06 11:36:32 +00002142 if (calldata->roc)
2143 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002144 nfs4_put_open_state(calldata->state);
2145 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002146 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002147 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002148 kfree(calldata);
2149}
2150
Trond Myklebust88069f72009-12-08 08:33:16 -05002151static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2152 fmode_t fmode)
2153{
2154 spin_lock(&state->owner->so_lock);
2155 if (!(fmode & FMODE_READ))
2156 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2157 if (!(fmode & FMODE_WRITE))
2158 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2159 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2160 spin_unlock(&state->owner->so_lock);
2161}
2162
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002163static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002165 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct nfs_server *server = NFS_SERVER(calldata->inode);
2168
Chuck Levera3ca5652012-03-01 17:00:40 -05002169 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002170 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* hmm. we are done with the inode, and in the process of freeing
2173 * the state_owner. we keep this around to process errors
2174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 switch (task->tk_status) {
2176 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002177 if (calldata->roc)
2178 pnfs_roc_set_barrier(state->inode,
2179 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002180 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002181 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002182 nfs4_close_clear_stateid_flags(state,
2183 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 break;
2185 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002186 case -NFS4ERR_OLD_STATEID:
2187 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002189 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002192 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2193 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002195 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002196 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002197 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002200static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002202 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002203 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002204 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002205 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002206
Chuck Levera3ca5652012-03-01 17:00:40 -05002207 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002208 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002209 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002210
Trond Myklebust88069f72009-12-08 08:33:16 -05002211 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2212 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002213 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002214 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002215 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002216 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002217 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2218 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2219 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002220 }
2221 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002222 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2223 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2224 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002225 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002226 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002227 if (!nfs4_valid_open_stateid(state))
2228 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002229 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002230
2231 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002232 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002233 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002234 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002235
Fred Isamanf7e89172011-01-06 11:36:32 +00002236 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002237 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002238 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002239 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002240 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002241 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002242
Trond Myklebust516a6af2005-10-27 22:12:41 -04002243 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002244 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002245 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002246 &calldata->arg.seq_args,
2247 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002248 task) != 0)
2249 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002250 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002251 return;
2252out_no_action:
2253 task->tk_action = NULL;
2254out_wait:
2255 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002258static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002259 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002260 .rpc_call_done = nfs4_close_done,
2261 .rpc_release = nfs4_free_closedata,
2262};
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264/*
2265 * It is possible for data to be read/written from a mem-mapped file
2266 * after the sys_close call (which hits the vfs layer as a flush).
2267 * This means that we can't safely call nfsv4 close on a file until
2268 * the inode is cleared. This in turn means that we are not good
2269 * NFSv4 citizens - we do not indicate to the server to update the file's
2270 * share state even when we are done with one of the three share
2271 * stateid's in the inode.
2272 *
2273 * NOTE: Caller must be holding the sp->so_owner semaphore!
2274 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002275int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002277 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002279 struct nfs4_state_owner *sp = state->owner;
2280 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2283 .rpc_cred = state->owner->so_cred,
2284 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002285 struct rpc_task_setup task_setup_data = {
2286 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002287 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002288 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002289 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002290 .flags = RPC_TASK_ASYNC,
2291 };
Trond Myklebust95121352005-10-18 14:20:12 -07002292 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002294 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002296 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002297 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002298 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002300 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002301 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002303 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002304 if (calldata->arg.seqid == NULL)
2305 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002306 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002307 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002308 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002309 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002310 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002311 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002312 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002313
Trond Myklebust1174dd12010-12-21 10:52:24 -05002314 msg.rpc_argp = &calldata->arg;
2315 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002316 task_setup_data.callback_data = calldata;
2317 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002318 if (IS_ERR(task))
2319 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002320 status = 0;
2321 if (wait)
2322 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002323 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002324 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002325out_free_calldata:
2326 kfree(calldata);
2327out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002328 nfs4_put_open_state(state);
2329 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002330 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Trond Myklebust2b484292010-09-17 10:56:51 -04002333static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002334nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct nfs4_state *state;
2337
Trond Myklebust565277f2007-10-15 18:17:53 -04002338 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002339 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2340 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002341 if (IS_ERR(state))
2342 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002343 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002344 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Trond Myklebust1185a552009-12-03 15:54:02 -05002347static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002348{
2349 if (ctx->state == NULL)
2350 return;
2351 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002352 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002353 else
Al Viro643168c2011-06-22 18:20:23 -04002354 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2358{
Benny Halevy43652ad2009-04-01 09:21:54 -04002359 struct nfs4_server_caps_arg args = {
2360 .fhandle = fhandle,
2361 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct nfs4_server_caps_res res = {};
2363 struct rpc_message msg = {
2364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002365 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 .rpc_resp = &res,
2367 };
2368 int status;
2369
Bryan Schumaker7c513052011-03-24 17:12:24 +00002370 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (status == 0) {
2372 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002373 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2374 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2375 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2376 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2377 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2379 server->caps |= NFS_CAP_ACLS;
2380 if (res.has_links != 0)
2381 server->caps |= NFS_CAP_HARDLINKS;
2382 if (res.has_symlinks != 0)
2383 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002384 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2385 server->caps |= NFS_CAP_FILEID;
2386 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2387 server->caps |= NFS_CAP_MODE;
2388 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2389 server->caps |= NFS_CAP_NLINK;
2390 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2391 server->caps |= NFS_CAP_OWNER;
2392 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2393 server->caps |= NFS_CAP_OWNER_GROUP;
2394 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2395 server->caps |= NFS_CAP_ATIME;
2396 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2397 server->caps |= NFS_CAP_CTIME;
2398 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2399 server->caps |= NFS_CAP_MTIME;
2400
Trond Myklebusta65318b2009-03-11 14:10:28 -04002401 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2402 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2403 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002405 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return status;
2409}
2410
Trond Myklebust55a97592006-06-09 09:34:19 -04002411int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 struct nfs4_exception exception = { };
2414 int err;
2415 do {
2416 err = nfs4_handle_exception(server,
2417 _nfs4_server_capabilities(server, fhandle),
2418 &exception);
2419 } while (exception.retry);
2420 return err;
2421}
2422
2423static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2424 struct nfs_fsinfo *info)
2425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct nfs4_lookup_root_arg args = {
2427 .bitmask = nfs4_fattr_bitmap,
2428 };
2429 struct nfs4_lookup_res res = {
2430 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002431 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 .fh = fhandle,
2433 };
2434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2436 .rpc_argp = &args,
2437 .rpc_resp = &res,
2438 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002439
Trond Myklebust0e574af2005-10-27 22:12:38 -04002440 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002441 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2445 struct nfs_fsinfo *info)
2446{
2447 struct nfs4_exception exception = { };
2448 int err;
2449 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002450 err = _nfs4_lookup_root(server, fhandle, info);
2451 switch (err) {
2452 case 0:
2453 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002454 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002455 default:
2456 err = nfs4_handle_exception(server, err, &exception);
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002459out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return err;
2461}
2462
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002463static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2464 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2465{
2466 struct rpc_auth *auth;
2467 int ret;
2468
2469 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002470 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002471 ret = -EIO;
2472 goto out;
2473 }
2474 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002475out:
2476 return ret;
2477}
2478
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002479static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002480 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002482 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002483 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002485 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002486 if (len < 0)
2487 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002488
2489 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002490 /* AUTH_UNIX is the default flavor if none was specified,
2491 * thus has already been tried. */
2492 if (flav_array[i] == RPC_AUTH_UNIX)
2493 continue;
2494
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002495 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002496 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002497 continue;
2498 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002499 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002500 /*
2501 * -EACCESS could mean that the user doesn't have correct permissions
2502 * to access the mount. It could also mean that we tried to mount
2503 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2504 * existing mount programs don't handle -EACCES very well so it should
2505 * be mapped to -EPERM instead.
2506 */
2507 if (status == -EACCES)
2508 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002509 return status;
2510}
2511
2512/*
2513 * get the file handle for the "/" directory on the server
2514 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002515int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2516 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002517{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002518 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002519 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002520 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2521 /*
2522 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2523 * by nfs4_map_errors() as this function exits.
2524 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002525 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (status == 0)
2527 status = nfs4_server_capabilities(server, fhandle);
2528 if (status == 0)
2529 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002530 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Bryan Schumakerbae36242012-05-10 15:07:31 -04002533static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2534 struct nfs_fsinfo *info)
2535{
2536 int error;
2537 struct nfs_fattr *fattr = info->fattr;
2538
2539 error = nfs4_server_capabilities(server, mntfh);
2540 if (error < 0) {
2541 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2542 return error;
2543 }
2544
2545 error = nfs4_proc_getattr(server, mntfh, fattr);
2546 if (error < 0) {
2547 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2548 return error;
2549 }
2550
2551 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2552 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2553 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2554
2555 return error;
2556}
2557
Manoj Naik6b97fd32006-06-09 09:34:29 -04002558/*
2559 * Get locations and (maybe) other attributes of a referral.
2560 * Note that we'll actually follow the referral later when
2561 * we detect fsid mismatch in inode revalidation
2562 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002563static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2564 const struct qstr *name, struct nfs_fattr *fattr,
2565 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002566{
2567 int status = -ENOMEM;
2568 struct page *page = NULL;
2569 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002570
2571 page = alloc_page(GFP_KERNEL);
2572 if (page == NULL)
2573 goto out;
2574 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2575 if (locations == NULL)
2576 goto out;
2577
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002578 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002579 if (status != 0)
2580 goto out;
2581 /* Make sure server returned a different fsid for the referral */
2582 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002583 dprintk("%s: server did not return a different fsid for"
2584 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002585 status = -EIO;
2586 goto out;
2587 }
Andy Adamson533eb462011-06-13 18:25:56 -04002588 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2589 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002590
Andy Adamson533eb462011-06-13 18:25:56 -04002591 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002592 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002593 memset(fhandle, 0, sizeof(struct nfs_fh));
2594out:
2595 if (page)
2596 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002597 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002598 return status;
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2602{
2603 struct nfs4_getattr_arg args = {
2604 .fh = fhandle,
2605 .bitmask = server->attr_bitmask,
2606 };
2607 struct nfs4_getattr_res res = {
2608 .fattr = fattr,
2609 .server = server,
2610 };
2611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2613 .rpc_argp = &args,
2614 .rpc_resp = &res,
2615 };
2616
Trond Myklebust0e574af2005-10-27 22:12:38 -04002617 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002618 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
2621static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2622{
2623 struct nfs4_exception exception = { };
2624 int err;
2625 do {
2626 err = nfs4_handle_exception(server,
2627 _nfs4_proc_getattr(server, fhandle, fattr),
2628 &exception);
2629 } while (exception.retry);
2630 return err;
2631}
2632
2633/*
2634 * The file is not closed if it is opened due to the a request to change
2635 * the size of the file. The open call will not be needed once the
2636 * VFS layer lookup-intents are implemented.
2637 *
2638 * Close is called when the inode is destroyed.
2639 * If we haven't opened the file for O_WRONLY, we
2640 * need to in the size_change case to obtain a stateid.
2641 *
2642 * Got race?
2643 * Because OPEN is always done by name in nfsv4, it is
2644 * possible that we opened a different file by the same
2645 * name. We can recognize this race condition, but we
2646 * can't do anything about it besides returning an error.
2647 *
2648 * This will be fixed with VFS changes (lookup-intent).
2649 */
2650static int
2651nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2652 struct iattr *sattr)
2653{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002654 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002655 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002656 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int status;
2658
Benny Halevy8a1636c2010-07-14 15:43:57 -04002659 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002660 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002661
Trond Myklebust0e574af2005-10-27 22:12:38 -04002662 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Andy Adamson26699402012-05-30 16:12:24 -04002664 /* Deal with open(O_TRUNC) */
2665 if (sattr->ia_valid & ATTR_OPEN)
2666 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2667
2668 /* Optimization: if the end result is no change, don't RPC */
2669 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2670 return 0;
2671
Trond Myklebustd5308382005-11-04 15:33:38 -05002672 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002673 if (sattr->ia_valid & ATTR_FILE) {
2674 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002675
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002676 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002677 if (ctx) {
2678 cred = ctx->cred;
2679 state = ctx->state;
2680 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002681 }
2682
2683 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002684 if (status == 0)
2685 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return status;
2687}
2688
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002689static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2690 const struct qstr *name, struct nfs_fh *fhandle,
2691 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002692{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002693 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002694 int status;
2695 struct nfs4_lookup_arg args = {
2696 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002697 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002698 .name = name,
2699 };
2700 struct nfs4_lookup_res res = {
2701 .server = server,
2702 .fattr = fattr,
2703 .fh = fhandle,
2704 };
2705 struct rpc_message msg = {
2706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2707 .rpc_argp = &args,
2708 .rpc_resp = &res,
2709 };
2710
2711 nfs_fattr_init(fattr);
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002714 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 dprintk("NFS reply lookup: %d\n", status);
2716 return status;
2717}
2718
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002719static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002720{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002721 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002722 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002723 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2724 fattr->nlink = 2;
2725}
2726
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002727static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2728 struct qstr *name, struct nfs_fh *fhandle,
2729 struct nfs_fattr *fattr)
2730{
2731 struct nfs4_exception exception = { };
2732 struct rpc_clnt *client = *clnt;
2733 int err;
2734 do {
2735 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2736 switch (err) {
2737 case -NFS4ERR_BADNAME:
2738 err = -ENOENT;
2739 goto out;
2740 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002741 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002742 goto out;
2743 case -NFS4ERR_WRONGSEC:
2744 err = -EPERM;
2745 if (client != *clnt)
2746 goto out;
2747
2748 client = nfs4_create_sec_client(client, dir, name);
2749 if (IS_ERR(client))
2750 return PTR_ERR(client);
2751
2752 exception.retry = 1;
2753 break;
2754 default:
2755 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2756 }
2757 } while (exception.retry);
2758
2759out:
2760 if (err == 0)
2761 *clnt = client;
2762 else if (client != *clnt)
2763 rpc_shutdown_client(client);
2764
2765 return err;
2766}
2767
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002768static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002769 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002771 int status;
2772 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002773
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002774 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2775 if (client != NFS_CLIENT(dir)) {
2776 rpc_shutdown_client(client);
2777 nfs_fixup_secinfo_attributes(fattr);
2778 }
2779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002782struct rpc_clnt *
2783nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2784 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2785{
2786 int status;
2787 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2788
2789 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2790 if (status < 0) {
2791 rpc_shutdown_client(client);
2792 return ERR_PTR(status);
2793 }
2794 return client;
2795}
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2798{
Trond Myklebust76b32992007-08-10 17:45:11 -04002799 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 struct nfs4_accessargs args = {
2801 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002802 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002804 struct nfs4_accessres res = {
2805 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002806 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 struct rpc_message msg = {
2808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2809 .rpc_argp = &args,
2810 .rpc_resp = &res,
2811 .rpc_cred = entry->cred,
2812 };
2813 int mode = entry->mask;
2814 int status;
2815
2816 /*
2817 * Determine which access bits we want to ask for...
2818 */
2819 if (mode & MAY_READ)
2820 args.access |= NFS4_ACCESS_READ;
2821 if (S_ISDIR(inode->i_mode)) {
2822 if (mode & MAY_WRITE)
2823 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2824 if (mode & MAY_EXEC)
2825 args.access |= NFS4_ACCESS_LOOKUP;
2826 } else {
2827 if (mode & MAY_WRITE)
2828 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2829 if (mode & MAY_EXEC)
2830 args.access |= NFS4_ACCESS_EXECUTE;
2831 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002832
2833 res.fattr = nfs_alloc_fattr();
2834 if (res.fattr == NULL)
2835 return -ENOMEM;
2836
Bryan Schumaker7c513052011-03-24 17:12:24 +00002837 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002839 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002840 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002842 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return status;
2844}
2845
2846static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2847{
2848 struct nfs4_exception exception = { };
2849 int err;
2850 do {
2851 err = nfs4_handle_exception(NFS_SERVER(inode),
2852 _nfs4_proc_access(inode, entry),
2853 &exception);
2854 } while (exception.retry);
2855 return err;
2856}
2857
2858/*
2859 * TODO: For the time being, we don't try to get any attributes
2860 * along with any of the zero-copy operations READ, READDIR,
2861 * READLINK, WRITE.
2862 *
2863 * In the case of the first three, we want to put the GETATTR
2864 * after the read-type operation -- this is because it is hard
2865 * to predict the length of a GETATTR response in v4, and thus
2866 * align the READ data correctly. This means that the GETATTR
2867 * may end up partially falling into the page cache, and we should
2868 * shift it into the 'tail' of the xdr_buf before processing.
2869 * To do this efficiently, we need to know the total length
2870 * of data received, which doesn't seem to be available outside
2871 * of the RPC layer.
2872 *
2873 * In the case of WRITE, we also want to put the GETATTR after
2874 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002875 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 *
2877 * Both of these changes to the XDR layer would in fact be quite
2878 * minor, but I decided to leave them for a subsequent patch.
2879 */
2880static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2881 unsigned int pgbase, unsigned int pglen)
2882{
2883 struct nfs4_readlink args = {
2884 .fh = NFS_FH(inode),
2885 .pgbase = pgbase,
2886 .pglen = pglen,
2887 .pages = &page,
2888 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002889 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 struct rpc_message msg = {
2891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2892 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002893 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 };
2895
Bryan Schumaker7c513052011-03-24 17:12:24 +00002896 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 -07002897}
2898
2899static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2900 unsigned int pgbase, unsigned int pglen)
2901{
2902 struct nfs4_exception exception = { };
2903 int err;
2904 do {
2905 err = nfs4_handle_exception(NFS_SERVER(inode),
2906 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2907 &exception);
2908 } while (exception.retry);
2909 return err;
2910}
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002913 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915static int
2916nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002917 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002919 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 int status = 0;
2922
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002923 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2924 if (IS_ERR(ctx))
2925 return PTR_ERR(ctx);
2926
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002927 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002928 state = nfs4_do_open(dir, dentry, ctx->mode,
2929 flags, sattr, ctx->cred,
2930 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002931 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (IS_ERR(state)) {
2933 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002936 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002937 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002938 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002940 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
2944static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2945{
Trond Myklebust16e42952005-10-27 22:12:44 -04002946 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002947 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002949 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002951 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002952 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002953 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2956 .rpc_argp = &args,
2957 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002959 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002960
Bryan Schumaker7c513052011-03-24 17:12:24 +00002961 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002962 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002963 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return status;
2965}
2966
2967static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2968{
2969 struct nfs4_exception exception = { };
2970 int err;
2971 do {
2972 err = nfs4_handle_exception(NFS_SERVER(dir),
2973 _nfs4_proc_remove(dir, name),
2974 &exception);
2975 } while (exception.retry);
2976 return err;
2977}
2978
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002979static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002981 struct nfs_server *server = NFS_SERVER(dir);
2982 struct nfs_removeargs *args = msg->rpc_argp;
2983 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002985 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002987 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002990static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2991{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002992 nfs4_setup_sequence(NFS_SERVER(data->dir),
2993 &data->args.seq_args,
2994 &data->res.seq_res,
2995 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002998static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003000 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3001
Trond Myklebust14516c32010-07-31 14:29:06 -04003002 if (!nfs4_sequence_done(task, &res->seq_res))
3003 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003004 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003005 return 0;
3006 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003007 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Jeff Laytond3d41522010-09-17 17:31:57 -04003010static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3011{
3012 struct nfs_server *server = NFS_SERVER(dir);
3013 struct nfs_renameargs *arg = msg->rpc_argp;
3014 struct nfs_renameres *res = msg->rpc_resp;
3015
3016 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003017 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003018 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003019}
3020
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003021static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3022{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003023 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3024 &data->args.seq_args,
3025 &data->res.seq_res,
3026 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003027}
3028
3029static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3030 struct inode *new_dir)
3031{
3032 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3033
3034 if (!nfs4_sequence_done(task, &res->seq_res))
3035 return 0;
3036 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3037 return 0;
3038
3039 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003040 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003041 return 1;
3042}
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3045 struct inode *new_dir, struct qstr *new_name)
3046{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003047 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003048 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 .old_dir = NFS_FH(old_dir),
3050 .new_dir = NFS_FH(new_dir),
3051 .old_name = old_name,
3052 .new_name = new_name,
3053 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003054 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003055 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct rpc_message msg = {
3058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3059 .rpc_argp = &arg,
3060 .rpc_resp = &res,
3061 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003062 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Bryan Schumaker7c513052011-03-24 17:12:24 +00003064 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!status) {
3066 update_changeattr(old_dir, &res.old_cinfo);
3067 update_changeattr(new_dir, &res.new_cinfo);
3068 }
3069 return status;
3070}
3071
3072static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3073 struct inode *new_dir, struct qstr *new_name)
3074{
3075 struct nfs4_exception exception = { };
3076 int err;
3077 do {
3078 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3079 _nfs4_proc_rename(old_dir, old_name,
3080 new_dir, new_name),
3081 &exception);
3082 } while (exception.retry);
3083 return err;
3084}
3085
3086static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3087{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003088 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 struct nfs4_link_arg arg = {
3090 .fh = NFS_FH(inode),
3091 .dir_fh = NFS_FH(dir),
3092 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003093 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003095 struct nfs4_link_res res = {
3096 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct rpc_message msg = {
3099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3100 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003101 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003103 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Trond Myklebust136f2622010-04-16 16:22:49 -04003105 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003106 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003107 goto out;
3108
Bryan Schumaker7c513052011-03-24 17:12:24 +00003109 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003110 if (!status) {
3111 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003112 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003113 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003114out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003115 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return status;
3117}
3118
3119static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3120{
3121 struct nfs4_exception exception = { };
3122 int err;
3123 do {
3124 err = nfs4_handle_exception(NFS_SERVER(inode),
3125 _nfs4_proc_link(inode, dir, name),
3126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003131struct nfs4_createdata {
3132 struct rpc_message msg;
3133 struct nfs4_create_arg arg;
3134 struct nfs4_create_res res;
3135 struct nfs_fh fh;
3136 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003137};
3138
3139static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3140 struct qstr *name, struct iattr *sattr, u32 ftype)
3141{
3142 struct nfs4_createdata *data;
3143
3144 data = kzalloc(sizeof(*data), GFP_KERNEL);
3145 if (data != NULL) {
3146 struct nfs_server *server = NFS_SERVER(dir);
3147
3148 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3149 data->msg.rpc_argp = &data->arg;
3150 data->msg.rpc_resp = &data->res;
3151 data->arg.dir_fh = NFS_FH(dir);
3152 data->arg.server = server;
3153 data->arg.name = name;
3154 data->arg.attrs = sattr;
3155 data->arg.ftype = ftype;
3156 data->arg.bitmask = server->attr_bitmask;
3157 data->res.server = server;
3158 data->res.fh = &data->fh;
3159 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003160 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003161 }
3162 return data;
3163}
3164
3165static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3166{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003167 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003168 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003169 if (status == 0) {
3170 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003171 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3172 }
3173 return status;
3174}
3175
3176static void nfs4_free_createdata(struct nfs4_createdata *data)
3177{
3178 kfree(data);
3179}
3180
Chuck Lever4f390c12006-08-22 20:06:22 -04003181static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003182 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003184 struct nfs4_createdata *data;
3185 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Chuck Lever94a6d752006-08-22 20:06:23 -04003187 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003188 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003189
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003190 status = -ENOMEM;
3191 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3192 if (data == NULL)
3193 goto out;
3194
3195 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3196 data->arg.u.symlink.pages = &page;
3197 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 status = nfs4_do_create(dir, dentry, data);
3200
3201 nfs4_free_createdata(data);
3202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return status;
3204}
3205
Chuck Lever4f390c12006-08-22 20:06:22 -04003206static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003207 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct nfs4_exception exception = { };
3210 int err;
3211 do {
3212 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003213 _nfs4_proc_symlink(dir, dentry, page,
3214 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 &exception);
3216 } while (exception.retry);
3217 return err;
3218}
3219
3220static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3221 struct iattr *sattr)
3222{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 struct nfs4_createdata *data;
3224 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003226 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3227 if (data == NULL)
3228 goto out;
3229
3230 status = nfs4_do_create(dir, dentry, data);
3231
3232 nfs4_free_createdata(data);
3233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return status;
3235}
3236
3237static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3238 struct iattr *sattr)
3239{
3240 struct nfs4_exception exception = { };
3241 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003242
3243 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 do {
3245 err = nfs4_handle_exception(NFS_SERVER(dir),
3246 _nfs4_proc_mkdir(dir, dentry, sattr),
3247 &exception);
3248 } while (exception.retry);
3249 return err;
3250}
3251
3252static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003253 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 struct inode *dir = dentry->d_inode;
3256 struct nfs4_readdir_arg args = {
3257 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003258 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 .pgbase = 0,
3260 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003261 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003262 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 };
3264 struct nfs4_readdir_res res;
3265 struct rpc_message msg = {
3266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3267 .rpc_argp = &args,
3268 .rpc_resp = &res,
3269 .rpc_cred = cred,
3270 };
3271 int status;
3272
Harvey Harrison3110ff82008-05-02 13:42:44 -07003273 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003274 dentry->d_parent->d_name.name,
3275 dentry->d_name.name,
3276 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003277 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003279 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 -05003280 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003281 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003282 status += args.pgbase;
3283 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003284
3285 nfs_invalidate_atime(dir);
3286
Harvey Harrison3110ff82008-05-02 13:42:44 -07003287 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return status;
3289}
3290
3291static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003292 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 struct nfs4_exception exception = { };
3295 int err;
3296 do {
3297 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3298 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003299 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
3305static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3306 struct iattr *sattr, dev_t rdev)
3307{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 struct nfs4_createdata *data;
3309 int mode = sattr->ia_mode;
3310 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003312 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3313 if (data == NULL)
3314 goto out;
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003317 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003319 data->arg.ftype = NF4BLK;
3320 data->arg.u.device.specdata1 = MAJOR(rdev);
3321 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003324 data->arg.ftype = NF4CHR;
3325 data->arg.u.device.specdata1 = MAJOR(rdev);
3326 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003327 } else if (!S_ISSOCK(mode)) {
3328 status = -EINVAL;
3329 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003332 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003333out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003334 nfs4_free_createdata(data);
3335out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return status;
3337}
3338
3339static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3340 struct iattr *sattr, dev_t rdev)
3341{
3342 struct nfs4_exception exception = { };
3343 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003344
3345 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 do {
3347 err = nfs4_handle_exception(NFS_SERVER(dir),
3348 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3349 &exception);
3350 } while (exception.retry);
3351 return err;
3352}
3353
3354static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3355 struct nfs_fsstat *fsstat)
3356{
3357 struct nfs4_statfs_arg args = {
3358 .fh = fhandle,
3359 .bitmask = server->attr_bitmask,
3360 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003361 struct nfs4_statfs_res res = {
3362 .fsstat = fsstat,
3363 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3366 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003367 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 };
3369
Trond Myklebust0e574af2005-10-27 22:12:38 -04003370 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003371 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
3374static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3375{
3376 struct nfs4_exception exception = { };
3377 int err;
3378 do {
3379 err = nfs4_handle_exception(server,
3380 _nfs4_proc_statfs(server, fhandle, fsstat),
3381 &exception);
3382 } while (exception.retry);
3383 return err;
3384}
3385
3386static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3387 struct nfs_fsinfo *fsinfo)
3388{
3389 struct nfs4_fsinfo_arg args = {
3390 .fh = fhandle,
3391 .bitmask = server->attr_bitmask,
3392 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003393 struct nfs4_fsinfo_res res = {
3394 .fsinfo = fsinfo,
3395 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 struct rpc_message msg = {
3397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3398 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003399 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 };
3401
Bryan Schumaker7c513052011-03-24 17:12:24 +00003402 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3406{
3407 struct nfs4_exception exception = { };
3408 int err;
3409
3410 do {
3411 err = nfs4_handle_exception(server,
3412 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3413 &exception);
3414 } while (exception.retry);
3415 return err;
3416}
3417
3418static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3419{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003420 int error;
3421
Trond Myklebust0e574af2005-10-27 22:12:38 -04003422 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003423 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003424 if (error == 0) {
3425 /* block layout checks this! */
3426 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003427 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003428 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003429
3430 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
3433static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3434 struct nfs_pathconf *pathconf)
3435{
3436 struct nfs4_pathconf_arg args = {
3437 .fh = fhandle,
3438 .bitmask = server->attr_bitmask,
3439 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003440 struct nfs4_pathconf_res res = {
3441 .pathconf = pathconf,
3442 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 struct rpc_message msg = {
3444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3445 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003446 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 };
3448
3449 /* None of the pathconf attributes are mandatory to implement */
3450 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3451 memset(pathconf, 0, sizeof(*pathconf));
3452 return 0;
3453 }
3454
Trond Myklebust0e574af2005-10-27 22:12:38 -04003455 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003456 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
3459static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3460 struct nfs_pathconf *pathconf)
3461{
3462 struct nfs4_exception exception = { };
3463 int err;
3464
3465 do {
3466 err = nfs4_handle_exception(server,
3467 _nfs4_proc_pathconf(server, fhandle, pathconf),
3468 &exception);
3469 } while (exception.retry);
3470 return err;
3471}
3472
Trond Myklebust5521abf2013-03-16 20:54:34 -04003473int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003474 const struct nfs_open_context *ctx,
3475 const struct nfs_lock_context *l_ctx,
3476 fmode_t fmode)
3477{
3478 const struct nfs_lockowner *lockowner = NULL;
3479
3480 if (l_ctx != NULL)
3481 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003482 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003483}
3484EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3485
Trond Myklebust5521abf2013-03-16 20:54:34 -04003486static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3487 const struct nfs_open_context *ctx,
3488 const struct nfs_lock_context *l_ctx,
3489 fmode_t fmode)
3490{
3491 nfs4_stateid current_stateid;
3492
3493 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3494 return false;
3495 return nfs4_stateid_match(stateid, &current_stateid);
3496}
3497
3498static bool nfs4_error_stateid_expired(int err)
3499{
3500 switch (err) {
3501 case -NFS4ERR_DELEG_REVOKED:
3502 case -NFS4ERR_ADMIN_REVOKED:
3503 case -NFS4ERR_BAD_STATEID:
3504 case -NFS4ERR_STALE_STATEID:
3505 case -NFS4ERR_OLD_STATEID:
3506 case -NFS4ERR_OPENMODE:
3507 case -NFS4ERR_EXPIRED:
3508 return true;
3509 }
3510 return false;
3511}
3512
Benny Halevyd20581a2011-05-22 19:52:03 +03003513void __nfs4_read_done_cb(struct nfs_read_data *data)
3514{
Fred Isamancd841602012-04-20 14:47:44 -04003515 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003516}
3517
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003518static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Fred Isamancd841602012-04-20 14:47:44 -04003520 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003522 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003523 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003524 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003526
Benny Halevyd20581a2011-05-22 19:52:03 +03003527 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003529 renew_lease(server, data->timestamp);
3530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Trond Myklebust5521abf2013-03-16 20:54:34 -04003533static bool nfs4_read_stateid_changed(struct rpc_task *task,
3534 struct nfs_readargs *args)
3535{
3536
3537 if (!nfs4_error_stateid_expired(task->tk_status) ||
3538 nfs4_stateid_is_current(&args->stateid,
3539 args->context,
3540 args->lock_context,
3541 FMODE_READ))
3542 return false;
3543 rpc_restart_call_prepare(task);
3544 return true;
3545}
3546
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003547static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3548{
3549
3550 dprintk("--> %s\n", __func__);
3551
3552 if (!nfs4_sequence_done(task, &data->res.seq_res))
3553 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003554 if (nfs4_read_stateid_changed(task, &data->args))
3555 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003556 return data->read_done_cb ? data->read_done_cb(task, data) :
3557 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003558}
3559
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003560static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003563 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003565 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003568static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3569{
Trond Myklebust9b206142013-03-17 15:52:00 -04003570 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003571 &data->args.seq_args,
3572 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003573 task))
3574 return;
3575 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3576 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Fred Isamanb029bc92011-03-03 15:13:42 +00003579static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Fred Isamancd841602012-04-20 14:47:44 -04003581 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003583 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003584 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003585 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003587 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003589 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003590 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Trond Myklebust5521abf2013-03-16 20:54:34 -04003594static bool nfs4_write_stateid_changed(struct rpc_task *task,
3595 struct nfs_writeargs *args)
3596{
3597
3598 if (!nfs4_error_stateid_expired(task->tk_status) ||
3599 nfs4_stateid_is_current(&args->stateid,
3600 args->context,
3601 args->lock_context,
3602 FMODE_WRITE))
3603 return false;
3604 rpc_restart_call_prepare(task);
3605 return true;
3606}
3607
Fred Isamanb029bc92011-03-03 15:13:42 +00003608static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3609{
3610 if (!nfs4_sequence_done(task, &data->res.seq_res))
3611 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003612 if (nfs4_write_stateid_changed(task, &data->args))
3613 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003614 return data->write_done_cb ? data->write_done_cb(task, data) :
3615 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003616}
3617
Trond Myklebust5a37f852012-04-28 14:55:16 -04003618static
3619bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003620{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003621 const struct nfs_pgio_header *hdr = data->header;
3622
3623 /* Don't request attributes for pNFS or O_DIRECT writes */
3624 if (data->ds_clp != NULL || hdr->dreq != NULL)
3625 return false;
3626 /* Otherwise, request attributes if and only if we don't hold
3627 * a delegation
3628 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003629 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003630}
Fred Isamana69aef12011-03-03 15:13:47 +00003631
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003632static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Fred Isamancd841602012-04-20 14:47:44 -04003634 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003635
Trond Myklebust5a37f852012-04-28 14:55:16 -04003636 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003637 data->args.bitmask = NULL;
3638 data->res.fattr = NULL;
3639 } else
3640 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003641
Fred Isamanb029bc92011-03-03 15:13:42 +00003642 if (!data->write_done_cb)
3643 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003644 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 data->timestamp = jiffies;
3646
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003647 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003648 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003651static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3652{
Trond Myklebust9b206142013-03-17 15:52:00 -04003653 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003654 &data->args.seq_args,
3655 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003656 task))
3657 return;
3658 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3659 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Fred Isaman0b7c0152012-04-20 14:47:39 -04003662static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3663{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003664 nfs4_setup_sequence(NFS_SERVER(data->inode),
3665 &data->args.seq_args,
3666 &data->res.seq_res,
3667 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
Fred Isaman0b7c0152012-04-20 14:47:39 -04003670static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003673
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003674 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003675 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003676 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Fred Isaman0b7c0152012-04-20 14:47:39 -04003681static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003682{
3683 if (!nfs4_sequence_done(task, &data->res.seq_res))
3684 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003685 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003686}
3687
Fred Isaman0b7c0152012-04-20 14:47:39 -04003688static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003690 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003691
Fred Isaman0b7c0152012-04-20 14:47:39 -04003692 if (data->commit_done_cb == NULL)
3693 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003694 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003695 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003696 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003699struct nfs4_renewdata {
3700 struct nfs_client *client;
3701 unsigned long timestamp;
3702};
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704/*
3705 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3706 * standalone procedure for queueing an asynchronous RENEW.
3707 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003708static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003709{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003710 struct nfs4_renewdata *data = calldata;
3711 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003712
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003713 if (atomic_read(&clp->cl_count) > 1)
3714 nfs4_schedule_state_renewal(clp);
3715 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003716 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003717}
3718
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003719static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003721 struct nfs4_renewdata *data = calldata;
3722 struct nfs_client *clp = data->client;
3723 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003726 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003727 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3728 return;
3729 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003730 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003731 return;
3732 }
3733 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003735 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003738static const struct rpc_call_ops nfs4_renew_ops = {
3739 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003740 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003741};
3742
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003743static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 struct rpc_message msg = {
3746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3747 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003748 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003750 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003752 if (renew_flags == 0)
3753 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003754 if (!atomic_inc_not_zero(&clp->cl_count))
3755 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003756 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757 if (data == NULL)
3758 return -ENOMEM;
3759 data->client = clp;
3760 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003762 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003765static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767 struct rpc_message msg = {
3768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3769 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003770 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 };
3772 unsigned long now = jiffies;
3773 int status;
3774
3775 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3776 if (status < 0)
3777 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003778 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return 0;
3780}
3781
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003782static inline int nfs4_server_supports_acls(struct nfs_server *server)
3783{
3784 return (server->caps & NFS_CAP_ACLS)
3785 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3786 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3787}
3788
Trond Myklebust21f498c2012-08-24 10:59:25 -04003789/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3790 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003791 * the stack.
3792 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003793#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003794
Neil Hormane9e3d722011-03-04 19:26:03 -05003795static int buf_to_pages_noslab(const void *buf, size_t buflen,
3796 struct page **pages, unsigned int *pgbase)
3797{
3798 struct page *newpage, **spages;
3799 int rc = 0;
3800 size_t len;
3801 spages = pages;
3802
3803 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003804 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003805 newpage = alloc_page(GFP_KERNEL);
3806
3807 if (newpage == NULL)
3808 goto unwind;
3809 memcpy(page_address(newpage), buf, len);
3810 buf += len;
3811 buflen -= len;
3812 *pages++ = newpage;
3813 rc++;
3814 } while (buflen != 0);
3815
3816 return rc;
3817
3818unwind:
3819 for(; rc > 0; rc--)
3820 __free_page(spages[rc-1]);
3821 return -ENOMEM;
3822}
3823
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824struct nfs4_cached_acl {
3825 int cached;
3826 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003827 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828};
3829
3830static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003831{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003832 struct nfs_inode *nfsi = NFS_I(inode);
3833
3834 spin_lock(&inode->i_lock);
3835 kfree(nfsi->nfs4_acl);
3836 nfsi->nfs4_acl = acl;
3837 spin_unlock(&inode->i_lock);
3838}
3839
3840static void nfs4_zap_acl_attr(struct inode *inode)
3841{
3842 nfs4_set_cached_acl(inode, NULL);
3843}
3844
3845static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3846{
3847 struct nfs_inode *nfsi = NFS_I(inode);
3848 struct nfs4_cached_acl *acl;
3849 int ret = -ENOENT;
3850
3851 spin_lock(&inode->i_lock);
3852 acl = nfsi->nfs4_acl;
3853 if (acl == NULL)
3854 goto out;
3855 if (buf == NULL) /* user is just asking for length */
3856 goto out_len;
3857 if (acl->cached == 0)
3858 goto out;
3859 ret = -ERANGE; /* see getxattr(2) man page */
3860 if (acl->len > buflen)
3861 goto out;
3862 memcpy(buf, acl->data, acl->len);
3863out_len:
3864 ret = acl->len;
3865out:
3866 spin_unlock(&inode->i_lock);
3867 return ret;
3868}
3869
Sachin Prabhu5794d212012-04-17 14:36:40 +01003870static 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 +00003871{
3872 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003873 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003874
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003875 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003876 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003877 if (acl == NULL)
3878 goto out;
3879 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003880 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003881 } else {
3882 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3883 if (acl == NULL)
3884 goto out;
3885 acl->cached = 0;
3886 }
3887 acl->len = acl_len;
3888out:
3889 nfs4_set_cached_acl(inode, acl);
3890}
3891
Andy Adamsonbf118a32011-12-07 11:55:27 -05003892/*
3893 * The getxattr API returns the required buffer length when called with a
3894 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3895 * the required buf. On a NULL buf, we send a page of data to the server
3896 * guessing that the ACL request can be serviced by a page. If so, we cache
3897 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3898 * the cache. If not so, we throw away the page, and cache the required
3899 * length. The next getxattr call will then produce another round trip to
3900 * the server, this time with the input buf of the required size.
3901 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003902static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003903{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003904 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003905 struct nfs_getaclargs args = {
3906 .fh = NFS_FH(inode),
3907 .acl_pages = pages,
3908 .acl_len = buflen,
3909 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003910 struct nfs_getaclres res = {
3911 .acl_len = buflen,
3912 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003913 struct rpc_message msg = {
3914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3915 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003916 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003917 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003918 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3919 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003920
Andy Adamsonbf118a32011-12-07 11:55:27 -05003921 /* As long as we're doing a round trip to the server anyway,
3922 * let's be prepared for a page of acl data. */
3923 if (npages == 0)
3924 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003925 if (npages > ARRAY_SIZE(pages))
3926 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003927
Andy Adamsonbf118a32011-12-07 11:55:27 -05003928 for (i = 0; i < npages; i++) {
3929 pages[i] = alloc_page(GFP_KERNEL);
3930 if (!pages[i])
3931 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003932 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003933
3934 /* for decoding across pages */
3935 res.acl_scratch = alloc_page(GFP_KERNEL);
3936 if (!res.acl_scratch)
3937 goto out_free;
3938
Andy Adamsonbf118a32011-12-07 11:55:27 -05003939 args.acl_len = npages * PAGE_SIZE;
3940 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003941
Peng Taode040be2012-01-10 22:42:47 +08003942 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003943 __func__, buf, buflen, npages, args.acl_len);
3944 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3945 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003946 if (ret)
3947 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003948
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003949 /* Handle the case where the passed-in buffer is too short */
3950 if (res.acl_flags & NFS4_ACL_TRUNC) {
3951 /* Did the user only issue a request for the acl length? */
3952 if (buf == NULL)
3953 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003954 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003955 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003956 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003957 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003958 if (buf) {
3959 if (res.acl_len > buflen) {
3960 ret = -ERANGE;
3961 goto out_free;
3962 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003963 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003964 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003965out_ok:
3966 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003967out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003968 for (i = 0; i < npages; i++)
3969 if (pages[i])
3970 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003971 if (res.acl_scratch)
3972 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973 return ret;
3974}
3975
Trond Myklebust16b42892006-08-24 12:27:15 -04003976static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3977{
3978 struct nfs4_exception exception = { };
3979 ssize_t ret;
3980 do {
3981 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3982 if (ret >= 0)
3983 break;
3984 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3985 } while (exception.retry);
3986 return ret;
3987}
3988
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003989static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3990{
3991 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003992 int ret;
3993
3994 if (!nfs4_server_supports_acls(server))
3995 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003996 ret = nfs_revalidate_inode(server, inode);
3997 if (ret < 0)
3998 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003999 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4000 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004001 ret = nfs4_read_cached_acl(inode, buf, buflen);
4002 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004003 /* -ENOENT is returned if there is no ACL or if there is an ACL
4004 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004005 return ret;
4006 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004007}
4008
Trond Myklebust16b42892006-08-24 12:27:15 -04004009static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004010{
4011 struct nfs_server *server = NFS_SERVER(inode);
4012 struct page *pages[NFS4ACL_MAXPAGES];
4013 struct nfs_setaclargs arg = {
4014 .fh = NFS_FH(inode),
4015 .acl_pages = pages,
4016 .acl_len = buflen,
4017 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004018 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004019 struct rpc_message msg = {
4020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4021 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004022 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004023 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004024 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004025 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004026
4027 if (!nfs4_server_supports_acls(server))
4028 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004029 if (npages > ARRAY_SIZE(pages))
4030 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004031 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4032 if (i < 0)
4033 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004034 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004035 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004036
4037 /*
4038 * Free each page after tx, so the only ref left is
4039 * held by the network stack
4040 */
4041 for (; i > 0; i--)
4042 put_page(pages[i-1]);
4043
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004044 /*
4045 * Acl update can result in inode attribute update.
4046 * so mark the attribute cache invalid.
4047 */
4048 spin_lock(&inode->i_lock);
4049 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4050 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004051 nfs_access_zap_cache(inode);
4052 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004053 return ret;
4054}
4055
Trond Myklebust16b42892006-08-24 12:27:15 -04004056static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4057{
4058 struct nfs4_exception exception = { };
4059 int err;
4060 do {
4061 err = nfs4_handle_exception(NFS_SERVER(inode),
4062 __nfs4_proc_set_acl(inode, buf, buflen),
4063 &exception);
4064 } while (exception.retry);
4065 return err;
4066}
4067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004069nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004071 struct nfs_client *clp = server->nfs_client;
4072
4073 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return 0;
4075 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004076 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004077 case -NFS4ERR_ADMIN_REVOKED:
4078 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004079 if (state == NULL)
4080 break;
4081 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004082 case -NFS4ERR_OPENMODE:
4083 if (state == NULL)
4084 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004085 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4086 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004087 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004088 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004089 if (state != NULL) {
4090 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4091 goto stateid_invalid;
4092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004094 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004095 nfs4_schedule_lease_recovery(clp);
4096 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004097#if defined(CONFIG_NFS_V4_1)
4098 case -NFS4ERR_BADSESSION:
4099 case -NFS4ERR_BADSLOT:
4100 case -NFS4ERR_BAD_HIGH_SLOT:
4101 case -NFS4ERR_DEADSESSION:
4102 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4103 case -NFS4ERR_SEQ_FALSE_RETRY:
4104 case -NFS4ERR_SEQ_MISORDERED:
4105 dprintk("%s ERROR %d, Reset session\n", __func__,
4106 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004107 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004108 task->tk_status = 0;
4109 return -EAGAIN;
4110#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004112 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004113 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4115 task->tk_status = 0;
4116 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004117 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 case -NFS4ERR_OLD_STATEID:
4119 task->tk_status = 0;
4120 return -EAGAIN;
4121 }
4122 task->tk_status = nfs4_map_errors(task->tk_status);
4123 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004124stateid_invalid:
4125 task->tk_status = -EIO;
4126 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004127wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004128 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004129 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4130 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4131 task->tk_status = 0;
4132 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Chuck Leverf0920752012-05-21 22:45:41 -04004135static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4136 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004137{
4138 __be32 verf[2];
4139
Chuck Lever2c820d92012-05-21 22:45:33 -04004140 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4141 /* An impossible timestamp guarantees this value
4142 * will never match a generated boot time. */
4143 verf[0] = 0;
4144 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4145 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004146 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4147 verf[0] = (__be32)nn->boot_time.tv_sec;
4148 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004149 }
Chuck Levercd937102012-03-02 17:14:31 -05004150 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4151}
4152
Chuck Levere984a552012-09-14 17:24:21 -04004153static unsigned int
4154nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4155 char *buf, size_t len)
4156{
4157 unsigned int result;
4158
4159 rcu_read_lock();
4160 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4161 clp->cl_ipaddr,
4162 rpc_peeraddr2str(clp->cl_rpcclient,
4163 RPC_DISPLAY_ADDR),
4164 rpc_peeraddr2str(clp->cl_rpcclient,
4165 RPC_DISPLAY_PROTO));
4166 rcu_read_unlock();
4167 return result;
4168}
4169
4170static unsigned int
4171nfs4_init_uniform_client_string(const struct nfs_client *clp,
4172 char *buf, size_t len)
4173{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004174 char *nodename = clp->cl_rpcclient->cl_nodename;
4175
4176 if (nfs4_client_id_uniquifier[0] != '\0')
4177 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004178 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4179 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004180 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004181}
4182
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004183/**
4184 * nfs4_proc_setclientid - Negotiate client ID
4185 * @clp: state data structure
4186 * @program: RPC program for NFSv4 callback service
4187 * @port: IP port number for NFS4 callback service
4188 * @cred: RPC credential to use for this call
4189 * @res: where to place the result
4190 *
4191 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4192 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004193int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4194 unsigned short port, struct rpc_cred *cred,
4195 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 nfs4_verifier sc_verifier;
4198 struct nfs4_setclientid setclientid = {
4199 .sc_verifier = &sc_verifier,
4200 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004201 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 };
4203 struct rpc_message msg = {
4204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4205 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004206 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004207 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004209 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Chuck Leverde734832012-07-11 16:30:50 -04004211 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004212 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004213 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4214 setclientid.sc_name_len =
4215 nfs4_init_uniform_client_string(clp,
4216 setclientid.sc_name,
4217 sizeof(setclientid.sc_name));
4218 else
4219 setclientid.sc_name_len =
4220 nfs4_init_nonuniform_client_string(clp,
4221 setclientid.sc_name,
4222 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004223 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004224 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004225 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004226 sizeof(setclientid.sc_netid),
4227 rpc_peeraddr2str(clp->cl_rpcclient,
4228 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004229 rcu_read_unlock();
4230 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004231 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 clp->cl_ipaddr, port >> 8, port & 255);
4233
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004234 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4235 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4236 setclientid.sc_name_len, setclientid.sc_name);
4237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4238 dprintk("NFS reply setclientid: %d\n", status);
4239 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004242/**
4243 * nfs4_proc_setclientid_confirm - Confirm client ID
4244 * @clp: state data structure
4245 * @res: result of a previous SETCLIENTID
4246 * @cred: RPC credential to use for this call
4247 *
4248 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4249 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004250int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004251 struct nfs4_setclientid_res *arg,
4252 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
4254 struct nfs_fsinfo fsinfo;
4255 struct rpc_message msg = {
4256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004257 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004259 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 };
4261 unsigned long now;
4262 int status;
4263
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004264 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4265 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4266 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004268 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (status == 0) {
4270 spin_lock(&clp->cl_lock);
4271 clp->cl_lease_time = fsinfo.lease_time * HZ;
4272 clp->cl_last_renewal = now;
4273 spin_unlock(&clp->cl_lock);
4274 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004275 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return status;
4277}
4278
Trond Myklebustfe650402006-01-03 09:55:18 +01004279struct nfs4_delegreturndata {
4280 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004281 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004282 struct nfs_fh fh;
4283 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004284 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004285 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004286 int rpc_status;
4287};
4288
Trond Myklebustfe650402006-01-03 09:55:18 +01004289static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4290{
4291 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004292
Trond Myklebust14516c32010-07-31 14:29:06 -04004293 if (!nfs4_sequence_done(task, &data->res.seq_res))
4294 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004295
Ricardo Labiaga79708862009-12-07 09:23:21 -05004296 switch (task->tk_status) {
4297 case -NFS4ERR_STALE_STATEID:
4298 case -NFS4ERR_EXPIRED:
4299 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004300 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004301 break;
4302 default:
4303 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4304 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004305 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004306 return;
4307 }
4308 }
4309 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004310}
4311
4312static void nfs4_delegreturn_release(void *calldata)
4313{
Trond Myklebustfe650402006-01-03 09:55:18 +01004314 kfree(calldata);
4315}
4316
Andy Adamson938e1012009-04-01 09:22:28 -04004317#if defined(CONFIG_NFS_V4_1)
4318static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4319{
4320 struct nfs4_delegreturndata *d_data;
4321
4322 d_data = (struct nfs4_delegreturndata *)data;
4323
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004324 nfs4_setup_sequence(d_data->res.server,
4325 &d_data->args.seq_args,
4326 &d_data->res.seq_res,
4327 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004328}
4329#endif /* CONFIG_NFS_V4_1 */
4330
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004331static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004332#if defined(CONFIG_NFS_V4_1)
4333 .rpc_call_prepare = nfs4_delegreturn_prepare,
4334#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004335 .rpc_call_done = nfs4_delegreturn_done,
4336 .rpc_release = nfs4_delegreturn_release,
4337};
4338
Trond Myklebuste6f81072008-01-24 18:14:34 -05004339static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004340{
4341 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004342 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004343 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004344 struct rpc_message msg = {
4345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4346 .rpc_cred = cred,
4347 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004348 struct rpc_task_setup task_setup_data = {
4349 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004350 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004351 .callback_ops = &nfs4_delegreturn_ops,
4352 .flags = RPC_TASK_ASYNC,
4353 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004354 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004355
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004356 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004357 if (data == NULL)
4358 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004359 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004360 data->args.fhandle = &data->fh;
4361 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004362 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004363 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004364 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004365 data->res.fattr = &data->fattr;
4366 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004367 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004368 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004369 data->rpc_status = 0;
4370
Trond Myklebustc970aa82007-07-14 15:39:59 -04004371 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004372 msg.rpc_argp = &data->args;
4373 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004374 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004375 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004376 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004377 if (!issync)
4378 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004379 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004380 if (status != 0)
4381 goto out;
4382 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004383 if (status == 0)
4384 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4385 else
4386 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004387out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004388 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
Trond Myklebuste6f81072008-01-24 18:14:34 -05004392int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
4394 struct nfs_server *server = NFS_SERVER(inode);
4395 struct nfs4_exception exception = { };
4396 int err;
4397 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004398 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 switch (err) {
4400 case -NFS4ERR_STALE_STATEID:
4401 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 case 0:
4403 return 0;
4404 }
4405 err = nfs4_handle_exception(server, err, &exception);
4406 } while (exception.retry);
4407 return err;
4408}
4409
4410#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4411#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4412
4413/*
4414 * sleep, with exponential backoff, and retry the LOCK operation.
4415 */
4416static unsigned long
4417nfs4_set_lock_task_retry(unsigned long timeout)
4418{
Jeff Laytond3103102011-12-01 22:44:39 +01004419 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 timeout <<= 1;
4421 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4422 return NFS4_LOCK_MAXTIMEOUT;
4423 return timeout;
4424}
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4427{
4428 struct inode *inode = state->inode;
4429 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004430 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004431 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004433 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004435 struct nfs_lockt_res res = {
4436 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 };
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4440 .rpc_argp = &arg,
4441 .rpc_resp = &res,
4442 .rpc_cred = state->owner->so_cred,
4443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 struct nfs4_lock_state *lsp;
4445 int status;
4446
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004447 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004448 status = nfs4_set_lock_state(state, request);
4449 if (status != 0)
4450 goto out;
4451 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004452 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004453 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004454 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004455 switch (status) {
4456 case 0:
4457 request->fl_type = F_UNLCK;
4458 break;
4459 case -NFS4ERR_DENIED:
4460 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004462 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return status;
4465}
4466
4467static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4468{
4469 struct nfs4_exception exception = { };
4470 int err;
4471
4472 do {
4473 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4474 _nfs4_proc_getlk(state, cmd, request),
4475 &exception);
4476 } while (exception.retry);
4477 return err;
4478}
4479
4480static int do_vfs_lock(struct file *file, struct file_lock *fl)
4481{
4482 int res = 0;
4483 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4484 case FL_POSIX:
4485 res = posix_lock_file_wait(file, fl);
4486 break;
4487 case FL_FLOCK:
4488 res = flock_lock_file_wait(file, fl);
4489 break;
4490 default:
4491 BUG();
4492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return res;
4494}
4495
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004496struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004497 struct nfs_locku_args arg;
4498 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004499 struct nfs4_lock_state *lsp;
4500 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004501 struct file_lock fl;
4502 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004503 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004504};
4505
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004506static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4507 struct nfs_open_context *ctx,
4508 struct nfs4_lock_state *lsp,
4509 struct nfs_seqid *seqid)
4510{
4511 struct nfs4_unlockdata *p;
4512 struct inode *inode = lsp->ls_state->inode;
4513
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004514 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004515 if (p == NULL)
4516 return NULL;
4517 p->arg.fh = NFS_FH(inode);
4518 p->arg.fl = &p->fl;
4519 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004520 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004521 p->arg.stateid = &lsp->ls_stateid;
4522 p->lsp = lsp;
4523 atomic_inc(&lsp->ls_count);
4524 /* Ensure we don't close file until we're done freeing locks! */
4525 p->ctx = get_nfs_open_context(ctx);
4526 memcpy(&p->fl, fl, sizeof(p->fl));
4527 p->server = NFS_SERVER(inode);
4528 return p;
4529}
4530
Trond Myklebust06f814a2006-01-03 09:55:07 +01004531static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004532{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004533 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004535 nfs4_put_lock_state(calldata->lsp);
4536 put_nfs_open_context(calldata->ctx);
4537 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538}
4539
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004540static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004541{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004542 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004543
Trond Myklebust14516c32010-07-31 14:29:06 -04004544 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4545 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004546 switch (task->tk_status) {
4547 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004548 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4549 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004550 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004551 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004552 case -NFS4ERR_BAD_STATEID:
4553 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004554 case -NFS4ERR_STALE_STATEID:
4555 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004556 break;
4557 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004558 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004559 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004560 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004561 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004562}
4563
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004564static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004565{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004566 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004568 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004569 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004570 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004571 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004572 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004573 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004574 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004575 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004576 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004577 &calldata->res.seq_res,
4578 task) != 0)
4579 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004580 return;
4581out_no_action:
4582 task->tk_action = NULL;
4583out_wait:
4584 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
4586
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004587static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004588 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004589 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004590 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004591};
4592
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4594 struct nfs_open_context *ctx,
4595 struct nfs4_lock_state *lsp,
4596 struct nfs_seqid *seqid)
4597{
4598 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4601 .rpc_cred = ctx->cred,
4602 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004603 struct rpc_task_setup task_setup_data = {
4604 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004605 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004607 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004608 .flags = RPC_TASK_ASYNC,
4609 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610
Frank Filz137d6ac2007-07-09 15:32:29 -07004611 /* Ensure this is an unlock - when canceling a lock, the
4612 * canceled lock is passed in, and it won't be an unlock.
4613 */
4614 fl->fl_type = F_UNLCK;
4615
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4617 if (data == NULL) {
4618 nfs_free_seqid(seqid);
4619 return ERR_PTR(-ENOMEM);
4620 }
4621
Trond Myklebust9d12b212012-01-17 22:04:25 -05004622 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004623 msg.rpc_argp = &data->arg;
4624 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004625 task_setup_data.callback_data = data;
4626 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627}
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4630{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004631 struct inode *inode = state->inode;
4632 struct nfs4_state_owner *sp = state->owner;
4633 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004634 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004635 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004636 struct rpc_task *task;
4637 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004638 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Trond Myklebust9b073572006-06-29 16:38:34 -04004640 status = nfs4_set_lock_state(state, request);
4641 /* Unlock _before_ we do the RPC call */
4642 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004643 /* Exclude nfs_delegation_claim_locks() */
4644 mutex_lock(&sp->so_delegreturn_mutex);
4645 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004646 down_read(&nfsi->rwsem);
4647 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4648 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004649 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004650 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004651 }
4652 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004653 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004654 if (status != 0)
4655 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004656 /* Is this a delegated lock? */
4657 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004658 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004659 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004660 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004661 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004662 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004663 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004664 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004665 status = PTR_ERR(task);
4666 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004667 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004669 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004670out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004671 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004672 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673}
4674
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004675struct nfs4_lockdata {
4676 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004677 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004678 struct nfs4_lock_state *lsp;
4679 struct nfs_open_context *ctx;
4680 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004681 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004682 int rpc_status;
4683 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004684 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685};
4686
4687static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004688 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4689 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004690{
4691 struct nfs4_lockdata *p;
4692 struct inode *inode = lsp->ls_state->inode;
4693 struct nfs_server *server = NFS_SERVER(inode);
4694
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004695 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 if (p == NULL)
4697 return NULL;
4698
4699 p->arg.fh = NFS_FH(inode);
4700 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004701 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004702 if (p->arg.open_seqid == NULL)
4703 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004704 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004705 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004706 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004708 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004709 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004710 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004711 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004712 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004713 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714 atomic_inc(&lsp->ls_count);
4715 p->ctx = get_nfs_open_context(ctx);
4716 memcpy(&p->fl, fl, sizeof(p->fl));
4717 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004718out_free_seqid:
4719 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004720out_free:
4721 kfree(p);
4722 return NULL;
4723}
4724
4725static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4726{
4727 struct nfs4_lockdata *data = calldata;
4728 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Harvey Harrison3110ff82008-05-02 13:42:44 -07004730 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004731 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004732 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733 /* Do we need to do an open_to_lock_owner? */
4734 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004735 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004736 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004737 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004738 data->arg.open_stateid = &state->stateid;
4739 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004740 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004741 } else
4742 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004743 if (!nfs4_valid_open_stateid(state)) {
4744 data->rpc_status = -EBADF;
4745 task->tk_action = NULL;
4746 goto out_release_open_seqid;
4747 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004748 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004749 if (nfs4_setup_sequence(data->server,
4750 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004751 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004752 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004753 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004754out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004755 nfs_release_seqid(data->arg.open_seqid);
4756out_release_lock_seqid:
4757 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004758out_wait:
4759 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004760 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004761}
4762
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4764{
4765 struct nfs4_lockdata *data = calldata;
4766
Harvey Harrison3110ff82008-05-02 13:42:44 -07004767 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004768
Trond Myklebust14516c32010-07-31 14:29:06 -04004769 if (!nfs4_sequence_done(task, &data->res.seq_res))
4770 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004771
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004773 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004774 if (data->rpc_status == 0)
4775 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4776 else
4777 goto out;
4778 }
4779 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004780 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004781 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004782 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004783 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004784out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004785 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004786}
4787
4788static void nfs4_lock_release(void *calldata)
4789{
4790 struct nfs4_lockdata *data = calldata;
4791
Harvey Harrison3110ff82008-05-02 13:42:44 -07004792 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004793 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004794 if (data->cancelled != 0) {
4795 struct rpc_task *task;
4796 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4797 data->arg.lock_seqid);
4798 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004799 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004800 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004801 } else
4802 nfs_free_seqid(data->arg.lock_seqid);
4803 nfs4_put_lock_state(data->lsp);
4804 put_nfs_open_context(data->ctx);
4805 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004806 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004807}
4808
4809static const struct rpc_call_ops nfs4_lock_ops = {
4810 .rpc_call_prepare = nfs4_lock_prepare,
4811 .rpc_call_done = nfs4_lock_done,
4812 .rpc_release = nfs4_lock_release,
4813};
4814
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004815static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4816{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004817 switch (error) {
4818 case -NFS4ERR_ADMIN_REVOKED:
4819 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004820 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004821 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004822 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004823 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004824 break;
4825 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004826 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004827 case -NFS4ERR_EXPIRED:
4828 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004829 };
4830}
4831
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004832static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004833{
4834 struct nfs4_lockdata *data;
4835 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004836 struct rpc_message msg = {
4837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4838 .rpc_cred = state->owner->so_cred,
4839 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004840 struct rpc_task_setup task_setup_data = {
4841 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004842 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004843 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004844 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004845 .flags = RPC_TASK_ASYNC,
4846 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847 int ret;
4848
Harvey Harrison3110ff82008-05-02 13:42:44 -07004849 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004850 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004851 fl->fl_u.nfs4_fl.owner,
4852 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004853 if (data == NULL)
4854 return -ENOMEM;
4855 if (IS_SETLKW(cmd))
4856 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004857 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004858 msg.rpc_argp = &data->arg;
4859 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004860 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004861 if (recovery_type > NFS_LOCK_NEW) {
4862 if (recovery_type == NFS_LOCK_RECLAIM)
4863 data->arg.reclaim = NFS_LOCK_RECLAIM;
4864 nfs4_set_sequence_privileged(&data->arg.seq_args);
4865 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004866 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004867 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004868 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004869 ret = nfs4_wait_for_completion_rpc_task(task);
4870 if (ret == 0) {
4871 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004872 if (ret)
4873 nfs4_handle_setlk_error(data->server, data->lsp,
4874 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004875 } else
4876 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004877 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004878 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
4881
4882static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4883{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004884 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004885 struct nfs4_exception exception = {
4886 .inode = state->inode,
4887 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004888 int err;
4889
4890 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004891 /* Cache the lock if possible... */
4892 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4893 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004894 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004895 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004896 break;
4897 nfs4_handle_exception(server, err, &exception);
4898 } while (exception.retry);
4899 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
4902static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4903{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004904 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004905 struct nfs4_exception exception = {
4906 .inode = state->inode,
4907 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004908 int err;
4909
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004910 err = nfs4_set_lock_state(state, request);
4911 if (err != 0)
4912 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004913 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004914 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4915 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004916 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004917 switch (err) {
4918 default:
4919 goto out;
4920 case -NFS4ERR_GRACE:
4921 case -NFS4ERR_DELAY:
4922 nfs4_handle_exception(server, err, &exception);
4923 err = 0;
4924 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004925 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004926out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004930#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004931/**
4932 * nfs41_check_expired_locks - possibly free a lock stateid
4933 *
4934 * @state: NFSv4 state for an inode
4935 *
4936 * Returns NFS_OK if recovery for this stateid is now finished.
4937 * Otherwise a negative NFS4ERR value is returned.
4938 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004939static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004940{
Chuck Levereb64cf92012-07-11 16:30:05 -04004941 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004942 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004943 struct nfs_server *server = NFS_SERVER(state->inode);
4944
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004945 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004946 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004947 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4948 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004949 /* Free the stateid unless the server
4950 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004951 if (status != -NFS4ERR_BAD_STATEID)
4952 nfs41_free_stateid(server,
4953 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004954 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004955 ret = status;
4956 }
4957 }
4958 };
4959
4960 return ret;
4961}
4962
4963static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4964{
4965 int status = NFS_OK;
4966
4967 if (test_bit(LK_STATE_IN_USE, &state->flags))
4968 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004969 if (status != NFS_OK)
4970 status = nfs4_lock_expired(state, request);
4971 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004972}
4973#endif
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4976{
Trond Myklebust9a99af42013-02-04 20:17:49 -05004977 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004978 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004979 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05004980 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004981 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004983 if ((fl_flags & FL_POSIX) &&
4984 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4985 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004986 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004987 status = nfs4_set_lock_state(state, request);
4988 if (status != 0)
4989 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004990 request->fl_flags |= FL_ACCESS;
4991 status = do_vfs_lock(request->fl_file, request);
4992 if (status < 0)
4993 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004994 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004995 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004996 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004997 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004998 request->fl_flags = fl_flags & ~FL_SLEEP;
4999 status = do_vfs_lock(request->fl_file, request);
5000 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005001 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005002 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5003 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005004 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005005 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005006 goto out;
5007 down_read(&nfsi->rwsem);
5008 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5009 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005010 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005011 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005012 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005013 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005014 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005015 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5016 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005017out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005018 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005019out:
5020 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 return status;
5022}
5023
5024static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5025{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005026 struct nfs4_exception exception = {
5027 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005028 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 int err;
5031
5032 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005033 err = _nfs4_proc_setlk(state, cmd, request);
5034 if (err == -NFS4ERR_DENIED)
5035 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005037 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 } while (exception.retry);
5039 return err;
5040}
5041
5042static int
5043nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5044{
5045 struct nfs_open_context *ctx;
5046 struct nfs4_state *state;
5047 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5048 int status;
5049
5050 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005051 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 state = ctx->state;
5053
5054 if (request->fl_start < 0 || request->fl_end < 0)
5055 return -EINVAL;
5056
Trond Myklebustd9531262009-07-21 19:22:38 -04005057 if (IS_GETLK(cmd)) {
5058 if (state != NULL)
5059 return nfs4_proc_getlk(state, F_GETLK, request);
5060 return 0;
5061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5064 return -EINVAL;
5065
Trond Myklebustd9531262009-07-21 19:22:38 -04005066 if (request->fl_type == F_UNLCK) {
5067 if (state != NULL)
5068 return nfs4_proc_unlck(state, cmd, request);
5069 return 0;
5070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
Trond Myklebustd9531262009-07-21 19:22:38 -04005072 if (state == NULL)
5073 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005074 /*
5075 * Don't rely on the VFS having checked the file open mode,
5076 * since it won't do this for flock() locks.
5077 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005078 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005079 case F_RDLCK:
5080 if (!(filp->f_mode & FMODE_READ))
5081 return -EBADF;
5082 break;
5083 case F_WRLCK:
5084 if (!(filp->f_mode & FMODE_WRITE))
5085 return -EBADF;
5086 }
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 do {
5089 status = nfs4_proc_setlk(state, cmd, request);
5090 if ((status != -EAGAIN) || IS_SETLK(cmd))
5091 break;
5092 timeout = nfs4_set_lock_task_retry(timeout);
5093 status = -ERESTARTSYS;
5094 if (signalled())
5095 break;
5096 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 return status;
5098}
5099
Trond Myklebust888e6942005-11-04 15:38:11 -05005100int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5101{
5102 struct nfs_server *server = NFS_SERVER(state->inode);
5103 struct nfs4_exception exception = { };
5104 int err;
5105
5106 err = nfs4_set_lock_state(state, fl);
5107 if (err != 0)
5108 goto out;
5109 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005110 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005111 switch (err) {
5112 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005113 printk(KERN_ERR "NFS: %s: unhandled error "
5114 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005115 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005116 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005117 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005118 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005119 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005120 set_bit(NFS_DELEGATED_STATE, &state->flags);
5121 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005122 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005123 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005124 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005125 case -NFS4ERR_BADSESSION:
5126 case -NFS4ERR_BADSLOT:
5127 case -NFS4ERR_BAD_HIGH_SLOT:
5128 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5129 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005130 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005131 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005132 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005133 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005134 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005135 case -NFS4ERR_ADMIN_REVOKED:
5136 case -NFS4ERR_BAD_STATEID:
5137 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005138 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005139 err = 0;
5140 goto out;
5141 case -ENOMEM:
5142 case -NFS4ERR_DENIED:
5143 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5144 err = 0;
5145 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005146 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005147 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005148 err = nfs4_handle_exception(server, err, &exception);
5149 } while (exception.retry);
5150out:
5151 return err;
5152}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005153
Trond Myklebustcf470c32012-03-07 13:49:12 -05005154struct nfs_release_lockowner_data {
5155 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005156 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005157 struct nfs_release_lockowner_args args;
5158};
5159
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005160static void nfs4_release_lockowner_release(void *calldata)
5161{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005162 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005163 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005164 kfree(calldata);
5165}
5166
Trond Myklebust17280172012-03-11 13:11:00 -04005167static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005168 .rpc_release = nfs4_release_lockowner_release,
5169};
5170
Trond Myklebustcf470c32012-03-07 13:49:12 -05005171int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005172{
5173 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005174 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005175 struct rpc_message msg = {
5176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5177 };
5178
5179 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005180 return -EINVAL;
5181 data = kmalloc(sizeof(*data), GFP_NOFS);
5182 if (!data)
5183 return -ENOMEM;
5184 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005185 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005186 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5187 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5188 data->args.lock_owner.s_dev = server->s_dev;
5189 msg.rpc_argp = &data->args;
5190 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5191 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005192}
5193
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005194#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5195
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005196static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5197 const void *buf, size_t buflen,
5198 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005199{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005200 if (strcmp(key, "") != 0)
5201 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005202
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005203 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005204}
5205
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005206static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5207 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005208{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005209 if (strcmp(key, "") != 0)
5210 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005211
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005212 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005213}
5214
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005215static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5216 size_t list_len, const char *name,
5217 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005218{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005219 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005220
J. Bruce Fields096455a2006-03-20 23:23:42 -05005221 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5222 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005223
5224 if (list && len <= list_len)
5225 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005226 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005227}
5228
Andy Adamson533eb462011-06-13 18:25:56 -04005229/*
5230 * nfs_fhget will use either the mounted_on_fileid or the fileid
5231 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005232static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5233{
Andy Adamson533eb462011-06-13 18:25:56 -04005234 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5235 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5236 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005237 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005238 return;
5239
5240 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005241 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005242 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5243 fattr->nlink = 2;
5244}
5245
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005246static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5247 const struct qstr *name,
5248 struct nfs4_fs_locations *fs_locations,
5249 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005250{
5251 struct nfs_server *server = NFS_SERVER(dir);
5252 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005253 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005254 };
5255 struct nfs4_fs_locations_arg args = {
5256 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005257 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005258 .page = page,
5259 .bitmask = bitmask,
5260 };
Benny Halevy22958462009-04-01 09:22:02 -04005261 struct nfs4_fs_locations_res res = {
5262 .fs_locations = fs_locations,
5263 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5266 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005267 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005268 };
5269 int status;
5270
Harvey Harrison3110ff82008-05-02 13:42:44 -07005271 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005272
5273 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5274 * is not supported */
5275 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5276 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5277 else
5278 bitmask[0] |= FATTR4_WORD0_FILEID;
5279
Trond Myklebustc228fd32007-01-13 02:28:11 -05005280 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005281 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005282 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005283 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005284 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005285 return status;
5286}
5287
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005288int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5289 const struct qstr *name,
5290 struct nfs4_fs_locations *fs_locations,
5291 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005292{
5293 struct nfs4_exception exception = { };
5294 int err;
5295 do {
5296 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005297 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005298 &exception);
5299 } while (exception.retry);
5300 return err;
5301}
5302
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005303static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5304{
5305 int status;
5306 struct nfs4_secinfo_arg args = {
5307 .dir_fh = NFS_FH(dir),
5308 .name = name,
5309 };
5310 struct nfs4_secinfo_res res = {
5311 .flavors = flavors,
5312 };
5313 struct rpc_message msg = {
5314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5315 .rpc_argp = &args,
5316 .rpc_resp = &res,
5317 };
5318
5319 dprintk("NFS call secinfo %s\n", name->name);
5320 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5321 dprintk("NFS reply secinfo: %d\n", status);
5322 return status;
5323}
5324
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005325int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5326 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005327{
5328 struct nfs4_exception exception = { };
5329 int err;
5330 do {
5331 err = nfs4_handle_exception(NFS_SERVER(dir),
5332 _nfs4_proc_secinfo(dir, name, flavors),
5333 &exception);
5334 } while (exception.retry);
5335 return err;
5336}
5337
Andy Adamson557134a2009-04-01 09:21:53 -04005338#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005339/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005340 * Check the exchange flags returned by the server for invalid flags, having
5341 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5342 * DS flags set.
5343 */
5344static int nfs4_check_cl_exchange_flags(u32 flags)
5345{
5346 if (flags & ~EXCHGID4_FLAG_MASK_R)
5347 goto out_inval;
5348 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5349 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5350 goto out_inval;
5351 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5352 goto out_inval;
5353 return NFS_OK;
5354out_inval:
5355 return -NFS4ERR_INVAL;
5356}
5357
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005358static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005359nfs41_same_server_scope(struct nfs41_server_scope *a,
5360 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005361{
5362 if (a->server_scope_sz == b->server_scope_sz &&
5363 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5364 return true;
5365
5366 return false;
5367}
5368
Andy Adamson357f54d2010-12-14 10:11:57 -05005369/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005370 * nfs4_proc_bind_conn_to_session()
5371 *
5372 * The 4.1 client currently uses the same TCP connection for the
5373 * fore and backchannel.
5374 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005375int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005376{
5377 int status;
5378 struct nfs41_bind_conn_to_session_res res;
5379 struct rpc_message msg = {
5380 .rpc_proc =
5381 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5382 .rpc_argp = clp,
5383 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005384 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005385 };
5386
5387 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005388
5389 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5390 if (unlikely(res.session == NULL)) {
5391 status = -ENOMEM;
5392 goto out;
5393 }
5394
5395 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5396 if (status == 0) {
5397 if (memcmp(res.session->sess_id.data,
5398 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5399 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5400 status = -EIO;
5401 goto out_session;
5402 }
5403 if (res.dir != NFS4_CDFS4_BOTH) {
5404 dprintk("NFS: %s: Unexpected direction from server\n",
5405 __func__);
5406 status = -EIO;
5407 goto out_session;
5408 }
5409 if (res.use_conn_in_rdma_mode) {
5410 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5411 __func__);
5412 status = -EIO;
5413 goto out_session;
5414 }
5415 }
5416out_session:
5417 kfree(res.session);
5418out:
5419 dprintk("<-- %s status= %d\n", __func__, status);
5420 return status;
5421}
5422
5423/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005424 * nfs4_proc_exchange_id()
5425 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005426 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5427 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005428 * Since the clientid has expired, all compounds using sessions
5429 * associated with the stale clientid will be returning
5430 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5431 * be in some phase of session reset.
5432 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005433int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005434{
5435 nfs4_verifier verifier;
5436 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005437 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005438 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005439 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005440 };
5441 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005442 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005443 };
5444 int status;
5445 struct rpc_message msg = {
5446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5447 .rpc_argp = &args,
5448 .rpc_resp = &res,
5449 .rpc_cred = cred,
5450 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005451
Chuck Leverf0920752012-05-21 22:45:41 -04005452 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005453 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5454 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005455 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5456 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5457 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005458
Chuck Leveracdeb692012-05-21 22:46:16 -04005459 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005460 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005461 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005462 status = -ENOMEM;
5463 goto out;
5464 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005465
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005466 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005467 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005468 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005469 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005470 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005471 }
5472
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005473 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005474 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005475 status = -ENOMEM;
5476 goto out_server_scope;
5477 }
5478
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005479 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005480 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005481 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005482
Chuck Lever177313f2012-05-21 22:44:58 -04005483 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005484 clp->cl_clientid = res.clientid;
5485 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5486 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5487 clp->cl_seqid = res.seqid;
5488
Chuck Leveracdeb692012-05-21 22:46:16 -04005489 kfree(clp->cl_serverowner);
5490 clp->cl_serverowner = res.server_owner;
5491 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005492
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005493 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005494 kfree(clp->cl_implid);
5495 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005496
Chuck Lever177313f2012-05-21 22:44:58 -04005497 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005498 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005499 res.server_scope)) {
5500 dprintk("%s: server_scope mismatch detected\n",
5501 __func__);
5502 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005503 kfree(clp->cl_serverscope);
5504 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005505 }
5506
Chuck Lever177313f2012-05-21 22:44:58 -04005507 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005508 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005509 goto out;
5510 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005511 } else
5512 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005513
Chuck Leveracdeb692012-05-21 22:46:16 -04005514out_server_owner:
5515 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005516out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005517 kfree(res.server_scope);
5518out:
Chuck Lever177313f2012-05-21 22:44:58 -04005519 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005520 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005521 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005522 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005523 clp->cl_implid->date.seconds,
5524 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005525 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005526 return status;
5527}
5528
Trond Myklebust66245532012-05-25 17:18:09 -04005529static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5530 struct rpc_cred *cred)
5531{
5532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5534 .rpc_argp = clp,
5535 .rpc_cred = cred,
5536 };
5537 int status;
5538
5539 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5540 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005541 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005542 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5543 return status;
5544}
5545
5546static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5547 struct rpc_cred *cred)
5548{
5549 unsigned int loop;
5550 int ret;
5551
5552 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5553 ret = _nfs4_proc_destroy_clientid(clp, cred);
5554 switch (ret) {
5555 case -NFS4ERR_DELAY:
5556 case -NFS4ERR_CLIENTID_BUSY:
5557 ssleep(1);
5558 break;
5559 default:
5560 return ret;
5561 }
5562 }
5563 return 0;
5564}
5565
5566int nfs4_destroy_clientid(struct nfs_client *clp)
5567{
5568 struct rpc_cred *cred;
5569 int ret = 0;
5570
5571 if (clp->cl_mvops->minor_version < 1)
5572 goto out;
5573 if (clp->cl_exchange_flags == 0)
5574 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005575 if (clp->cl_preserve_clid)
5576 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005577 cred = nfs4_get_exchange_id_cred(clp);
5578 ret = nfs4_proc_destroy_clientid(clp, cred);
5579 if (cred)
5580 put_rpccred(cred);
5581 switch (ret) {
5582 case 0:
5583 case -NFS4ERR_STALE_CLIENTID:
5584 clp->cl_exchange_flags = 0;
5585 }
5586out:
5587 return ret;
5588}
5589
Andy Adamson2050f0c2009-04-01 09:22:30 -04005590struct nfs4_get_lease_time_data {
5591 struct nfs4_get_lease_time_args *args;
5592 struct nfs4_get_lease_time_res *res;
5593 struct nfs_client *clp;
5594};
5595
5596static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5597 void *calldata)
5598{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005599 struct nfs4_get_lease_time_data *data =
5600 (struct nfs4_get_lease_time_data *)calldata;
5601
5602 dprintk("--> %s\n", __func__);
5603 /* just setup sequence, do not trigger session recovery
5604 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005605 nfs41_setup_sequence(data->clp->cl_session,
5606 &data->args->la_seq_args,
5607 &data->res->lr_seq_res,
5608 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005609 dprintk("<-- %s\n", __func__);
5610}
5611
5612/*
5613 * Called from nfs4_state_manager thread for session setup, so don't recover
5614 * from sequence operation or clientid errors.
5615 */
5616static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5617{
5618 struct nfs4_get_lease_time_data *data =
5619 (struct nfs4_get_lease_time_data *)calldata;
5620
5621 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005622 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5623 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005624 switch (task->tk_status) {
5625 case -NFS4ERR_DELAY:
5626 case -NFS4ERR_GRACE:
5627 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5628 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5629 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005630 /* fall through */
5631 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005632 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005633 return;
5634 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005635 dprintk("<-- %s\n", __func__);
5636}
5637
Trond Myklebust17280172012-03-11 13:11:00 -04005638static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005639 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5640 .rpc_call_done = nfs4_get_lease_time_done,
5641};
5642
5643int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5644{
5645 struct rpc_task *task;
5646 struct nfs4_get_lease_time_args args;
5647 struct nfs4_get_lease_time_res res = {
5648 .lr_fsinfo = fsinfo,
5649 };
5650 struct nfs4_get_lease_time_data data = {
5651 .args = &args,
5652 .res = &res,
5653 .clp = clp,
5654 };
5655 struct rpc_message msg = {
5656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5657 .rpc_argp = &args,
5658 .rpc_resp = &res,
5659 };
5660 struct rpc_task_setup task_setup = {
5661 .rpc_client = clp->cl_rpcclient,
5662 .rpc_message = &msg,
5663 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005664 .callback_data = &data,
5665 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005666 };
5667 int status;
5668
Trond Myklebust9d12b212012-01-17 22:04:25 -05005669 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005670 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005671 dprintk("--> %s\n", __func__);
5672 task = rpc_run_task(&task_setup);
5673
5674 if (IS_ERR(task))
5675 status = PTR_ERR(task);
5676 else {
5677 status = task->tk_status;
5678 rpc_put_task(task);
5679 }
5680 dprintk("<-- %s return %d\n", __func__, status);
5681
5682 return status;
5683}
5684
Andy Adamsonfc931582009-04-01 09:22:31 -04005685/*
5686 * Initialize the values to be used by the client in CREATE_SESSION
5687 * If nfs4_init_session set the fore channel request and response sizes,
5688 * use them.
5689 *
5690 * Set the back channel max_resp_sz_cached to zero to force the client to
5691 * always set csa_cachethis to FALSE because the current implementation
5692 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5693 */
5694static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5695{
5696 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005697 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5698 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005699
5700 if (mxrqst_sz == 0)
5701 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5702 if (mxresp_sz == 0)
5703 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5704 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005705 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5706 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005707 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005708 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005709
5710 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005711 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005712 __func__,
5713 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005714 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005715
5716 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005717 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5718 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5719 args->bc_attrs.max_resp_sz_cached = 0;
5720 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5721 args->bc_attrs.max_reqs = 1;
5722
5723 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5724 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5725 __func__,
5726 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5727 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5728 args->bc_attrs.max_reqs);
5729}
5730
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005731static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005732{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005733 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5734 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5735
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005736 if (rcvd->max_resp_sz > sent->max_resp_sz)
5737 return -EINVAL;
5738 /*
5739 * Our requested max_ops is the minimum we need; we're not
5740 * prepared to break up compounds into smaller pieces than that.
5741 * So, no point even trying to continue if the server won't
5742 * cooperate:
5743 */
5744 if (rcvd->max_ops < sent->max_ops)
5745 return -EINVAL;
5746 if (rcvd->max_reqs == 0)
5747 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005748 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5749 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005750 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005751}
5752
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005753static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5754{
5755 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5756 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005757
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005758 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5759 return -EINVAL;
5760 if (rcvd->max_resp_sz < sent->max_resp_sz)
5761 return -EINVAL;
5762 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5763 return -EINVAL;
5764 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005765 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005766 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005767 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005768 return -EINVAL;
5769 return 0;
5770}
Andy Adamson8d353012009-04-01 09:22:32 -04005771
Andy Adamson8d353012009-04-01 09:22:32 -04005772static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5773 struct nfs4_session *session)
5774{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005775 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005776
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005777 ret = nfs4_verify_fore_channel_attrs(args, session);
5778 if (ret)
5779 return ret;
5780 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005781}
5782
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005783static int _nfs4_proc_create_session(struct nfs_client *clp,
5784 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005785{
5786 struct nfs4_session *session = clp->cl_session;
5787 struct nfs41_create_session_args args = {
5788 .client = clp,
5789 .cb_program = NFS4_CALLBACK,
5790 };
5791 struct nfs41_create_session_res res = {
5792 .client = clp,
5793 };
5794 struct rpc_message msg = {
5795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5796 .rpc_argp = &args,
5797 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005798 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005799 };
5800 int status;
5801
5802 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005803 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005804
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005805 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005806
Trond Myklebust43095d32012-11-20 11:13:12 -05005807 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005808 /* Verify the session's negotiated channel_attrs values */
5809 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005810 /* Increment the clientid slot sequence id */
5811 clp->cl_seqid++;
5812 }
5813
5814 return status;
5815}
5816
5817/*
5818 * Issues a CREATE_SESSION operation to the server.
5819 * It is the responsibility of the caller to verify the session is
5820 * expired before calling this routine.
5821 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005822int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005823{
5824 int status;
5825 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005826 struct nfs4_session *session = clp->cl_session;
5827
5828 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5829
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005830 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005831 if (status)
5832 goto out;
5833
Andy Adamsonaacd5532011-11-09 13:58:21 -05005834 /* Init or reset the session slot tables */
5835 status = nfs4_setup_session_slot_tables(session);
5836 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005837 if (status)
5838 goto out;
5839
5840 ptr = (unsigned *)&session->sess_id.data[0];
5841 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5842 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005843out:
5844 dprintk("<-- %s\n", __func__);
5845 return status;
5846}
5847
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005848/*
5849 * Issue the over-the-wire RPC DESTROY_SESSION.
5850 * The caller must serialize access to this routine.
5851 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005852int nfs4_proc_destroy_session(struct nfs4_session *session,
5853 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005854{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005855 struct rpc_message msg = {
5856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5857 .rpc_argp = session,
5858 .rpc_cred = cred,
5859 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005860 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005861
5862 dprintk("--> nfs4_proc_destroy_session\n");
5863
5864 /* session is still being setup */
5865 if (session->clp->cl_cons_state != NFS_CS_READY)
5866 return status;
5867
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005868 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005869
5870 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005871 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005872 "Session has been destroyed regardless...\n", status);
5873
5874 dprintk("<-- nfs4_proc_destroy_session\n");
5875 return status;
5876}
5877
Trond Myklebust7b38c362012-05-23 13:23:31 -04005878/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005879 * Renew the cl_session lease.
5880 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005881struct nfs4_sequence_data {
5882 struct nfs_client *clp;
5883 struct nfs4_sequence_args args;
5884 struct nfs4_sequence_res res;
5885};
5886
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005887static void nfs41_sequence_release(void *data)
5888{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005889 struct nfs4_sequence_data *calldata = data;
5890 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005891
Alexandros Batsakis71358402010-02-05 03:45:05 -08005892 if (atomic_read(&clp->cl_count) > 1)
5893 nfs4_schedule_state_renewal(clp);
5894 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005895 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005896}
5897
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005898static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5899{
5900 switch(task->tk_status) {
5901 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005902 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5903 return -EAGAIN;
5904 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005905 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005906 }
5907 return 0;
5908}
5909
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005910static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005911{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005912 struct nfs4_sequence_data *calldata = data;
5913 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914
Trond Myklebust14516c32010-07-31 14:29:06 -04005915 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5916 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005917
5918 if (task->tk_status < 0) {
5919 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005920 if (atomic_read(&clp->cl_count) == 1)
5921 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005922
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005923 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5924 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005925 return;
5926 }
5927 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005928 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005929out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005930 dprintk("<-- %s\n", __func__);
5931}
5932
5933static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5934{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005935 struct nfs4_sequence_data *calldata = data;
5936 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005937 struct nfs4_sequence_args *args;
5938 struct nfs4_sequence_res *res;
5939
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005940 args = task->tk_msg.rpc_argp;
5941 res = task->tk_msg.rpc_resp;
5942
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005943 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005944}
5945
5946static const struct rpc_call_ops nfs41_sequence_ops = {
5947 .rpc_call_done = nfs41_sequence_call_done,
5948 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005949 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005950};
5951
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005952static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5953 struct rpc_cred *cred,
5954 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005955{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005956 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5959 .rpc_cred = cred,
5960 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005961 struct rpc_task_setup task_setup_data = {
5962 .rpc_client = clp->cl_rpcclient,
5963 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005964 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005965 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5966 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005967
Alexandros Batsakis71358402010-02-05 03:45:05 -08005968 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005969 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005970 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005971 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005972 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005973 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005975 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005976 if (is_privileged)
5977 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005978 msg.rpc_argp = &calldata->args;
5979 msg.rpc_resp = &calldata->res;
5980 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005981 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005982
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005983 return rpc_run_task(&task_setup_data);
5984}
5985
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005986static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005987{
5988 struct rpc_task *task;
5989 int ret = 0;
5990
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005991 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5992 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005993 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005994 if (IS_ERR(task))
5995 ret = PTR_ERR(task);
5996 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005997 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005998 dprintk("<-- %s status=%d\n", __func__, ret);
5999 return ret;
6000}
6001
6002static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6003{
6004 struct rpc_task *task;
6005 int ret;
6006
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006007 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006008 if (IS_ERR(task)) {
6009 ret = PTR_ERR(task);
6010 goto out;
6011 }
6012 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006013 if (!ret) {
6014 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6015
6016 if (task->tk_status == 0)
6017 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006019 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006020 rpc_put_task(task);
6021out:
6022 dprintk("<-- %s status=%d\n", __func__, ret);
6023 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006024}
6025
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006026struct nfs4_reclaim_complete_data {
6027 struct nfs_client *clp;
6028 struct nfs41_reclaim_complete_args arg;
6029 struct nfs41_reclaim_complete_res res;
6030};
6031
6032static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6033{
6034 struct nfs4_reclaim_complete_data *calldata = data;
6035
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006036 nfs41_setup_sequence(calldata->clp->cl_session,
6037 &calldata->arg.seq_args,
6038 &calldata->res.seq_res,
6039 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006040}
6041
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006042static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6043{
6044 switch(task->tk_status) {
6045 case 0:
6046 case -NFS4ERR_COMPLETE_ALREADY:
6047 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6048 break;
6049 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006050 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006051 /* fall through */
6052 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006053 return -EAGAIN;
6054 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006055 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006056 }
6057 return 0;
6058}
6059
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006060static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6061{
6062 struct nfs4_reclaim_complete_data *calldata = data;
6063 struct nfs_client *clp = calldata->clp;
6064 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6065
6066 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006067 if (!nfs41_sequence_done(task, res))
6068 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006069
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006070 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6071 rpc_restart_call_prepare(task);
6072 return;
6073 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006074 dprintk("<-- %s\n", __func__);
6075}
6076
6077static void nfs4_free_reclaim_complete_data(void *data)
6078{
6079 struct nfs4_reclaim_complete_data *calldata = data;
6080
6081 kfree(calldata);
6082}
6083
6084static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6085 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6086 .rpc_call_done = nfs4_reclaim_complete_done,
6087 .rpc_release = nfs4_free_reclaim_complete_data,
6088};
6089
6090/*
6091 * Issue a global reclaim complete.
6092 */
6093static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6094{
6095 struct nfs4_reclaim_complete_data *calldata;
6096 struct rpc_task *task;
6097 struct rpc_message msg = {
6098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6099 };
6100 struct rpc_task_setup task_setup_data = {
6101 .rpc_client = clp->cl_rpcclient,
6102 .rpc_message = &msg,
6103 .callback_ops = &nfs4_reclaim_complete_call_ops,
6104 .flags = RPC_TASK_ASYNC,
6105 };
6106 int status = -ENOMEM;
6107
6108 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006109 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006110 if (calldata == NULL)
6111 goto out;
6112 calldata->clp = clp;
6113 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114
Trond Myklebust9d12b212012-01-17 22:04:25 -05006115 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006116 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006117 msg.rpc_argp = &calldata->arg;
6118 msg.rpc_resp = &calldata->res;
6119 task_setup_data.callback_data = calldata;
6120 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006121 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006122 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006123 goto out;
6124 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006125 status = nfs4_wait_for_completion_rpc_task(task);
6126 if (status == 0)
6127 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006128 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006129 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006130out:
6131 dprintk("<-- %s status=%d\n", __func__, status);
6132 return status;
6133}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006134
6135static void
6136nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6137{
6138 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006139 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006140 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006141
6142 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006143 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6144 * right now covering the LAYOUTGET we are about to send.
6145 * However, that is not so catastrophic, and there seems
6146 * to be no way to prevent it completely.
6147 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006148 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006149 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006151 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6152 NFS_I(lgp->args.inode)->layout,
6153 lgp->args.ctx->state)) {
6154 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006155 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006156}
6157
6158static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6159{
6160 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006161 struct inode *inode = lgp->args.inode;
6162 struct nfs_server *server = NFS_SERVER(inode);
6163 struct pnfs_layout_hdr *lo;
6164 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006165 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006166
6167 dprintk("--> %s\n", __func__);
6168
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006169 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006170 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171
6172 switch (task->tk_status) {
6173 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006174 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006175 case -NFS4ERR_LAYOUTTRYLATER:
6176 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006177 timeo = rpc_get_timeout(task->tk_client);
6178 giveup = lgp->args.timestamp + timeo;
6179 if (time_after(giveup, jiffies))
6180 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006181 break;
6182 case -NFS4ERR_EXPIRED:
6183 case -NFS4ERR_BAD_STATEID:
6184 spin_lock(&inode->i_lock);
6185 lo = NFS_I(inode)->layout;
6186 if (!lo || list_empty(&lo->plh_segs)) {
6187 spin_unlock(&inode->i_lock);
6188 /* If the open stateid was bad, then recover it. */
6189 state = lgp->args.ctx->state;
6190 } else {
6191 LIST_HEAD(head);
6192
6193 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6194 spin_unlock(&inode->i_lock);
6195 /* Mark the bad layout state as invalid, then
6196 * retry using the open stateid. */
6197 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006198 }
6199 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006200 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6201 rpc_restart_call_prepare(task);
6202out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006203 dprintk("<-- %s\n", __func__);
6204}
6205
Idan Kedar85541162012-08-02 11:47:10 +03006206static size_t max_response_pages(struct nfs_server *server)
6207{
6208 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6209 return nfs_page_array_len(0, max_resp_sz);
6210}
6211
6212static void nfs4_free_pages(struct page **pages, size_t size)
6213{
6214 int i;
6215
6216 if (!pages)
6217 return;
6218
6219 for (i = 0; i < size; i++) {
6220 if (!pages[i])
6221 break;
6222 __free_page(pages[i]);
6223 }
6224 kfree(pages);
6225}
6226
6227static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6228{
6229 struct page **pages;
6230 int i;
6231
6232 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6233 if (!pages) {
6234 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6235 return NULL;
6236 }
6237
6238 for (i = 0; i < size; i++) {
6239 pages[i] = alloc_page(gfp_flags);
6240 if (!pages[i]) {
6241 dprintk("%s: failed to allocate page\n", __func__);
6242 nfs4_free_pages(pages, size);
6243 return NULL;
6244 }
6245 }
6246
6247 return pages;
6248}
6249
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006250static void nfs4_layoutget_release(void *calldata)
6251{
6252 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006253 struct inode *inode = lgp->args.inode;
6254 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006255 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006256
6257 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006258 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006259 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006260 put_nfs_open_context(lgp->args.ctx);
6261 kfree(calldata);
6262 dprintk("<-- %s\n", __func__);
6263}
6264
6265static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6266 .rpc_call_prepare = nfs4_layoutget_prepare,
6267 .rpc_call_done = nfs4_layoutget_done,
6268 .rpc_release = nfs4_layoutget_release,
6269};
6270
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006271struct pnfs_layout_segment *
6272nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006273{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006274 struct inode *inode = lgp->args.inode;
6275 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006276 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006277 struct rpc_task *task;
6278 struct rpc_message msg = {
6279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6280 .rpc_argp = &lgp->args,
6281 .rpc_resp = &lgp->res,
6282 };
6283 struct rpc_task_setup task_setup_data = {
6284 .rpc_client = server->client,
6285 .rpc_message = &msg,
6286 .callback_ops = &nfs4_layoutget_call_ops,
6287 .callback_data = lgp,
6288 .flags = RPC_TASK_ASYNC,
6289 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006290 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006291 int status = 0;
6292
6293 dprintk("--> %s\n", __func__);
6294
Idan Kedar85541162012-08-02 11:47:10 +03006295 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6296 if (!lgp->args.layout.pages) {
6297 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006298 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006299 }
6300 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006301 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006302
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006303 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006304 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006305 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006306
6307 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6308 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6309
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006310 task = rpc_run_task(&task_setup_data);
6311 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006312 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006313 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006314 if (status == 0)
6315 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006316 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6317 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006318 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006319 rpc_put_task(task);
6320 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006321 if (status)
6322 return ERR_PTR(status);
6323 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006324}
6325
Benny Halevycbe82602011-05-22 19:52:37 +03006326static void
6327nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6328{
6329 struct nfs4_layoutreturn *lrp = calldata;
6330
6331 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006332 nfs41_setup_sequence(lrp->clp->cl_session,
6333 &lrp->args.seq_args,
6334 &lrp->res.seq_res,
6335 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006336}
6337
6338static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6339{
6340 struct nfs4_layoutreturn *lrp = calldata;
6341 struct nfs_server *server;
6342
6343 dprintk("--> %s\n", __func__);
6344
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006345 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006346 return;
6347
6348 server = NFS_SERVER(lrp->args.inode);
6349 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006350 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006351 return;
6352 }
Benny Halevycbe82602011-05-22 19:52:37 +03006353 dprintk("<-- %s\n", __func__);
6354}
6355
6356static void nfs4_layoutreturn_release(void *calldata)
6357{
6358 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006359 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006360
6361 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006362 spin_lock(&lo->plh_inode->i_lock);
6363 if (lrp->res.lrs_present)
6364 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6365 lo->plh_block_lgets--;
6366 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006367 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006368 kfree(calldata);
6369 dprintk("<-- %s\n", __func__);
6370}
6371
6372static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6373 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6374 .rpc_call_done = nfs4_layoutreturn_done,
6375 .rpc_release = nfs4_layoutreturn_release,
6376};
6377
6378int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6379{
6380 struct rpc_task *task;
6381 struct rpc_message msg = {
6382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6383 .rpc_argp = &lrp->args,
6384 .rpc_resp = &lrp->res,
6385 };
6386 struct rpc_task_setup task_setup_data = {
6387 .rpc_client = lrp->clp->cl_rpcclient,
6388 .rpc_message = &msg,
6389 .callback_ops = &nfs4_layoutreturn_call_ops,
6390 .callback_data = lrp,
6391 };
6392 int status;
6393
6394 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006395 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006396 task = rpc_run_task(&task_setup_data);
6397 if (IS_ERR(task))
6398 return PTR_ERR(task);
6399 status = task->tk_status;
6400 dprintk("<-- %s status=%d\n", __func__, status);
6401 rpc_put_task(task);
6402 return status;
6403}
6404
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006405/*
6406 * Retrieve the list of Data Server devices from the MDS.
6407 */
6408static int _nfs4_getdevicelist(struct nfs_server *server,
6409 const struct nfs_fh *fh,
6410 struct pnfs_devicelist *devlist)
6411{
6412 struct nfs4_getdevicelist_args args = {
6413 .fh = fh,
6414 .layoutclass = server->pnfs_curr_ld->id,
6415 };
6416 struct nfs4_getdevicelist_res res = {
6417 .devlist = devlist,
6418 };
6419 struct rpc_message msg = {
6420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6421 .rpc_argp = &args,
6422 .rpc_resp = &res,
6423 };
6424 int status;
6425
6426 dprintk("--> %s\n", __func__);
6427 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6428 &res.seq_res, 0);
6429 dprintk("<-- %s status=%d\n", __func__, status);
6430 return status;
6431}
6432
6433int nfs4_proc_getdevicelist(struct nfs_server *server,
6434 const struct nfs_fh *fh,
6435 struct pnfs_devicelist *devlist)
6436{
6437 struct nfs4_exception exception = { };
6438 int err;
6439
6440 do {
6441 err = nfs4_handle_exception(server,
6442 _nfs4_getdevicelist(server, fh, devlist),
6443 &exception);
6444 } while (exception.retry);
6445
6446 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6447 err, devlist->num_devs);
6448
6449 return err;
6450}
6451EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6452
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006453static int
6454_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6455{
6456 struct nfs4_getdeviceinfo_args args = {
6457 .pdev = pdev,
6458 };
6459 struct nfs4_getdeviceinfo_res res = {
6460 .pdev = pdev,
6461 };
6462 struct rpc_message msg = {
6463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6464 .rpc_argp = &args,
6465 .rpc_resp = &res,
6466 };
6467 int status;
6468
6469 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006470 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006471 dprintk("<-- %s status=%d\n", __func__, status);
6472
6473 return status;
6474}
6475
6476int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6477{
6478 struct nfs4_exception exception = { };
6479 int err;
6480
6481 do {
6482 err = nfs4_handle_exception(server,
6483 _nfs4_proc_getdeviceinfo(server, pdev),
6484 &exception);
6485 } while (exception.retry);
6486 return err;
6487}
6488EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6489
Andy Adamson863a3c62011-03-23 13:27:54 +00006490static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6491{
6492 struct nfs4_layoutcommit_data *data = calldata;
6493 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006494 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006495
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006496 nfs41_setup_sequence(session,
6497 &data->args.seq_args,
6498 &data->res.seq_res,
6499 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006500}
6501
6502static void
6503nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6504{
6505 struct nfs4_layoutcommit_data *data = calldata;
6506 struct nfs_server *server = NFS_SERVER(data->args.inode);
6507
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006508 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006509 return;
6510
6511 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006512 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6513 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6514 case -NFS4ERR_BADLAYOUT: /* no layout */
6515 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006516 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006517 break;
6518 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006519 nfs_post_op_update_inode_force_wcc(data->args.inode,
6520 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006521 break;
6522 default:
6523 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6524 rpc_restart_call_prepare(task);
6525 return;
6526 }
6527 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006528}
6529
6530static void nfs4_layoutcommit_release(void *calldata)
6531{
6532 struct nfs4_layoutcommit_data *data = calldata;
6533
Andy Adamsondb29c082011-07-30 20:52:38 -04006534 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006535 put_rpccred(data->cred);
6536 kfree(data);
6537}
6538
6539static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6540 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6541 .rpc_call_done = nfs4_layoutcommit_done,
6542 .rpc_release = nfs4_layoutcommit_release,
6543};
6544
6545int
Andy Adamsonef311532011-03-12 02:58:10 -05006546nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006547{
6548 struct rpc_message msg = {
6549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6550 .rpc_argp = &data->args,
6551 .rpc_resp = &data->res,
6552 .rpc_cred = data->cred,
6553 };
6554 struct rpc_task_setup task_setup_data = {
6555 .task = &data->task,
6556 .rpc_client = NFS_CLIENT(data->args.inode),
6557 .rpc_message = &msg,
6558 .callback_ops = &nfs4_layoutcommit_ops,
6559 .callback_data = data,
6560 .flags = RPC_TASK_ASYNC,
6561 };
6562 struct rpc_task *task;
6563 int status = 0;
6564
6565 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6566 "lbw: %llu inode %lu\n",
6567 data->task.tk_pid, sync,
6568 data->args.lastbytewritten,
6569 data->args.inode->i_ino);
6570
Trond Myklebust9d12b212012-01-17 22:04:25 -05006571 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006572 task = rpc_run_task(&task_setup_data);
6573 if (IS_ERR(task))
6574 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006575 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006576 goto out;
6577 status = nfs4_wait_for_completion_rpc_task(task);
6578 if (status != 0)
6579 goto out;
6580 status = task->tk_status;
6581out:
6582 dprintk("%s: status %d\n", __func__, status);
6583 rpc_put_task(task);
6584 return status;
6585}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006586
6587static int
6588_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6589 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6590{
6591 struct nfs41_secinfo_no_name_args args = {
6592 .style = SECINFO_STYLE_CURRENT_FH,
6593 };
6594 struct nfs4_secinfo_res res = {
6595 .flavors = flavors,
6596 };
6597 struct rpc_message msg = {
6598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6599 .rpc_argp = &args,
6600 .rpc_resp = &res,
6601 };
6602 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6603}
6604
6605static int
6606nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6607 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6608{
6609 struct nfs4_exception exception = { };
6610 int err;
6611 do {
6612 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6613 switch (err) {
6614 case 0:
6615 case -NFS4ERR_WRONGSEC:
6616 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006617 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006618 default:
6619 err = nfs4_handle_exception(server, err, &exception);
6620 }
6621 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006622out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006623 return err;
6624}
6625
6626static int
6627nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6628 struct nfs_fsinfo *info)
6629{
6630 int err;
6631 struct page *page;
6632 rpc_authflavor_t flavor;
6633 struct nfs4_secinfo_flavors *flavors;
6634
6635 page = alloc_page(GFP_KERNEL);
6636 if (!page) {
6637 err = -ENOMEM;
6638 goto out;
6639 }
6640
6641 flavors = page_address(page);
6642 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6643
6644 /*
6645 * Fall back on "guess and check" method if
6646 * the server doesn't support SECINFO_NO_NAME
6647 */
6648 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6649 err = nfs4_find_root_sec(server, fhandle, info);
6650 goto out_freepage;
6651 }
6652 if (err)
6653 goto out_freepage;
6654
6655 flavor = nfs_find_best_sec(flavors);
6656 if (err == 0)
6657 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6658
6659out_freepage:
6660 put_page(page);
6661 if (err == -EACCES)
6662 return -EPERM;
6663out:
6664 return err;
6665}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006666
6667static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006668{
6669 int status;
6670 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006671 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006672 };
6673 struct nfs41_test_stateid_res res;
6674 struct rpc_message msg = {
6675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6676 .rpc_argp = &args,
6677 .rpc_resp = &res,
6678 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006679
Chuck Lever38527b12012-07-11 16:30:23 -04006680 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006681 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006682 nfs4_set_sequence_privileged(&args.seq_args);
6683 status = nfs4_call_sync_sequence(server->client, server, &msg,
6684 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006685 if (status != NFS_OK) {
6686 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006687 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006688 }
6689 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006690 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006691}
6692
Chuck Lever38527b12012-07-11 16:30:23 -04006693/**
6694 * nfs41_test_stateid - perform a TEST_STATEID operation
6695 *
6696 * @server: server / transport on which to perform the operation
6697 * @stateid: state ID to test
6698 *
6699 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6700 * Otherwise a negative NFS4ERR value is returned if the operation
6701 * failed or the state ID is not currently valid.
6702 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006703static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006704{
6705 struct nfs4_exception exception = { };
6706 int err;
6707 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006708 err = _nfs41_test_stateid(server, stateid);
6709 if (err != -NFS4ERR_DELAY)
6710 break;
6711 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006712 } while (exception.retry);
6713 return err;
6714}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006715
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006716static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006717{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006718 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006719 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006720 };
6721 struct nfs41_free_stateid_res res;
6722 struct rpc_message msg = {
6723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6724 .rpc_argp = &args,
6725 .rpc_resp = &res,
6726 };
Chuck Lever38527b12012-07-11 16:30:23 -04006727 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006728
Chuck Lever38527b12012-07-11 16:30:23 -04006729 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006730 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006731 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006732 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006733 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006734 dprintk("NFS reply free_stateid: %d\n", status);
6735 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006736}
6737
Chuck Lever38527b12012-07-11 16:30:23 -04006738/**
6739 * nfs41_free_stateid - perform a FREE_STATEID operation
6740 *
6741 * @server: server / transport on which to perform the operation
6742 * @stateid: state ID to release
6743 *
6744 * Returns NFS_OK if the server freed "stateid". Otherwise a
6745 * negative NFS4ERR value is returned.
6746 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006747static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006748{
6749 struct nfs4_exception exception = { };
6750 int err;
6751 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006752 err = _nfs4_free_stateid(server, stateid);
6753 if (err != -NFS4ERR_DELAY)
6754 break;
6755 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006756 } while (exception.retry);
6757 return err;
6758}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006759
6760static bool nfs41_match_stateid(const nfs4_stateid *s1,
6761 const nfs4_stateid *s2)
6762{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006763 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006764 return false;
6765
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006766 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006767 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006768 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006769 return true;
6770
6771 return false;
6772}
6773
Andy Adamson557134a2009-04-01 09:21:53 -04006774#endif /* CONFIG_NFS_V4_1 */
6775
Trond Myklebust36281ca2012-03-04 18:13:56 -05006776static bool nfs4_match_stateid(const nfs4_stateid *s1,
6777 const nfs4_stateid *s2)
6778{
Trond Myklebustf597c532012-03-04 18:13:56 -05006779 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006780}
6781
6782
Trond Myklebust17280172012-03-11 13:11:00 -04006783static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006784 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006785 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 .recover_open = nfs4_open_reclaim,
6787 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006788 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006789 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006790 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791};
6792
Andy Adamson591d71c2009-04-01 09:22:47 -04006793#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006794static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006795 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6796 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6797 .recover_open = nfs4_open_reclaim,
6798 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006799 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006800 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006801 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006802 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006803};
6804#endif /* CONFIG_NFS_V4_1 */
6805
Trond Myklebust17280172012-03-11 13:11:00 -04006806static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006807 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006808 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 .recover_open = nfs4_open_expired,
6810 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006811 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006812 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813};
6814
Andy Adamson591d71c2009-04-01 09:22:47 -04006815#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006816static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006817 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6818 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006819 .recover_open = nfs41_open_expired,
6820 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006821 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006822 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006823};
6824#endif /* CONFIG_NFS_V4_1 */
6825
Trond Myklebust17280172012-03-11 13:11:00 -04006826static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006827 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006828 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006829 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006830};
6831
6832#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006833static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006834 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006835 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006836 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006837};
6838#endif
6839
Trond Myklebust97dc1352010-06-16 09:52:26 -04006840static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6841 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006842 .init_caps = NFS_CAP_READDIRPLUS
6843 | NFS_CAP_ATOMIC_OPEN
6844 | NFS_CAP_CHANGE_ATTR
6845 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006846 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006847 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006848 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006849 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6850 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6851 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006852};
6853
6854#if defined(CONFIG_NFS_V4_1)
6855static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6856 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006857 .init_caps = NFS_CAP_READDIRPLUS
6858 | NFS_CAP_ATOMIC_OPEN
6859 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006860 | NFS_CAP_POSIX_LOCK
6861 | NFS_CAP_STATEID_NFSV41,
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 */