blob: 22c80f9ed824639d54401dae711392137e60a627 [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,
785 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786{
Al Viro82a2c1b2011-06-22 18:30:55 -0400787 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788 struct inode *dir = parent->d_inode;
789 struct nfs_server *server = NFS_SERVER(dir);
790 struct nfs4_opendata *p;
791
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400792 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 if (p == NULL)
794 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 if (p->o_arg.seqid == NULL)
797 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400798 nfs_sb_active(dentry->d_sb);
799 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 p->dir = parent;
801 p->owner = sp;
802 atomic_inc(&sp->so_count);
803 p->o_arg.fh = NFS_FH(dir);
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 Myklebuste56e0b782006-01-03 09:55:08 +0100821 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500822 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500823 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400824
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 p->o_arg.u.attrs = &p->attrs;
826 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500827
828 verf[0] = jiffies;
829 verf[1] = current->pid;
830 memcpy(p->o_arg.u.verifier.data, verf,
831 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100833 p->c_arg.fh = &p->o_res.fh;
834 p->c_arg.stateid = &p->o_res.stateid;
835 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838 return p;
839err_free:
840 kfree(p);
841err:
842 dput(parent);
843 return NULL;
844}
845
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848 struct nfs4_opendata *p = container_of(kref,
849 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400850 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851
852 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853 if (p->state != NULL)
854 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400855 nfs4_put_state_owner(p->owner);
856 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 dput(p->dentry);
858 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500859 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400860 kfree(p);
861}
862
863static void nfs4_opendata_put(struct nfs4_opendata *p)
864{
865 if (p != NULL)
866 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867}
868
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
870{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 int ret;
872
Trond Myklebust06f814a2006-01-03 09:55:07 +0100873 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100874 return ret;
875}
876
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878{
879 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880
Trond Myklebust536e43d2012-01-17 22:04:26 -0500881 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500882 goto out;
883 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500885 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
886 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 break;
888 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500889 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
890 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 break;
892 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500893 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
894 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400897 return ret;
898}
899
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500902 if (delegation == NULL)
903 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400905 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500906 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500908 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
909 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500910 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911 return 1;
912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100915{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100917 case FMODE_WRITE:
918 state->n_wronly++;
919 break;
920 case FMODE_READ:
921 state->n_rdonly++;
922 break;
923 case FMODE_READ|FMODE_WRITE:
924 state->n_rdwr++;
925 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100927}
928
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400930{
931 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500932 nfs4_stateid_copy(&state->stateid, stateid);
933 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400935 case FMODE_READ:
936 set_bit(NFS_O_RDONLY_STATE, &state->flags);
937 break;
938 case FMODE_WRITE:
939 set_bit(NFS_O_WRONLY_STATE, &state->flags);
940 break;
941 case FMODE_READ|FMODE_WRITE:
942 set_bit(NFS_O_RDWR_STATE, &state->flags);
943 }
944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400947{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400948 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400950 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static 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 -0700954{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400955 /*
956 * Protect the call to nfs4_state_set_mode_locked and
957 * serialise the stateid update
958 */
959 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400960 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500961 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400962 set_bit(NFS_DELEGATED_STATE, &state->flags);
963 }
964 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400966 write_sequnlock(&state->seqlock);
967 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700969 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static 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 -0500973{
974 struct nfs_inode *nfsi = NFS_I(state->inode);
975 struct nfs_delegation *deleg_cur;
976 int ret = 0;
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500979
980 rcu_read_lock();
981 deleg_cur = rcu_dereference(nfsi->delegation);
982 if (deleg_cur == NULL)
983 goto no_delegation;
984
985 spin_lock(&deleg_cur->lock);
986 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -0500987 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500989 goto no_delegation_unlock;
990
991 if (delegation == NULL)
992 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -0500993 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -0500994 goto no_delegation_unlock;
995
Trond Myklebustb7391f42008-12-23 15:21:52 -0500996 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500998 ret = 1;
999no_delegation_unlock:
1000 spin_unlock(&deleg_cur->lock);
1001no_delegation:
1002 rcu_read_unlock();
1003
1004 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001006 ret = 1;
1007 }
1008
1009 return ret;
1010}
1011
1012
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014{
1015 struct nfs_delegation *delegation;
1016
1017 rcu_read_lock();
1018 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020 rcu_read_unlock();
1021 return;
1022 }
1023 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001024 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025}
1026
Trond Myklebust6ee41262007-07-08 14:11:36 -04001027static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028{
1029 struct nfs4_state *state = opendata->state;
1030 struct nfs_inode *nfsi = NFS_I(state->inode);
1031 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001032 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 nfs4_stateid stateid;
1035 int ret = -EAGAIN;
1036
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 if (can_open_cached(state, fmode, open_mode)) {
1041 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001043 goto out_return_state;
1044 }
1045 spin_unlock(&state->owner->so_lock);
1046 }
Trond Myklebust34310432008-12-23 15:21:38 -05001047 rcu_read_lock();
1048 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001049 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001050 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001051 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001052 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001054 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001056 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 if (ret != 0)
1058 goto out;
1059 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001060
1061 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001063 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065out:
1066 return ERR_PTR(ret);
1067out_return_state:
1068 atomic_inc(&state->count);
1069 return state;
1070}
1071
Andy Adamsone23008e2012-10-02 21:07:32 -04001072static void
1073nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1074{
1075 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1076 struct nfs_delegation *delegation;
1077 int delegation_flags = 0;
1078
1079 rcu_read_lock();
1080 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1081 if (delegation)
1082 delegation_flags = delegation->flags;
1083 rcu_read_unlock();
1084 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1085 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1086 "returning a delegation for "
1087 "OPEN(CLAIM_DELEGATE_CUR)\n",
1088 clp->cl_hostname);
1089 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1090 nfs_inode_set_delegation(state->inode,
1091 data->owner->so_cred,
1092 &data->o_res);
1093 else
1094 nfs_inode_reclaim_delegation(state->inode,
1095 data->owner->so_cred,
1096 &data->o_res);
1097}
1098
1099/*
1100 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1101 * and update the nfs4_state.
1102 */
1103static struct nfs4_state *
1104_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1105{
1106 struct inode *inode = data->state->inode;
1107 struct nfs4_state *state = data->state;
1108 int ret;
1109
1110 if (!data->rpc_done) {
1111 ret = data->rpc_status;
1112 goto err;
1113 }
1114
1115 ret = -ESTALE;
1116 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1117 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1118 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1119 goto err;
1120
1121 ret = -ENOMEM;
1122 state = nfs4_get_open_state(inode, data->owner);
1123 if (state == NULL)
1124 goto err;
1125
1126 ret = nfs_refresh_inode(inode, &data->f_attr);
1127 if (ret)
1128 goto err;
1129
1130 if (data->o_res.delegation_type != 0)
1131 nfs4_opendata_check_deleg(data, state);
1132 update_open_stateid(state, &data->o_res.stateid, NULL,
1133 data->o_arg.fmode);
1134
1135 return state;
1136err:
1137 return ERR_PTR(ret);
1138
1139}
1140
1141static struct nfs4_state *
1142_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143{
1144 struct inode *inode;
1145 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147
Trond Myklebustaac00a82007-07-05 19:02:21 -04001148 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001149 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001150 goto out;
1151 }
1152
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001153 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001154 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001155 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001156 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001157 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001158 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001159 goto err;
1160 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001161 state = nfs4_get_open_state(inode, data->owner);
1162 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001163 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001164 if (data->o_res.delegation_type != 0)
1165 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001166 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001168 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001169out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001170 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001171 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001172err_put_inode:
1173 iput(inode);
1174err:
1175 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001176}
1177
Andy Adamsone23008e2012-10-02 21:07:32 -04001178static struct nfs4_state *
1179nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1180{
1181 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1182 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1183 return _nfs4_opendata_to_nfs4_state(data);
1184}
1185
Trond Myklebust864472e2006-01-03 09:55:15 +01001186static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1187{
1188 struct nfs_inode *nfsi = NFS_I(state->inode);
1189 struct nfs_open_context *ctx;
1190
1191 spin_lock(&state->inode->i_lock);
1192 list_for_each_entry(ctx, &nfsi->open_files, list) {
1193 if (ctx->state != state)
1194 continue;
1195 get_nfs_open_context(ctx);
1196 spin_unlock(&state->inode->i_lock);
1197 return ctx;
1198 }
1199 spin_unlock(&state->inode->i_lock);
1200 return ERR_PTR(-ENOENT);
1201}
1202
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001203static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1204{
1205 struct nfs4_opendata *opendata;
1206
Al Viro3d4ff432011-06-22 18:40:12 -04001207 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001208 if (opendata == NULL)
1209 return ERR_PTR(-ENOMEM);
1210 opendata->state = state;
1211 atomic_inc(&state->count);
1212 return opendata;
1213}
1214
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001216{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 int ret;
1219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220 opendata->o_arg.open_flags = 0;
1221 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1223 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1224 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001225 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 if (ret != 0)
1227 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001229 if (IS_ERR(newstate))
1230 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001231 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 return 0;
1234}
1235
1236static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1237{
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 int ret;
1240
1241 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001242 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 smp_rmb();
1244 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001245 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001246 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 if (ret != 0)
1248 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001249 if (newstate != state)
1250 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 }
1252 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001253 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001254 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 if (ret != 0)
1256 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001257 if (newstate != state)
1258 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001259 }
1260 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001261 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 if (ret != 0)
1264 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001265 if (newstate != state)
1266 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 /*
1269 * We may have performed cached opens for all three recoveries.
1270 * Check if we need to update the current stateid.
1271 */
1272 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001273 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001274 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001275 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001276 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001277 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001278 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 return 0;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
1283 * OPEN_RECLAIM:
1284 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001286static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001288 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int status;
1292
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001293 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1294 if (IS_ERR(opendata))
1295 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1297 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001298 rcu_read_lock();
1299 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001300 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001301 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001302 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 opendata->o_arg.u.delegation_type = delegation_type;
1304 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return status;
1307}
1308
Trond Myklebust539cd032007-06-05 11:46:42 -04001309static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct nfs_server *server = NFS_SERVER(state->inode);
1312 struct nfs4_exception exception = { };
1313 int err;
1314 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001315 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001316 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001317 break;
1318 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 } while (exception.retry);
1320 return err;
1321}
1322
Trond Myklebust864472e2006-01-03 09:55:15 +01001323static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1324{
1325 struct nfs_open_context *ctx;
1326 int ret;
1327
1328 ctx = nfs4_state_find_open_context(state);
1329 if (IS_ERR(ctx))
1330 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001331 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001332 put_nfs_open_context(ctx);
1333 return ret;
1334}
1335
Trond Myklebust13437e12007-07-06 15:10:43 -04001336static 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 -07001337{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001338 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001341 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1342 if (IS_ERR(opendata))
1343 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001344 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001345 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001346 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001347 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Trond Myklebust13437e12007-07-06 15:10:43 -04001351int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001354 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int err;
1356 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001357 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 switch (err) {
1359 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001360 case -ENOENT:
1361 case -ESTALE:
1362 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001363 case -NFS4ERR_BADSESSION:
1364 case -NFS4ERR_BADSLOT:
1365 case -NFS4ERR_BAD_HIGH_SLOT:
1366 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1367 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05001368 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04001369 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05001370 err = -EAGAIN;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 case -NFS4ERR_STALE_CLIENTID:
1373 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05001374 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case -NFS4ERR_EXPIRED:
1376 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001377 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05001378 err = -EAGAIN;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001379 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001380 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001381 case -NFS4ERR_ADMIN_REVOKED:
1382 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001383 nfs_inode_find_state_and_recover(state->inode,
1384 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001385 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001386 case -ENOMEM:
1387 err = 0;
1388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Trond Myklebust37380e42013-02-05 12:35:16 -05001390 set_bit(NFS_DELEGATED_STATE, &state->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 err = nfs4_handle_exception(server, err, &exception);
1392 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return err;
1395}
1396
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1398{
1399 struct nfs4_opendata *data = calldata;
1400
1401 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001402 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001403 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001404 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001405 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001406 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001407 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408}
1409
1410static void nfs4_open_confirm_release(void *calldata)
1411{
1412 struct nfs4_opendata *data = calldata;
1413 struct nfs4_state *state = NULL;
1414
1415 /* If this request hasn't been cancelled, do nothing */
1416 if (data->cancelled == 0)
1417 goto out_free;
1418 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001419 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001422 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001423 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001424out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426}
1427
1428static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001429 .rpc_call_done = nfs4_open_confirm_done,
1430 .rpc_release = nfs4_open_confirm_release,
1431};
1432
1433/*
1434 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1435 */
1436static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1437{
1438 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1439 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001440 struct rpc_message msg = {
1441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1442 .rpc_argp = &data->c_arg,
1443 .rpc_resp = &data->c_res,
1444 .rpc_cred = data->owner->so_cred,
1445 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001446 struct rpc_task_setup task_setup_data = {
1447 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001449 .callback_ops = &nfs4_open_confirm_ops,
1450 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001451 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001452 .flags = RPC_TASK_ASYNC,
1453 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int status;
1455
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001456 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001457 data->rpc_done = 0;
1458 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001459 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001460 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001461 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001462 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001463 status = nfs4_wait_for_completion_rpc_task(task);
1464 if (status != 0) {
1465 data->cancelled = 1;
1466 smp_wmb();
1467 } else
1468 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001469 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return status;
1471}
1472
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 struct nfs4_opendata *data = calldata;
1476 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001477
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001479 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001480 /*
1481 * Check if we still need to send an OPEN call, or if we can use
1482 * a delegation instead.
1483 */
1484 if (data->state != NULL) {
1485 struct nfs_delegation *delegation;
1486
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001489 rcu_read_lock();
1490 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001491 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1492 can_open_delegated(delegation, data->o_arg.fmode))
1493 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001494 rcu_read_unlock();
1495 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001496 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001497 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001498 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001499 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001500 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001501 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1502 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001503 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001504 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001505 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001506 &data->o_res.seq_res,
1507 task) != 0)
1508 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001509 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001510unlock_no_action:
1511 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512out_no_action:
1513 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001514out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001515 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001516}
1517
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518static void nfs4_open_done(struct rpc_task *task, void *calldata)
1519{
1520 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001523
Trond Myklebust14516c32010-07-31 14:29:06 -04001524 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1525 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001526
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001528 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1529 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 case S_IFREG:
1531 break;
1532 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001534 break;
1535 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001537 break;
1538 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001540 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001541 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001542 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001543 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1544 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001545 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001546 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001548
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549static void nfs4_open_release(void *calldata)
1550{
1551 struct nfs4_opendata *data = calldata;
1552 struct nfs4_state *state = NULL;
1553
1554 /* If this request hasn't been cancelled, do nothing */
1555 if (data->cancelled == 0)
1556 goto out_free;
1557 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001558 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 goto out_free;
1560 /* In case we need an open_confirm, no cleanup! */
1561 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1562 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001564 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001565 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001566out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001567 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568}
1569
1570static const struct rpc_call_ops nfs4_open_ops = {
1571 .rpc_call_prepare = nfs4_open_prepare,
1572 .rpc_call_done = nfs4_open_done,
1573 .rpc_release = nfs4_open_release,
1574};
1575
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001576static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001577{
1578 struct inode *dir = data->dir->d_inode;
1579 struct nfs_server *server = NFS_SERVER(dir);
1580 struct nfs_openargs *o_arg = &data->o_arg;
1581 struct nfs_openres *o_res = &data->o_res;
1582 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001583 struct rpc_message msg = {
1584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1585 .rpc_argp = o_arg,
1586 .rpc_resp = o_res,
1587 .rpc_cred = data->owner->so_cred,
1588 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001589 struct rpc_task_setup task_setup_data = {
1590 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001591 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 .callback_ops = &nfs4_open_ops,
1593 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001594 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 .flags = RPC_TASK_ASYNC,
1596 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 int status;
1598
Trond Myklebust9d12b212012-01-17 22:04:25 -05001599 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001600 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001601 data->rpc_done = 0;
1602 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001603 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001604 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001605 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001606 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001607 if (IS_ERR(task))
1608 return PTR_ERR(task);
1609 status = nfs4_wait_for_completion_rpc_task(task);
1610 if (status != 0) {
1611 data->cancelled = 1;
1612 smp_wmb();
1613 } else
1614 status = data->rpc_status;
1615 rpc_put_task(task);
1616
1617 return status;
1618}
1619
1620static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1621{
1622 struct inode *dir = data->dir->d_inode;
1623 struct nfs_openres *o_res = &data->o_res;
1624 int status;
1625
1626 status = nfs4_run_open_task(data, 1);
1627 if (status != 0 || !data->rpc_done)
1628 return status;
1629
Trond Myklebust6926afd2012-01-07 13:22:46 -05001630 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1631
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001632 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1633 status = _nfs4_proc_open_confirm(data);
1634 if (status != 0)
1635 return status;
1636 }
1637
1638 return status;
1639}
1640
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001641static int nfs4_opendata_access(struct rpc_cred *cred,
1642 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001643 struct nfs4_state *state, fmode_t fmode,
1644 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001645{
1646 struct nfs_access_entry cache;
1647 u32 mask;
1648
1649 /* access call failed or for some reason the server doesn't
1650 * support any access modes -- defer access call until later */
1651 if (opendata->o_res.access_supported == 0)
1652 return 0;
1653
1654 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001655 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001656 * write mode bits, but POSIX allows the creating process to write.
1657 * use openflags to check for exec, because fmode won't
1658 * always have FMODE_EXEC set when file open for exec. */
1659 if (openflags & __FMODE_EXEC) {
1660 /* ONLY check for exec rights */
1661 mask = MAY_EXEC;
1662 } else if (fmode & FMODE_READ)
1663 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001664
1665 cache.cred = cred;
1666 cache.jiffies = jiffies;
1667 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1668 nfs_access_add_cache(state->inode, &cache);
1669
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001670 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001671 return 0;
1672
1673 /* even though OPEN succeeded, access is denied. Close the file */
1674 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001675 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001676}
1677
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678/*
1679 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1680 */
1681static int _nfs4_proc_open(struct nfs4_opendata *data)
1682{
1683 struct inode *dir = data->dir->d_inode;
1684 struct nfs_server *server = NFS_SERVER(dir);
1685 struct nfs_openargs *o_arg = &data->o_arg;
1686 struct nfs_openres *o_res = &data->o_res;
1687 int status;
1688
1689 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001690 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001692 if (status != 0) {
1693 if (status == -NFS4ERR_BADNAME &&
1694 !(o_arg->open_flags & O_CREAT))
1695 return -ENOENT;
1696 return status;
1697 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698
Trond Myklebust6926afd2012-01-07 13:22:46 -05001699 nfs_fattr_map_and_free_names(server, &data->f_attr);
1700
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001701 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001702 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001703 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1704 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001706 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001711 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Andy Adamsond83217c2011-03-01 01:34:17 +00001715static int nfs4_recover_expired_lease(struct nfs_server *server)
1716{
1717 return nfs4_client_recover_expired_lease(server->nfs_client);
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720/*
1721 * OPEN_EXPIRED:
1722 * reclaim state on the server after a network partition.
1723 * Assumes caller holds the appropriate lock
1724 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001725static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001727 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001728 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001730 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1731 if (IS_ERR(opendata))
1732 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001734 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001735 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001736 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001740static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001741{
Trond Myklebust539cd032007-06-05 11:46:42 -04001742 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001743 struct nfs4_exception exception = { };
1744 int err;
1745
1746 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001747 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001748 switch (err) {
1749 default:
1750 goto out;
1751 case -NFS4ERR_GRACE:
1752 case -NFS4ERR_DELAY:
1753 nfs4_handle_exception(server, err, &exception);
1754 err = 0;
1755 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001756 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001757out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001758 return err;
1759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001764 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Trond Myklebust864472e2006-01-03 09:55:15 +01001766 ctx = nfs4_state_find_open_context(state);
1767 if (IS_ERR(ctx))
1768 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001769 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001770 put_nfs_open_context(ctx);
1771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001774#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001775static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001776{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001777 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001778 nfs4_stateid *stateid = &state->stateid;
1779 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001780
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001781 /* If a state reset has been done, test_stateid is unneeded */
1782 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1783 return;
1784
1785 status = nfs41_test_stateid(server, stateid);
1786 if (status != NFS_OK) {
1787 /* Free the stateid unless the server explicitly
1788 * informs us the stateid is unrecognized. */
1789 if (status != -NFS4ERR_BAD_STATEID)
1790 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001791 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001792
Bryan Schumaker69388672012-09-26 15:25:52 -04001793 write_seqlock(&state->seqlock);
1794 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1795 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001796 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1797 }
1798}
1799
1800/**
1801 * nfs41_check_open_stateid - possibly free an open stateid
1802 *
1803 * @state: NFSv4 state for an inode
1804 *
1805 * Returns NFS_OK if recovery for this stateid is now finished.
1806 * Otherwise a negative NFS4ERR value is returned.
1807 */
1808static int nfs41_check_open_stateid(struct nfs4_state *state)
1809{
1810 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001811 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001812 int status;
1813
1814 /* If a state reset has been done, test_stateid is unneeded */
1815 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1816 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1817 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1818 return -NFS4ERR_BAD_STATEID;
1819
1820 status = nfs41_test_stateid(server, stateid);
1821 if (status != NFS_OK) {
1822 /* Free the stateid unless the server explicitly
1823 * informs us the stateid is unrecognized. */
1824 if (status != -NFS4ERR_BAD_STATEID)
1825 nfs41_free_stateid(server, stateid);
1826
1827 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1828 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1829 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001830 }
1831 return status;
1832}
1833
1834static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1835{
Chuck Levereb64cf92012-07-11 16:30:05 -04001836 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001837
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001838 nfs41_clear_delegation_stateid(state);
1839 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001840 if (status != NFS_OK)
1841 status = nfs4_open_expired(sp, state);
1842 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001843}
1844#endif
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001847 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1848 * fields corresponding to attributes that were used to store the verifier.
1849 * Make sure we clobber those fields in the later setattr call
1850 */
1851static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1852{
1853 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1854 !(sattr->ia_valid & ATTR_ATIME_SET))
1855 sattr->ia_valid |= ATTR_ATIME;
1856
1857 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1858 !(sattr->ia_valid & ATTR_MTIME_SET))
1859 sattr->ia_valid |= ATTR_MTIME;
1860}
1861
Trond Myklebustc21443c2013-02-07 14:26:21 -05001862static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1863 fmode_t fmode,
1864 int flags,
1865 struct nfs4_state **res)
1866{
1867 struct nfs4_state_owner *sp = opendata->owner;
1868 struct nfs_server *server = sp->so_server;
1869 struct nfs4_state *state;
1870 unsigned int seq;
1871 int ret;
1872
1873 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1874
1875 ret = _nfs4_proc_open(opendata);
1876 if (ret != 0)
1877 goto out;
1878
1879 state = nfs4_opendata_to_nfs4_state(opendata);
1880 ret = PTR_ERR(state);
1881 if (IS_ERR(state))
1882 goto out;
1883 if (server->caps & NFS_CAP_POSIX_LOCK)
1884 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1885
1886 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1887 if (ret != 0)
1888 goto out;
1889
1890 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1891 nfs4_schedule_stateid_recovery(server, state);
1892 nfs4_wait_clnt_recover(server->nfs_client);
1893 }
1894 *res = state;
1895out:
1896 return ret;
1897}
1898
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001899/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001900 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Andy Adamson82be4172012-05-23 05:02:35 -04001902static int _nfs4_do_open(struct inode *dir,
1903 struct dentry *dentry,
1904 fmode_t fmode,
1905 int flags,
1906 struct iattr *sattr,
1907 struct rpc_cred *cred,
1908 struct nfs4_state **res,
1909 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct nfs4_state_owner *sp;
1912 struct nfs4_state *state = NULL;
1913 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001914 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001915 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001919 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1920 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1922 goto out_err;
1923 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001924 status = nfs4_recover_expired_lease(server);
1925 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001926 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001927 if (dentry->d_inode != NULL)
1928 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001929 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001930 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001931 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001932 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Andy Adamson82be4172012-05-23 05:02:35 -04001934 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1935 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1936 if (!opendata->f_attr.mdsthreshold)
1937 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001938 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001939 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001940 if (dentry->d_inode != NULL)
1941 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001942
Trond Myklebustc21443c2013-02-07 14:26:21 -05001943 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001944 if (status != 0)
1945 goto err_opendata_put;
1946
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001947 if (opendata->o_arg.open_flags & O_EXCL) {
1948 nfs4_exclusive_attrset(opendata, sattr);
1949
1950 nfs_fattr_init(opendata->o_res.f_attr);
1951 status = nfs4_do_setattr(state->inode, cred,
1952 opendata->o_res.f_attr, sattr,
1953 state);
1954 if (status == 0)
1955 nfs_setattr_update_inode(state->inode, sattr);
1956 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1957 }
Andy Adamson82be4172012-05-23 05:02:35 -04001958
1959 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1960 *ctx_th = opendata->f_attr.mdsthreshold;
1961 else
1962 kfree(opendata->f_attr.mdsthreshold);
1963 opendata->f_attr.mdsthreshold = NULL;
1964
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001965 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *res = state;
1968 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001969err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001970 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001971 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001972err_put_state_owner:
1973 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 *res = NULL;
1976 return status;
1977}
1978
1979
Andy Adamson82be4172012-05-23 05:02:35 -04001980static struct nfs4_state *nfs4_do_open(struct inode *dir,
1981 struct dentry *dentry,
1982 fmode_t fmode,
1983 int flags,
1984 struct iattr *sattr,
1985 struct rpc_cred *cred,
1986 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct nfs4_exception exception = { };
1989 struct nfs4_state *res;
1990 int status;
1991
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001992 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001994 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1995 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (status == 0)
1997 break;
1998 /* NOTE: BAD_SEQID means the server and client disagree about the
1999 * book-keeping w.r.t. state-changing operations
2000 * (OPEN/CLOSE/LOCK/LOCKU...)
2001 * It is actually a sign of a bug on the client or on the server.
2002 *
2003 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002004 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 * have unhashed the old state_owner for us, and that we can
2006 * therefore safely retry using a new one. We should still warn
2007 * the user though...
2008 */
2009 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002010 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002011 " returned a bad sequence-id error!\n",
2012 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 exception.retry = 1;
2014 continue;
2015 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002016 /*
2017 * BAD_STATEID on OPEN means that the server cancelled our
2018 * state before it received the OPEN_CONFIRM.
2019 * Recover by retrying the request as per the discussion
2020 * on Page 181 of RFC3530.
2021 */
2022 if (status == -NFS4ERR_BAD_STATEID) {
2023 exception.retry = 1;
2024 continue;
2025 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002026 if (status == -EAGAIN) {
2027 /* We must have found a delegation */
2028 exception.retry = 1;
2029 continue;
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2032 status, &exception));
2033 } while (exception.retry);
2034 return res;
2035}
2036
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002037static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2038 struct nfs_fattr *fattr, struct iattr *sattr,
2039 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002041 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002043 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 .iap = sattr,
2045 .server = server,
2046 .bitmask = server->attr_bitmask,
2047 };
2048 struct nfs_setattrres res = {
2049 .fattr = fattr,
2050 .server = server,
2051 };
2052 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2054 .rpc_argp = &arg,
2055 .rpc_resp = &res,
2056 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002058 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002059 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Trond Myklebust0e574af2005-10-27 22:12:38 -04002061 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Trond Myklebust5d422302013-03-14 16:57:48 -04002063 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002064 struct nfs_lockowner lockowner = {
2065 .l_owner = current->files,
2066 .l_pid = current->tgid,
2067 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002068 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002069 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002070 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2071 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002072 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002073 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002074 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Bryan Schumaker7c513052011-03-24 17:12:24 +00002076 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002077 if (status == 0 && state != NULL)
2078 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002079 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2083 struct nfs_fattr *fattr, struct iattr *sattr,
2084 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002086 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002087 struct nfs4_exception exception = {
2088 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002089 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int err;
2092 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002093 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2094 switch (err) {
2095 case -NFS4ERR_OPENMODE:
2096 if (state && !(state->state & FMODE_WRITE)) {
2097 err = -EBADF;
2098 if (sattr->ia_valid & ATTR_OPEN)
2099 err = -EACCES;
2100 goto out;
2101 }
2102 }
2103 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002105out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107}
2108
2109struct nfs4_closedata {
2110 struct inode *inode;
2111 struct nfs4_state *state;
2112 struct nfs_closeargs arg;
2113 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002114 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002115 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002116 bool roc;
2117 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118};
2119
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002120static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002121{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002122 struct nfs4_closedata *calldata = data;
2123 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002124 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002125
Fred Isamanf7e89172011-01-06 11:36:32 +00002126 if (calldata->roc)
2127 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002128 nfs4_put_open_state(calldata->state);
2129 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002130 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002131 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002132 kfree(calldata);
2133}
2134
Trond Myklebust88069f72009-12-08 08:33:16 -05002135static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2136 fmode_t fmode)
2137{
2138 spin_lock(&state->owner->so_lock);
2139 if (!(fmode & FMODE_READ))
2140 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2141 if (!(fmode & FMODE_WRITE))
2142 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2143 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2144 spin_unlock(&state->owner->so_lock);
2145}
2146
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002147static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002149 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 struct nfs_server *server = NFS_SERVER(calldata->inode);
2152
Chuck Levera3ca5652012-03-01 17:00:40 -05002153 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002154 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2155 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /* hmm. we are done with the inode, and in the process of freeing
2157 * the state_owner. we keep this around to process errors
2158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 switch (task->tk_status) {
2160 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002161 if (calldata->roc)
2162 pnfs_roc_set_barrier(state->inode,
2163 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002164 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002165 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002166 nfs4_close_clear_stateid_flags(state,
2167 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002170 case -NFS4ERR_OLD_STATEID:
2171 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002173 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002176 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2177 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002179 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002180 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002181 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002184static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002186 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002187 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002188 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002189 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002190
Chuck Levera3ca5652012-03-01 17:00:40 -05002191 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002193 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002194
Trond Myklebust88069f72009-12-08 08:33:16 -05002195 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2196 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002197 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002198 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002199 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002200 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002201 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2202 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2203 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002204 }
2205 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002206 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2207 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2208 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002209 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002210 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002211 if (!nfs4_valid_open_stateid(state))
2212 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002213 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002214
2215 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002216 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002217 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002218 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002219
Fred Isamanf7e89172011-01-06 11:36:32 +00002220 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002221 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002222 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002223 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002224 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002225 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002226
Trond Myklebust516a6af2005-10-27 22:12:41 -04002227 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002228 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002229 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002230 &calldata->arg.seq_args,
2231 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002232 task) != 0)
2233 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002234 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002235 return;
2236out_no_action:
2237 task->tk_action = NULL;
2238out_wait:
2239 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002242static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002243 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002244 .rpc_call_done = nfs4_close_done,
2245 .rpc_release = nfs4_free_closedata,
2246};
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/*
2249 * It is possible for data to be read/written from a mem-mapped file
2250 * after the sys_close call (which hits the vfs layer as a flush).
2251 * This means that we can't safely call nfsv4 close on a file until
2252 * the inode is cleared. This in turn means that we are not good
2253 * NFSv4 citizens - we do not indicate to the server to update the file's
2254 * share state even when we are done with one of the three share
2255 * stateid's in the inode.
2256 *
2257 * NOTE: Caller must be holding the sp->so_owner semaphore!
2258 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002259int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002261 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002263 struct nfs4_state_owner *sp = state->owner;
2264 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2267 .rpc_cred = state->owner->so_cred,
2268 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002269 struct rpc_task_setup task_setup_data = {
2270 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002271 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002272 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002273 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002274 .flags = RPC_TASK_ASYNC,
2275 };
Trond Myklebust95121352005-10-18 14:20:12 -07002276 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002278 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002280 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002281 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002282 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002284 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002285 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002287 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002288 if (calldata->arg.seqid == NULL)
2289 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002290 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002291 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002292 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002293 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002294 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002295 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002296 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002297
Trond Myklebust1174dd12010-12-21 10:52:24 -05002298 msg.rpc_argp = &calldata->arg;
2299 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002300 task_setup_data.callback_data = calldata;
2301 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002302 if (IS_ERR(task))
2303 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002304 status = 0;
2305 if (wait)
2306 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002307 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002308 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002309out_free_calldata:
2310 kfree(calldata);
2311out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002312 nfs4_put_open_state(state);
2313 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Trond Myklebust2b484292010-09-17 10:56:51 -04002317static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002318nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct nfs4_state *state;
2321
Trond Myklebust565277f2007-10-15 18:17:53 -04002322 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002323 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2324 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002325 if (IS_ERR(state))
2326 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002327 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002328 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebust1185a552009-12-03 15:54:02 -05002331static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002332{
2333 if (ctx->state == NULL)
2334 return;
2335 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002336 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002337 else
Al Viro643168c2011-06-22 18:20:23 -04002338 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2342{
Benny Halevy43652ad2009-04-01 09:21:54 -04002343 struct nfs4_server_caps_arg args = {
2344 .fhandle = fhandle,
2345 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct nfs4_server_caps_res res = {};
2347 struct rpc_message msg = {
2348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002349 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 .rpc_resp = &res,
2351 };
2352 int status;
2353
Bryan Schumaker7c513052011-03-24 17:12:24 +00002354 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (status == 0) {
2356 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002357 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2358 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2359 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2360 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2361 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2363 server->caps |= NFS_CAP_ACLS;
2364 if (res.has_links != 0)
2365 server->caps |= NFS_CAP_HARDLINKS;
2366 if (res.has_symlinks != 0)
2367 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002368 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2369 server->caps |= NFS_CAP_FILEID;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2371 server->caps |= NFS_CAP_MODE;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2373 server->caps |= NFS_CAP_NLINK;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2375 server->caps |= NFS_CAP_OWNER;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2377 server->caps |= NFS_CAP_OWNER_GROUP;
2378 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2379 server->caps |= NFS_CAP_ATIME;
2380 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2381 server->caps |= NFS_CAP_CTIME;
2382 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2383 server->caps |= NFS_CAP_MTIME;
2384
Trond Myklebusta65318b2009-03-11 14:10:28 -04002385 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2386 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2387 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002389 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return status;
2393}
2394
Trond Myklebust55a97592006-06-09 09:34:19 -04002395int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct nfs4_exception exception = { };
2398 int err;
2399 do {
2400 err = nfs4_handle_exception(server,
2401 _nfs4_server_capabilities(server, fhandle),
2402 &exception);
2403 } while (exception.retry);
2404 return err;
2405}
2406
2407static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2408 struct nfs_fsinfo *info)
2409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct nfs4_lookup_root_arg args = {
2411 .bitmask = nfs4_fattr_bitmap,
2412 };
2413 struct nfs4_lookup_res res = {
2414 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002415 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 .fh = fhandle,
2417 };
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2420 .rpc_argp = &args,
2421 .rpc_resp = &res,
2422 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002423
Trond Myklebust0e574af2005-10-27 22:12:38 -04002424 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2429 struct nfs_fsinfo *info)
2430{
2431 struct nfs4_exception exception = { };
2432 int err;
2433 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002434 err = _nfs4_lookup_root(server, fhandle, info);
2435 switch (err) {
2436 case 0:
2437 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002438 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002439 default:
2440 err = nfs4_handle_exception(server, err, &exception);
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return err;
2445}
2446
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002447static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2448 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2449{
2450 struct rpc_auth *auth;
2451 int ret;
2452
2453 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002454 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002455 ret = -EIO;
2456 goto out;
2457 }
2458 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002459out:
2460 return ret;
2461}
2462
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002463static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002464 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002466 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002467 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002469 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002470 if (len < 0)
2471 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002472
2473 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002474 /* AUTH_UNIX is the default flavor if none was specified,
2475 * thus has already been tried. */
2476 if (flav_array[i] == RPC_AUTH_UNIX)
2477 continue;
2478
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002479 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002480 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002481 continue;
2482 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002483 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002484 /*
2485 * -EACCESS could mean that the user doesn't have correct permissions
2486 * to access the mount. It could also mean that we tried to mount
2487 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2488 * existing mount programs don't handle -EACCES very well so it should
2489 * be mapped to -EPERM instead.
2490 */
2491 if (status == -EACCES)
2492 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002493 return status;
2494}
2495
2496/*
2497 * get the file handle for the "/" directory on the server
2498 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002499int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2500 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002501{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002502 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002503 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002504 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2505 /*
2506 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2507 * by nfs4_map_errors() as this function exits.
2508 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002509 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (status == 0)
2511 status = nfs4_server_capabilities(server, fhandle);
2512 if (status == 0)
2513 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002514 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
Bryan Schumakerbae36242012-05-10 15:07:31 -04002517static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2518 struct nfs_fsinfo *info)
2519{
2520 int error;
2521 struct nfs_fattr *fattr = info->fattr;
2522
2523 error = nfs4_server_capabilities(server, mntfh);
2524 if (error < 0) {
2525 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2526 return error;
2527 }
2528
2529 error = nfs4_proc_getattr(server, mntfh, fattr);
2530 if (error < 0) {
2531 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2532 return error;
2533 }
2534
2535 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2536 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2537 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2538
2539 return error;
2540}
2541
Manoj Naik6b97fd32006-06-09 09:34:29 -04002542/*
2543 * Get locations and (maybe) other attributes of a referral.
2544 * Note that we'll actually follow the referral later when
2545 * we detect fsid mismatch in inode revalidation
2546 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002547static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2548 const struct qstr *name, struct nfs_fattr *fattr,
2549 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002550{
2551 int status = -ENOMEM;
2552 struct page *page = NULL;
2553 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002554
2555 page = alloc_page(GFP_KERNEL);
2556 if (page == NULL)
2557 goto out;
2558 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2559 if (locations == NULL)
2560 goto out;
2561
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002562 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002563 if (status != 0)
2564 goto out;
2565 /* Make sure server returned a different fsid for the referral */
2566 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002567 dprintk("%s: server did not return a different fsid for"
2568 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002569 status = -EIO;
2570 goto out;
2571 }
Andy Adamson533eb462011-06-13 18:25:56 -04002572 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2573 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002574
Andy Adamson533eb462011-06-13 18:25:56 -04002575 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002576 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002577 memset(fhandle, 0, sizeof(struct nfs_fh));
2578out:
2579 if (page)
2580 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002581 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002582 return status;
2583}
2584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2586{
2587 struct nfs4_getattr_arg args = {
2588 .fh = fhandle,
2589 .bitmask = server->attr_bitmask,
2590 };
2591 struct nfs4_getattr_res res = {
2592 .fattr = fattr,
2593 .server = server,
2594 };
2595 struct rpc_message msg = {
2596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2597 .rpc_argp = &args,
2598 .rpc_resp = &res,
2599 };
2600
Trond Myklebust0e574af2005-10-27 22:12:38 -04002601 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002602 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
2605static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2606{
2607 struct nfs4_exception exception = { };
2608 int err;
2609 do {
2610 err = nfs4_handle_exception(server,
2611 _nfs4_proc_getattr(server, fhandle, fattr),
2612 &exception);
2613 } while (exception.retry);
2614 return err;
2615}
2616
2617/*
2618 * The file is not closed if it is opened due to the a request to change
2619 * the size of the file. The open call will not be needed once the
2620 * VFS layer lookup-intents are implemented.
2621 *
2622 * Close is called when the inode is destroyed.
2623 * If we haven't opened the file for O_WRONLY, we
2624 * need to in the size_change case to obtain a stateid.
2625 *
2626 * Got race?
2627 * Because OPEN is always done by name in nfsv4, it is
2628 * possible that we opened a different file by the same
2629 * name. We can recognize this race condition, but we
2630 * can't do anything about it besides returning an error.
2631 *
2632 * This will be fixed with VFS changes (lookup-intent).
2633 */
2634static int
2635nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2636 struct iattr *sattr)
2637{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002638 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002639 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002640 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 int status;
2642
Benny Halevy8a1636c2010-07-14 15:43:57 -04002643 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002644 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002645
Trond Myklebust0e574af2005-10-27 22:12:38 -04002646 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Andy Adamson26699402012-05-30 16:12:24 -04002648 /* Deal with open(O_TRUNC) */
2649 if (sattr->ia_valid & ATTR_OPEN)
2650 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2651
2652 /* Optimization: if the end result is no change, don't RPC */
2653 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2654 return 0;
2655
Trond Myklebustd5308382005-11-04 15:33:38 -05002656 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002657 if (sattr->ia_valid & ATTR_FILE) {
2658 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002659
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002660 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002661 if (ctx) {
2662 cred = ctx->cred;
2663 state = ctx->state;
2664 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002665 }
2666
2667 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002668 if (status == 0)
2669 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return status;
2671}
2672
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002673static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2674 const struct qstr *name, struct nfs_fh *fhandle,
2675 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002676{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002677 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002678 int status;
2679 struct nfs4_lookup_arg args = {
2680 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002681 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002682 .name = name,
2683 };
2684 struct nfs4_lookup_res res = {
2685 .server = server,
2686 .fattr = fattr,
2687 .fh = fhandle,
2688 };
2689 struct rpc_message msg = {
2690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2691 .rpc_argp = &args,
2692 .rpc_resp = &res,
2693 };
2694
2695 nfs_fattr_init(fattr);
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002698 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 dprintk("NFS reply lookup: %d\n", status);
2700 return status;
2701}
2702
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002703static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002704{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002705 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002706 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002707 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2708 fattr->nlink = 2;
2709}
2710
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002711static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2712 struct qstr *name, struct nfs_fh *fhandle,
2713 struct nfs_fattr *fattr)
2714{
2715 struct nfs4_exception exception = { };
2716 struct rpc_clnt *client = *clnt;
2717 int err;
2718 do {
2719 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2720 switch (err) {
2721 case -NFS4ERR_BADNAME:
2722 err = -ENOENT;
2723 goto out;
2724 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002725 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002726 goto out;
2727 case -NFS4ERR_WRONGSEC:
2728 err = -EPERM;
2729 if (client != *clnt)
2730 goto out;
2731
2732 client = nfs4_create_sec_client(client, dir, name);
2733 if (IS_ERR(client))
2734 return PTR_ERR(client);
2735
2736 exception.retry = 1;
2737 break;
2738 default:
2739 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2740 }
2741 } while (exception.retry);
2742
2743out:
2744 if (err == 0)
2745 *clnt = client;
2746 else if (client != *clnt)
2747 rpc_shutdown_client(client);
2748
2749 return err;
2750}
2751
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002752static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002753 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002755 int status;
2756 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002757
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002758 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2759 if (client != NFS_CLIENT(dir)) {
2760 rpc_shutdown_client(client);
2761 nfs_fixup_secinfo_attributes(fattr);
2762 }
2763 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002766struct rpc_clnt *
2767nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2768 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2769{
2770 int status;
2771 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2772
2773 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2774 if (status < 0) {
2775 rpc_shutdown_client(client);
2776 return ERR_PTR(status);
2777 }
2778 return client;
2779}
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2782{
Trond Myklebust76b32992007-08-10 17:45:11 -04002783 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct nfs4_accessargs args = {
2785 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002786 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002788 struct nfs4_accessres res = {
2789 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002790 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct rpc_message msg = {
2792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2793 .rpc_argp = &args,
2794 .rpc_resp = &res,
2795 .rpc_cred = entry->cred,
2796 };
2797 int mode = entry->mask;
2798 int status;
2799
2800 /*
2801 * Determine which access bits we want to ask for...
2802 */
2803 if (mode & MAY_READ)
2804 args.access |= NFS4_ACCESS_READ;
2805 if (S_ISDIR(inode->i_mode)) {
2806 if (mode & MAY_WRITE)
2807 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2808 if (mode & MAY_EXEC)
2809 args.access |= NFS4_ACCESS_LOOKUP;
2810 } else {
2811 if (mode & MAY_WRITE)
2812 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2813 if (mode & MAY_EXEC)
2814 args.access |= NFS4_ACCESS_EXECUTE;
2815 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002816
2817 res.fattr = nfs_alloc_fattr();
2818 if (res.fattr == NULL)
2819 return -ENOMEM;
2820
Bryan Schumaker7c513052011-03-24 17:12:24 +00002821 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002823 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002824 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002826 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return status;
2828}
2829
2830static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2831{
2832 struct nfs4_exception exception = { };
2833 int err;
2834 do {
2835 err = nfs4_handle_exception(NFS_SERVER(inode),
2836 _nfs4_proc_access(inode, entry),
2837 &exception);
2838 } while (exception.retry);
2839 return err;
2840}
2841
2842/*
2843 * TODO: For the time being, we don't try to get any attributes
2844 * along with any of the zero-copy operations READ, READDIR,
2845 * READLINK, WRITE.
2846 *
2847 * In the case of the first three, we want to put the GETATTR
2848 * after the read-type operation -- this is because it is hard
2849 * to predict the length of a GETATTR response in v4, and thus
2850 * align the READ data correctly. This means that the GETATTR
2851 * may end up partially falling into the page cache, and we should
2852 * shift it into the 'tail' of the xdr_buf before processing.
2853 * To do this efficiently, we need to know the total length
2854 * of data received, which doesn't seem to be available outside
2855 * of the RPC layer.
2856 *
2857 * In the case of WRITE, we also want to put the GETATTR after
2858 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002859 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 *
2861 * Both of these changes to the XDR layer would in fact be quite
2862 * minor, but I decided to leave them for a subsequent patch.
2863 */
2864static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2865 unsigned int pgbase, unsigned int pglen)
2866{
2867 struct nfs4_readlink args = {
2868 .fh = NFS_FH(inode),
2869 .pgbase = pgbase,
2870 .pglen = pglen,
2871 .pages = &page,
2872 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002873 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 struct rpc_message msg = {
2875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2876 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002877 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 };
2879
Bryan Schumaker7c513052011-03-24 17:12:24 +00002880 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 -07002881}
2882
2883static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2884 unsigned int pgbase, unsigned int pglen)
2885{
2886 struct nfs4_exception exception = { };
2887 int err;
2888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(inode),
2890 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2891 &exception);
2892 } while (exception.retry);
2893 return err;
2894}
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002897 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899static int
2900nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002901 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002903 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 int status = 0;
2906
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002907 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2908 if (IS_ERR(ctx))
2909 return PTR_ERR(ctx);
2910
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002911 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002912 state = nfs4_do_open(dir, dentry, ctx->mode,
2913 flags, sattr, ctx->cred,
2914 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002915 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (IS_ERR(state)) {
2917 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002920 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002921 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002922 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002924 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return status;
2926}
2927
2928static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2929{
Trond Myklebust16e42952005-10-27 22:12:44 -04002930 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002931 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002933 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002935 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002936 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002937 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2940 .rpc_argp = &args,
2941 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002943 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002944
Bryan Schumaker7c513052011-03-24 17:12:24 +00002945 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002946 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002947 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return status;
2949}
2950
2951static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2952{
2953 struct nfs4_exception exception = { };
2954 int err;
2955 do {
2956 err = nfs4_handle_exception(NFS_SERVER(dir),
2957 _nfs4_proc_remove(dir, name),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002963static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002965 struct nfs_server *server = NFS_SERVER(dir);
2966 struct nfs_removeargs *args = msg->rpc_argp;
2967 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002969 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002971 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002974static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2975{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04002976 nfs4_setup_sequence(NFS_SERVER(data->dir),
2977 &data->args.seq_args,
2978 &data->res.seq_res,
2979 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002982static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002984 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2985
Trond Myklebust14516c32010-07-31 14:29:06 -04002986 if (!nfs4_sequence_done(task, &res->seq_res))
2987 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002988 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002989 return 0;
2990 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002991 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
Jeff Laytond3d41522010-09-17 17:31:57 -04002994static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2995{
2996 struct nfs_server *server = NFS_SERVER(dir);
2997 struct nfs_renameargs *arg = msg->rpc_argp;
2998 struct nfs_renameres *res = msg->rpc_resp;
2999
3000 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003001 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003002 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003003}
3004
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003005static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3006{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003007 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3008 &data->args.seq_args,
3009 &data->res.seq_res,
3010 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003011}
3012
3013static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3014 struct inode *new_dir)
3015{
3016 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3017
3018 if (!nfs4_sequence_done(task, &res->seq_res))
3019 return 0;
3020 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3021 return 0;
3022
3023 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003024 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003025 return 1;
3026}
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3029 struct inode *new_dir, struct qstr *new_name)
3030{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003031 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003032 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 .old_dir = NFS_FH(old_dir),
3034 .new_dir = NFS_FH(new_dir),
3035 .old_name = old_name,
3036 .new_name = new_name,
3037 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003038 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003039 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3043 .rpc_argp = &arg,
3044 .rpc_resp = &res,
3045 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003046 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Bryan Schumaker7c513052011-03-24 17:12:24 +00003048 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 if (!status) {
3050 update_changeattr(old_dir, &res.old_cinfo);
3051 update_changeattr(new_dir, &res.new_cinfo);
3052 }
3053 return status;
3054}
3055
3056static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3057 struct inode *new_dir, struct qstr *new_name)
3058{
3059 struct nfs4_exception exception = { };
3060 int err;
3061 do {
3062 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3063 _nfs4_proc_rename(old_dir, old_name,
3064 new_dir, new_name),
3065 &exception);
3066 } while (exception.retry);
3067 return err;
3068}
3069
3070static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3071{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003072 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct nfs4_link_arg arg = {
3074 .fh = NFS_FH(inode),
3075 .dir_fh = NFS_FH(dir),
3076 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003077 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003079 struct nfs4_link_res res = {
3080 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003081 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct rpc_message msg = {
3083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3084 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003085 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003087 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Trond Myklebust136f2622010-04-16 16:22:49 -04003089 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003090 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003091 goto out;
3092
Bryan Schumaker7c513052011-03-24 17:12:24 +00003093 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003094 if (!status) {
3095 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003096 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003097 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003098out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003099 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return status;
3101}
3102
3103static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3104{
3105 struct nfs4_exception exception = { };
3106 int err;
3107 do {
3108 err = nfs4_handle_exception(NFS_SERVER(inode),
3109 _nfs4_proc_link(inode, dir, name),
3110 &exception);
3111 } while (exception.retry);
3112 return err;
3113}
3114
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003115struct nfs4_createdata {
3116 struct rpc_message msg;
3117 struct nfs4_create_arg arg;
3118 struct nfs4_create_res res;
3119 struct nfs_fh fh;
3120 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003121};
3122
3123static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3124 struct qstr *name, struct iattr *sattr, u32 ftype)
3125{
3126 struct nfs4_createdata *data;
3127
3128 data = kzalloc(sizeof(*data), GFP_KERNEL);
3129 if (data != NULL) {
3130 struct nfs_server *server = NFS_SERVER(dir);
3131
3132 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3133 data->msg.rpc_argp = &data->arg;
3134 data->msg.rpc_resp = &data->res;
3135 data->arg.dir_fh = NFS_FH(dir);
3136 data->arg.server = server;
3137 data->arg.name = name;
3138 data->arg.attrs = sattr;
3139 data->arg.ftype = ftype;
3140 data->arg.bitmask = server->attr_bitmask;
3141 data->res.server = server;
3142 data->res.fh = &data->fh;
3143 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003144 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003145 }
3146 return data;
3147}
3148
3149static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3150{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003151 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003152 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003153 if (status == 0) {
3154 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003155 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3156 }
3157 return status;
3158}
3159
3160static void nfs4_free_createdata(struct nfs4_createdata *data)
3161{
3162 kfree(data);
3163}
3164
Chuck Lever4f390c12006-08-22 20:06:22 -04003165static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003166 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003168 struct nfs4_createdata *data;
3169 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Chuck Lever94a6d752006-08-22 20:06:23 -04003171 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003173
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003174 status = -ENOMEM;
3175 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3176 if (data == NULL)
3177 goto out;
3178
3179 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3180 data->arg.u.symlink.pages = &page;
3181 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003183 status = nfs4_do_create(dir, dentry, data);
3184
3185 nfs4_free_createdata(data);
3186out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return status;
3188}
3189
Chuck Lever4f390c12006-08-22 20:06:22 -04003190static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003191 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct nfs4_exception exception = { };
3194 int err;
3195 do {
3196 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003197 _nfs4_proc_symlink(dir, dentry, page,
3198 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 &exception);
3200 } while (exception.retry);
3201 return err;
3202}
3203
3204static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3205 struct iattr *sattr)
3206{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 struct nfs4_createdata *data;
3208 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3211 if (data == NULL)
3212 goto out;
3213
3214 status = nfs4_do_create(dir, dentry, data);
3215
3216 nfs4_free_createdata(data);
3217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return status;
3219}
3220
3221static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3222 struct iattr *sattr)
3223{
3224 struct nfs4_exception exception = { };
3225 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003226
3227 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 do {
3229 err = nfs4_handle_exception(NFS_SERVER(dir),
3230 _nfs4_proc_mkdir(dir, dentry, sattr),
3231 &exception);
3232 } while (exception.retry);
3233 return err;
3234}
3235
3236static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003237 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 struct inode *dir = dentry->d_inode;
3240 struct nfs4_readdir_arg args = {
3241 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003242 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 .pgbase = 0,
3244 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003245 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003246 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 };
3248 struct nfs4_readdir_res res;
3249 struct rpc_message msg = {
3250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3251 .rpc_argp = &args,
3252 .rpc_resp = &res,
3253 .rpc_cred = cred,
3254 };
3255 int status;
3256
Harvey Harrison3110ff82008-05-02 13:42:44 -07003257 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003258 dentry->d_parent->d_name.name,
3259 dentry->d_name.name,
3260 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003261 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003263 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 -05003264 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003265 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003266 status += args.pgbase;
3267 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003268
3269 nfs_invalidate_atime(dir);
3270
Harvey Harrison3110ff82008-05-02 13:42:44 -07003271 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return status;
3273}
3274
3275static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003276 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct nfs4_exception exception = { };
3279 int err;
3280 do {
3281 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3282 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003283 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 &exception);
3285 } while (exception.retry);
3286 return err;
3287}
3288
3289static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3290 struct iattr *sattr, dev_t rdev)
3291{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003292 struct nfs4_createdata *data;
3293 int mode = sattr->ia_mode;
3294 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003296 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3297 if (data == NULL)
3298 goto out;
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003301 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003303 data->arg.ftype = NF4BLK;
3304 data->arg.u.device.specdata1 = MAJOR(rdev);
3305 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003308 data->arg.ftype = NF4CHR;
3309 data->arg.u.device.specdata1 = MAJOR(rdev);
3310 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003311 } else if (!S_ISSOCK(mode)) {
3312 status = -EINVAL;
3313 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003316 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003317out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003318 nfs4_free_createdata(data);
3319out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return status;
3321}
3322
3323static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3324 struct iattr *sattr, dev_t rdev)
3325{
3326 struct nfs4_exception exception = { };
3327 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003328
3329 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 do {
3331 err = nfs4_handle_exception(NFS_SERVER(dir),
3332 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3333 &exception);
3334 } while (exception.retry);
3335 return err;
3336}
3337
3338static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3339 struct nfs_fsstat *fsstat)
3340{
3341 struct nfs4_statfs_arg args = {
3342 .fh = fhandle,
3343 .bitmask = server->attr_bitmask,
3344 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003345 struct nfs4_statfs_res res = {
3346 .fsstat = fsstat,
3347 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct rpc_message msg = {
3349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3350 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003351 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 };
3353
Trond Myklebust0e574af2005-10-27 22:12:38 -04003354 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003355 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
3358static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3359{
3360 struct nfs4_exception exception = { };
3361 int err;
3362 do {
3363 err = nfs4_handle_exception(server,
3364 _nfs4_proc_statfs(server, fhandle, fsstat),
3365 &exception);
3366 } while (exception.retry);
3367 return err;
3368}
3369
3370static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3371 struct nfs_fsinfo *fsinfo)
3372{
3373 struct nfs4_fsinfo_arg args = {
3374 .fh = fhandle,
3375 .bitmask = server->attr_bitmask,
3376 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003377 struct nfs4_fsinfo_res res = {
3378 .fsinfo = fsinfo,
3379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 struct rpc_message msg = {
3381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3382 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003383 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 };
3385
Bryan Schumaker7c513052011-03-24 17:12:24 +00003386 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
3389static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3390{
3391 struct nfs4_exception exception = { };
3392 int err;
3393
3394 do {
3395 err = nfs4_handle_exception(server,
3396 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3397 &exception);
3398 } while (exception.retry);
3399 return err;
3400}
3401
3402static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3403{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003404 int error;
3405
Trond Myklebust0e574af2005-10-27 22:12:38 -04003406 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003407 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003408 if (error == 0) {
3409 /* block layout checks this! */
3410 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003411 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003412 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003413
3414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
3417static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3418 struct nfs_pathconf *pathconf)
3419{
3420 struct nfs4_pathconf_arg args = {
3421 .fh = fhandle,
3422 .bitmask = server->attr_bitmask,
3423 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003424 struct nfs4_pathconf_res res = {
3425 .pathconf = pathconf,
3426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct rpc_message msg = {
3428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3429 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003430 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 };
3432
3433 /* None of the pathconf attributes are mandatory to implement */
3434 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3435 memset(pathconf, 0, sizeof(*pathconf));
3436 return 0;
3437 }
3438
Trond Myklebust0e574af2005-10-27 22:12:38 -04003439 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003440 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
3443static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3444 struct nfs_pathconf *pathconf)
3445{
3446 struct nfs4_exception exception = { };
3447 int err;
3448
3449 do {
3450 err = nfs4_handle_exception(server,
3451 _nfs4_proc_pathconf(server, fhandle, pathconf),
3452 &exception);
3453 } while (exception.retry);
3454 return err;
3455}
3456
Trond Myklebust5521abf2013-03-16 20:54:34 -04003457int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003458 const struct nfs_open_context *ctx,
3459 const struct nfs_lock_context *l_ctx,
3460 fmode_t fmode)
3461{
3462 const struct nfs_lockowner *lockowner = NULL;
3463
3464 if (l_ctx != NULL)
3465 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003466 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003467}
3468EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3469
Trond Myklebust5521abf2013-03-16 20:54:34 -04003470static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3471 const struct nfs_open_context *ctx,
3472 const struct nfs_lock_context *l_ctx,
3473 fmode_t fmode)
3474{
3475 nfs4_stateid current_stateid;
3476
3477 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3478 return false;
3479 return nfs4_stateid_match(stateid, &current_stateid);
3480}
3481
3482static bool nfs4_error_stateid_expired(int err)
3483{
3484 switch (err) {
3485 case -NFS4ERR_DELEG_REVOKED:
3486 case -NFS4ERR_ADMIN_REVOKED:
3487 case -NFS4ERR_BAD_STATEID:
3488 case -NFS4ERR_STALE_STATEID:
3489 case -NFS4ERR_OLD_STATEID:
3490 case -NFS4ERR_OPENMODE:
3491 case -NFS4ERR_EXPIRED:
3492 return true;
3493 }
3494 return false;
3495}
3496
Benny Halevyd20581a2011-05-22 19:52:03 +03003497void __nfs4_read_done_cb(struct nfs_read_data *data)
3498{
Fred Isamancd841602012-04-20 14:47:44 -04003499 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003500}
3501
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003502static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
Fred Isamancd841602012-04-20 14:47:44 -04003504 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003506 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003507 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003508 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003510
Benny Halevyd20581a2011-05-22 19:52:03 +03003511 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003513 renew_lease(server, data->timestamp);
3514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Trond Myklebust5521abf2013-03-16 20:54:34 -04003517static bool nfs4_read_stateid_changed(struct rpc_task *task,
3518 struct nfs_readargs *args)
3519{
3520
3521 if (!nfs4_error_stateid_expired(task->tk_status) ||
3522 nfs4_stateid_is_current(&args->stateid,
3523 args->context,
3524 args->lock_context,
3525 FMODE_READ))
3526 return false;
3527 rpc_restart_call_prepare(task);
3528 return true;
3529}
3530
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003531static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3532{
3533
3534 dprintk("--> %s\n", __func__);
3535
3536 if (!nfs4_sequence_done(task, &data->res.seq_res))
3537 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003538 if (nfs4_read_stateid_changed(task, &data->args))
3539 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003540 return data->read_done_cb ? data->read_done_cb(task, data) :
3541 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003542}
3543
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003544static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003547 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003548 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003549 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003552static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3553{
Trond Myklebust9b206142013-03-17 15:52:00 -04003554 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003555 &data->args.seq_args,
3556 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003557 task))
3558 return;
3559 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3560 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Fred Isamanb029bc92011-03-03 15:13:42 +00003563static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Fred Isamancd841602012-04-20 14:47:44 -04003565 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003567 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003568 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003569 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003571 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003573 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003574 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebust5521abf2013-03-16 20:54:34 -04003578static bool nfs4_write_stateid_changed(struct rpc_task *task,
3579 struct nfs_writeargs *args)
3580{
3581
3582 if (!nfs4_error_stateid_expired(task->tk_status) ||
3583 nfs4_stateid_is_current(&args->stateid,
3584 args->context,
3585 args->lock_context,
3586 FMODE_WRITE))
3587 return false;
3588 rpc_restart_call_prepare(task);
3589 return true;
3590}
3591
Fred Isamanb029bc92011-03-03 15:13:42 +00003592static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3593{
3594 if (!nfs4_sequence_done(task, &data->res.seq_res))
3595 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003596 if (nfs4_write_stateid_changed(task, &data->args))
3597 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003598 return data->write_done_cb ? data->write_done_cb(task, data) :
3599 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003600}
3601
Trond Myklebust5a37f852012-04-28 14:55:16 -04003602static
3603bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003604{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003605 const struct nfs_pgio_header *hdr = data->header;
3606
3607 /* Don't request attributes for pNFS or O_DIRECT writes */
3608 if (data->ds_clp != NULL || hdr->dreq != NULL)
3609 return false;
3610 /* Otherwise, request attributes if and only if we don't hold
3611 * a delegation
3612 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003613 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003614}
Fred Isamana69aef12011-03-03 15:13:47 +00003615
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003616static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Fred Isamancd841602012-04-20 14:47:44 -04003618 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003619
Trond Myklebust5a37f852012-04-28 14:55:16 -04003620 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003621 data->args.bitmask = NULL;
3622 data->res.fattr = NULL;
3623 } else
3624 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003625
Fred Isamanb029bc92011-03-03 15:13:42 +00003626 if (!data->write_done_cb)
3627 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003628 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 data->timestamp = jiffies;
3630
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003631 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003632 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003635static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3636{
Trond Myklebust9b206142013-03-17 15:52:00 -04003637 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003638 &data->args.seq_args,
3639 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003640 task))
3641 return;
3642 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3643 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644}
3645
Fred Isaman0b7c0152012-04-20 14:47:39 -04003646static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3647{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003648 nfs4_setup_sequence(NFS_SERVER(data->inode),
3649 &data->args.seq_args,
3650 &data->res.seq_res,
3651 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Fred Isaman0b7c0152012-04-20 14:47:39 -04003654static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003657
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003658 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003659 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003660 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
Fred Isaman0b7c0152012-04-20 14:47:39 -04003665static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003666{
3667 if (!nfs4_sequence_done(task, &data->res.seq_res))
3668 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003669 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003670}
3671
Fred Isaman0b7c0152012-04-20 14:47:39 -04003672static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003674 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003675
Fred Isaman0b7c0152012-04-20 14:47:39 -04003676 if (data->commit_done_cb == NULL)
3677 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003678 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003680 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003683struct nfs4_renewdata {
3684 struct nfs_client *client;
3685 unsigned long timestamp;
3686};
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/*
3689 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3690 * standalone procedure for queueing an asynchronous RENEW.
3691 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003692static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003693{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003694 struct nfs4_renewdata *data = calldata;
3695 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003696
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003697 if (atomic_read(&clp->cl_count) > 1)
3698 nfs4_schedule_state_renewal(clp);
3699 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003700 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003701}
3702
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003703static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003705 struct nfs4_renewdata *data = calldata;
3706 struct nfs_client *clp = data->client;
3707 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003710 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003711 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3712 return;
3713 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003714 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003715 return;
3716 }
3717 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003719 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003722static const struct rpc_call_ops nfs4_renew_ops = {
3723 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003724 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003725};
3726
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003727static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 struct rpc_message msg = {
3730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3731 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003732 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003734 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003736 if (renew_flags == 0)
3737 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003738 if (!atomic_inc_not_zero(&clp->cl_count))
3739 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003740 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003741 if (data == NULL)
3742 return -ENOMEM;
3743 data->client = clp;
3744 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003746 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003749static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct rpc_message msg = {
3752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3753 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003754 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 };
3756 unsigned long now = jiffies;
3757 int status;
3758
3759 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3760 if (status < 0)
3761 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003762 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return 0;
3764}
3765
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003766static inline int nfs4_server_supports_acls(struct nfs_server *server)
3767{
3768 return (server->caps & NFS_CAP_ACLS)
3769 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3770 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3771}
3772
Trond Myklebust21f498c2012-08-24 10:59:25 -04003773/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3774 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003775 * the stack.
3776 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003777#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003778
Neil Hormane9e3d722011-03-04 19:26:03 -05003779static int buf_to_pages_noslab(const void *buf, size_t buflen,
3780 struct page **pages, unsigned int *pgbase)
3781{
3782 struct page *newpage, **spages;
3783 int rc = 0;
3784 size_t len;
3785 spages = pages;
3786
3787 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003788 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003789 newpage = alloc_page(GFP_KERNEL);
3790
3791 if (newpage == NULL)
3792 goto unwind;
3793 memcpy(page_address(newpage), buf, len);
3794 buf += len;
3795 buflen -= len;
3796 *pages++ = newpage;
3797 rc++;
3798 } while (buflen != 0);
3799
3800 return rc;
3801
3802unwind:
3803 for(; rc > 0; rc--)
3804 __free_page(spages[rc-1]);
3805 return -ENOMEM;
3806}
3807
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808struct nfs4_cached_acl {
3809 int cached;
3810 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003811 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812};
3813
3814static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 struct nfs_inode *nfsi = NFS_I(inode);
3817
3818 spin_lock(&inode->i_lock);
3819 kfree(nfsi->nfs4_acl);
3820 nfsi->nfs4_acl = acl;
3821 spin_unlock(&inode->i_lock);
3822}
3823
3824static void nfs4_zap_acl_attr(struct inode *inode)
3825{
3826 nfs4_set_cached_acl(inode, NULL);
3827}
3828
3829static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3830{
3831 struct nfs_inode *nfsi = NFS_I(inode);
3832 struct nfs4_cached_acl *acl;
3833 int ret = -ENOENT;
3834
3835 spin_lock(&inode->i_lock);
3836 acl = nfsi->nfs4_acl;
3837 if (acl == NULL)
3838 goto out;
3839 if (buf == NULL) /* user is just asking for length */
3840 goto out_len;
3841 if (acl->cached == 0)
3842 goto out;
3843 ret = -ERANGE; /* see getxattr(2) man page */
3844 if (acl->len > buflen)
3845 goto out;
3846 memcpy(buf, acl->data, acl->len);
3847out_len:
3848 ret = acl->len;
3849out:
3850 spin_unlock(&inode->i_lock);
3851 return ret;
3852}
3853
Sachin Prabhu5794d212012-04-17 14:36:40 +01003854static 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 +00003855{
3856 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003857 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003858
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003859 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003860 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003861 if (acl == NULL)
3862 goto out;
3863 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003864 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003865 } else {
3866 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3867 if (acl == NULL)
3868 goto out;
3869 acl->cached = 0;
3870 }
3871 acl->len = acl_len;
3872out:
3873 nfs4_set_cached_acl(inode, acl);
3874}
3875
Andy Adamsonbf118a32011-12-07 11:55:27 -05003876/*
3877 * The getxattr API returns the required buffer length when called with a
3878 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3879 * the required buf. On a NULL buf, we send a page of data to the server
3880 * guessing that the ACL request can be serviced by a page. If so, we cache
3881 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3882 * the cache. If not so, we throw away the page, and cache the required
3883 * length. The next getxattr call will then produce another round trip to
3884 * the server, this time with the input buf of the required size.
3885 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003886static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003887{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003888 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003889 struct nfs_getaclargs args = {
3890 .fh = NFS_FH(inode),
3891 .acl_pages = pages,
3892 .acl_len = buflen,
3893 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003894 struct nfs_getaclres res = {
3895 .acl_len = buflen,
3896 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3899 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003900 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003901 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003902 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3903 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003904
Andy Adamsonbf118a32011-12-07 11:55:27 -05003905 /* As long as we're doing a round trip to the server anyway,
3906 * let's be prepared for a page of acl data. */
3907 if (npages == 0)
3908 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003909 if (npages > ARRAY_SIZE(pages))
3910 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003911
Andy Adamsonbf118a32011-12-07 11:55:27 -05003912 for (i = 0; i < npages; i++) {
3913 pages[i] = alloc_page(GFP_KERNEL);
3914 if (!pages[i])
3915 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003916 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003917
3918 /* for decoding across pages */
3919 res.acl_scratch = alloc_page(GFP_KERNEL);
3920 if (!res.acl_scratch)
3921 goto out_free;
3922
Andy Adamsonbf118a32011-12-07 11:55:27 -05003923 args.acl_len = npages * PAGE_SIZE;
3924 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003925
Peng Taode040be2012-01-10 22:42:47 +08003926 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003927 __func__, buf, buflen, npages, args.acl_len);
3928 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3929 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003930 if (ret)
3931 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003932
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003933 /* Handle the case where the passed-in buffer is too short */
3934 if (res.acl_flags & NFS4_ACL_TRUNC) {
3935 /* Did the user only issue a request for the acl length? */
3936 if (buf == NULL)
3937 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003938 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003939 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003940 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003941 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003942 if (buf) {
3943 if (res.acl_len > buflen) {
3944 ret = -ERANGE;
3945 goto out_free;
3946 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003947 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003948 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003949out_ok:
3950 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003951out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003952 for (i = 0; i < npages; i++)
3953 if (pages[i])
3954 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003955 if (res.acl_scratch)
3956 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003957 return ret;
3958}
3959
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003960static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3961{
3962 struct nfs4_exception exception = { };
3963 ssize_t ret;
3964 do {
3965 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3966 if (ret >= 0)
3967 break;
3968 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3969 } while (exception.retry);
3970 return ret;
3971}
3972
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003973static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3974{
3975 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003976 int ret;
3977
3978 if (!nfs4_server_supports_acls(server))
3979 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003980 ret = nfs_revalidate_inode(server, inode);
3981 if (ret < 0)
3982 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003983 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3984 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003985 ret = nfs4_read_cached_acl(inode, buf, buflen);
3986 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003987 /* -ENOENT is returned if there is no ACL or if there is an ACL
3988 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003989 return ret;
3990 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003991}
3992
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003993static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003994{
3995 struct nfs_server *server = NFS_SERVER(inode);
3996 struct page *pages[NFS4ACL_MAXPAGES];
3997 struct nfs_setaclargs arg = {
3998 .fh = NFS_FH(inode),
3999 .acl_pages = pages,
4000 .acl_len = buflen,
4001 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004002 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004003 struct rpc_message msg = {
4004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4005 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004006 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004007 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004008 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004009 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004010
4011 if (!nfs4_server_supports_acls(server))
4012 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004013 if (npages > ARRAY_SIZE(pages))
4014 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004015 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4016 if (i < 0)
4017 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004018 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004019 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004020
4021 /*
4022 * Free each page after tx, so the only ref left is
4023 * held by the network stack
4024 */
4025 for (; i > 0; i--)
4026 put_page(pages[i-1]);
4027
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004028 /*
4029 * Acl update can result in inode attribute update.
4030 * so mark the attribute cache invalid.
4031 */
4032 spin_lock(&inode->i_lock);
4033 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4034 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004035 nfs_access_zap_cache(inode);
4036 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004037 return ret;
4038}
4039
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004040static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4041{
4042 struct nfs4_exception exception = { };
4043 int err;
4044 do {
4045 err = nfs4_handle_exception(NFS_SERVER(inode),
4046 __nfs4_proc_set_acl(inode, buf, buflen),
4047 &exception);
4048 } while (exception.retry);
4049 return err;
4050}
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004053nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004055 struct nfs_client *clp = server->nfs_client;
4056
4057 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return 0;
4059 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004060 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004061 case -NFS4ERR_ADMIN_REVOKED:
4062 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004063 if (state == NULL)
4064 break;
4065 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004066 case -NFS4ERR_OPENMODE:
4067 if (state == NULL)
4068 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004069 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4070 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004071 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004072 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004073 if (state != NULL) {
4074 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4075 goto stateid_invalid;
4076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004078 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004079 nfs4_schedule_lease_recovery(clp);
4080 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004081#if defined(CONFIG_NFS_V4_1)
4082 case -NFS4ERR_BADSESSION:
4083 case -NFS4ERR_BADSLOT:
4084 case -NFS4ERR_BAD_HIGH_SLOT:
4085 case -NFS4ERR_DEADSESSION:
4086 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4087 case -NFS4ERR_SEQ_FALSE_RETRY:
4088 case -NFS4ERR_SEQ_MISORDERED:
4089 dprintk("%s ERROR %d, Reset session\n", __func__,
4090 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004091 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004092 task->tk_status = 0;
4093 return -EAGAIN;
4094#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004096 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004097 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4099 task->tk_status = 0;
4100 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004101 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 case -NFS4ERR_OLD_STATEID:
4103 task->tk_status = 0;
4104 return -EAGAIN;
4105 }
4106 task->tk_status = nfs4_map_errors(task->tk_status);
4107 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004108stateid_invalid:
4109 task->tk_status = -EIO;
4110 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004111wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004112 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004113 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4114 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4115 task->tk_status = 0;
4116 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
Chuck Leverf0920752012-05-21 22:45:41 -04004119static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4120 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004121{
4122 __be32 verf[2];
4123
Chuck Lever2c820d92012-05-21 22:45:33 -04004124 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4125 /* An impossible timestamp guarantees this value
4126 * will never match a generated boot time. */
4127 verf[0] = 0;
4128 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4129 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004130 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4131 verf[0] = (__be32)nn->boot_time.tv_sec;
4132 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004133 }
Chuck Levercd937102012-03-02 17:14:31 -05004134 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4135}
4136
Chuck Levere984a552012-09-14 17:24:21 -04004137static unsigned int
4138nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4139 char *buf, size_t len)
4140{
4141 unsigned int result;
4142
4143 rcu_read_lock();
4144 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4145 clp->cl_ipaddr,
4146 rpc_peeraddr2str(clp->cl_rpcclient,
4147 RPC_DISPLAY_ADDR),
4148 rpc_peeraddr2str(clp->cl_rpcclient,
4149 RPC_DISPLAY_PROTO));
4150 rcu_read_unlock();
4151 return result;
4152}
4153
4154static unsigned int
4155nfs4_init_uniform_client_string(const struct nfs_client *clp,
4156 char *buf, size_t len)
4157{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004158 char *nodename = clp->cl_rpcclient->cl_nodename;
4159
4160 if (nfs4_client_id_uniquifier[0] != '\0')
4161 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004162 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4163 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004164 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004165}
4166
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004167/**
4168 * nfs4_proc_setclientid - Negotiate client ID
4169 * @clp: state data structure
4170 * @program: RPC program for NFSv4 callback service
4171 * @port: IP port number for NFS4 callback service
4172 * @cred: RPC credential to use for this call
4173 * @res: where to place the result
4174 *
4175 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4176 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004177int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4178 unsigned short port, struct rpc_cred *cred,
4179 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
4181 nfs4_verifier sc_verifier;
4182 struct nfs4_setclientid setclientid = {
4183 .sc_verifier = &sc_verifier,
4184 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004185 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 };
4187 struct rpc_message msg = {
4188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4189 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004190 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004191 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004193 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Chuck Leverde734832012-07-11 16:30:50 -04004195 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004196 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004197 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4198 setclientid.sc_name_len =
4199 nfs4_init_uniform_client_string(clp,
4200 setclientid.sc_name,
4201 sizeof(setclientid.sc_name));
4202 else
4203 setclientid.sc_name_len =
4204 nfs4_init_nonuniform_client_string(clp,
4205 setclientid.sc_name,
4206 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004207 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004208 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004209 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004210 sizeof(setclientid.sc_netid),
4211 rpc_peeraddr2str(clp->cl_rpcclient,
4212 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004213 rcu_read_unlock();
4214 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004215 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 clp->cl_ipaddr, port >> 8, port & 255);
4217
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004218 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4219 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4220 setclientid.sc_name_len, setclientid.sc_name);
4221 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4222 dprintk("NFS reply setclientid: %d\n", status);
4223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004226/**
4227 * nfs4_proc_setclientid_confirm - Confirm client ID
4228 * @clp: state data structure
4229 * @res: result of a previous SETCLIENTID
4230 * @cred: RPC credential to use for this call
4231 *
4232 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4233 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004234int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004235 struct nfs4_setclientid_res *arg,
4236 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 struct nfs_fsinfo fsinfo;
4239 struct rpc_message msg = {
4240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004241 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004243 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 };
4245 unsigned long now;
4246 int status;
4247
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004248 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4249 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4250 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004252 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (status == 0) {
4254 spin_lock(&clp->cl_lock);
4255 clp->cl_lease_time = fsinfo.lease_time * HZ;
4256 clp->cl_last_renewal = now;
4257 spin_unlock(&clp->cl_lock);
4258 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004259 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return status;
4261}
4262
Trond Myklebustfe650402006-01-03 09:55:18 +01004263struct nfs4_delegreturndata {
4264 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004265 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004266 struct nfs_fh fh;
4267 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004268 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004269 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004270 int rpc_status;
4271};
4272
Trond Myklebustfe650402006-01-03 09:55:18 +01004273static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4274{
4275 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004276
Trond Myklebust14516c32010-07-31 14:29:06 -04004277 if (!nfs4_sequence_done(task, &data->res.seq_res))
4278 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004279
Ricardo Labiaga79708862009-12-07 09:23:21 -05004280 switch (task->tk_status) {
4281 case -NFS4ERR_STALE_STATEID:
4282 case -NFS4ERR_EXPIRED:
4283 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004284 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004285 break;
4286 default:
4287 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4288 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004289 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004290 return;
4291 }
4292 }
4293 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004294}
4295
4296static void nfs4_delegreturn_release(void *calldata)
4297{
Trond Myklebustfe650402006-01-03 09:55:18 +01004298 kfree(calldata);
4299}
4300
Andy Adamson938e1012009-04-01 09:22:28 -04004301#if defined(CONFIG_NFS_V4_1)
4302static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4303{
4304 struct nfs4_delegreturndata *d_data;
4305
4306 d_data = (struct nfs4_delegreturndata *)data;
4307
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004308 nfs4_setup_sequence(d_data->res.server,
4309 &d_data->args.seq_args,
4310 &d_data->res.seq_res,
4311 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004312}
4313#endif /* CONFIG_NFS_V4_1 */
4314
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004315static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004316#if defined(CONFIG_NFS_V4_1)
4317 .rpc_call_prepare = nfs4_delegreturn_prepare,
4318#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004319 .rpc_call_done = nfs4_delegreturn_done,
4320 .rpc_release = nfs4_delegreturn_release,
4321};
4322
Trond Myklebuste6f81072008-01-24 18:14:34 -05004323static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004324{
4325 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004326 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004327 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4330 .rpc_cred = cred,
4331 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004332 struct rpc_task_setup task_setup_data = {
4333 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004334 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004335 .callback_ops = &nfs4_delegreturn_ops,
4336 .flags = RPC_TASK_ASYNC,
4337 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004338 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004339
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004340 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004341 if (data == NULL)
4342 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004343 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004344 data->args.fhandle = &data->fh;
4345 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004346 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004347 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004348 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004349 data->res.fattr = &data->fattr;
4350 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004351 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004352 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004353 data->rpc_status = 0;
4354
Trond Myklebustc970aa82007-07-14 15:39:59 -04004355 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004356 msg.rpc_argp = &data->args;
4357 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004358 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004359 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004360 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004361 if (!issync)
4362 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004363 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004364 if (status != 0)
4365 goto out;
4366 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004367 if (status == 0)
4368 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4369 else
4370 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004371out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004372 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004373 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374}
4375
Trond Myklebuste6f81072008-01-24 18:14:34 -05004376int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
4378 struct nfs_server *server = NFS_SERVER(inode);
4379 struct nfs4_exception exception = { };
4380 int err;
4381 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004382 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 switch (err) {
4384 case -NFS4ERR_STALE_STATEID:
4385 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 case 0:
4387 return 0;
4388 }
4389 err = nfs4_handle_exception(server, err, &exception);
4390 } while (exception.retry);
4391 return err;
4392}
4393
4394#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4395#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4396
4397/*
4398 * sleep, with exponential backoff, and retry the LOCK operation.
4399 */
4400static unsigned long
4401nfs4_set_lock_task_retry(unsigned long timeout)
4402{
Jeff Laytond3103102011-12-01 22:44:39 +01004403 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 timeout <<= 1;
4405 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4406 return NFS4_LOCK_MAXTIMEOUT;
4407 return timeout;
4408}
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4411{
4412 struct inode *inode = state->inode;
4413 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004414 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004415 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004417 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004419 struct nfs_lockt_res res = {
4420 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 };
4422 struct rpc_message msg = {
4423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4424 .rpc_argp = &arg,
4425 .rpc_resp = &res,
4426 .rpc_cred = state->owner->so_cred,
4427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 struct nfs4_lock_state *lsp;
4429 int status;
4430
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004431 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004432 status = nfs4_set_lock_state(state, request);
4433 if (status != 0)
4434 goto out;
4435 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004436 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004437 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004438 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004439 switch (status) {
4440 case 0:
4441 request->fl_type = F_UNLCK;
4442 break;
4443 case -NFS4ERR_DENIED:
4444 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004446 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004447out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return status;
4449}
4450
4451static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4452{
4453 struct nfs4_exception exception = { };
4454 int err;
4455
4456 do {
4457 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4458 _nfs4_proc_getlk(state, cmd, request),
4459 &exception);
4460 } while (exception.retry);
4461 return err;
4462}
4463
4464static int do_vfs_lock(struct file *file, struct file_lock *fl)
4465{
4466 int res = 0;
4467 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4468 case FL_POSIX:
4469 res = posix_lock_file_wait(file, fl);
4470 break;
4471 case FL_FLOCK:
4472 res = flock_lock_file_wait(file, fl);
4473 break;
4474 default:
4475 BUG();
4476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 return res;
4478}
4479
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004480struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004481 struct nfs_locku_args arg;
4482 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004483 struct nfs4_lock_state *lsp;
4484 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004485 struct file_lock fl;
4486 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004487 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004488};
4489
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004490static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4491 struct nfs_open_context *ctx,
4492 struct nfs4_lock_state *lsp,
4493 struct nfs_seqid *seqid)
4494{
4495 struct nfs4_unlockdata *p;
4496 struct inode *inode = lsp->ls_state->inode;
4497
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004498 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004499 if (p == NULL)
4500 return NULL;
4501 p->arg.fh = NFS_FH(inode);
4502 p->arg.fl = &p->fl;
4503 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004504 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 p->arg.stateid = &lsp->ls_stateid;
4506 p->lsp = lsp;
4507 atomic_inc(&lsp->ls_count);
4508 /* Ensure we don't close file until we're done freeing locks! */
4509 p->ctx = get_nfs_open_context(ctx);
4510 memcpy(&p->fl, fl, sizeof(p->fl));
4511 p->server = NFS_SERVER(inode);
4512 return p;
4513}
4514
Trond Myklebust06f814a2006-01-03 09:55:07 +01004515static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004516{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004517 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004518 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004519 nfs4_put_lock_state(calldata->lsp);
4520 put_nfs_open_context(calldata->ctx);
4521 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004522}
4523
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004524static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004525{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004526 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004527
Trond Myklebust14516c32010-07-31 14:29:06 -04004528 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4529 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004530 switch (task->tk_status) {
4531 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004532 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4533 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004534 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004536 case -NFS4ERR_BAD_STATEID:
4537 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538 case -NFS4ERR_STALE_STATEID:
4539 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004540 break;
4541 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004542 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004543 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004544 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004545 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004546}
4547
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004548static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004549{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004550 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004552 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004553 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004554 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004555 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004556 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004557 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004558 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004559 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004560 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004561 &calldata->res.seq_res,
4562 task) != 0)
4563 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004564 return;
4565out_no_action:
4566 task->tk_action = NULL;
4567out_wait:
4568 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569}
4570
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004571static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004572 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004573 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004574 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004575};
4576
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004577static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4578 struct nfs_open_context *ctx,
4579 struct nfs4_lock_state *lsp,
4580 struct nfs_seqid *seqid)
4581{
4582 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004583 struct rpc_message msg = {
4584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4585 .rpc_cred = ctx->cred,
4586 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004587 struct rpc_task_setup task_setup_data = {
4588 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004589 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004590 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004591 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004592 .flags = RPC_TASK_ASYNC,
4593 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594
Frank Filz137d6ac2007-07-09 15:32:29 -07004595 /* Ensure this is an unlock - when canceling a lock, the
4596 * canceled lock is passed in, and it won't be an unlock.
4597 */
4598 fl->fl_type = F_UNLCK;
4599
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4601 if (data == NULL) {
4602 nfs_free_seqid(seqid);
4603 return ERR_PTR(-ENOMEM);
4604 }
4605
Trond Myklebust9d12b212012-01-17 22:04:25 -05004606 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004607 msg.rpc_argp = &data->arg;
4608 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004609 task_setup_data.callback_data = data;
4610 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611}
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4614{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004615 struct inode *inode = state->inode;
4616 struct nfs4_state_owner *sp = state->owner;
4617 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004618 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004619 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004620 struct rpc_task *task;
4621 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004622 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Trond Myklebust9b073572006-06-29 16:38:34 -04004624 status = nfs4_set_lock_state(state, request);
4625 /* Unlock _before_ we do the RPC call */
4626 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004627 /* Exclude nfs_delegation_claim_locks() */
4628 mutex_lock(&sp->so_delegreturn_mutex);
4629 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004630 down_read(&nfsi->rwsem);
4631 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4632 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004633 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004634 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004635 }
4636 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004637 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004638 if (status != 0)
4639 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004640 /* Is this a delegated lock? */
4641 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004642 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004643 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004644 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004645 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004646 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004647 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004648 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649 status = PTR_ERR(task);
4650 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004651 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004652 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004653 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004654out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004655 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004656 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004659struct nfs4_lockdata {
4660 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004661 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004662 struct nfs4_lock_state *lsp;
4663 struct nfs_open_context *ctx;
4664 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004665 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666 int rpc_status;
4667 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004668 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004669};
4670
4671static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004672 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4673 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674{
4675 struct nfs4_lockdata *p;
4676 struct inode *inode = lsp->ls_state->inode;
4677 struct nfs_server *server = NFS_SERVER(inode);
4678
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004679 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004680 if (p == NULL)
4681 return NULL;
4682
4683 p->arg.fh = NFS_FH(inode);
4684 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004685 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004686 if (p->arg.open_seqid == NULL)
4687 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004688 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004690 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004691 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004692 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004693 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004694 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004695 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004696 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004697 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004698 atomic_inc(&lsp->ls_count);
4699 p->ctx = get_nfs_open_context(ctx);
4700 memcpy(&p->fl, fl, sizeof(p->fl));
4701 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004702out_free_seqid:
4703 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004704out_free:
4705 kfree(p);
4706 return NULL;
4707}
4708
4709static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4710{
4711 struct nfs4_lockdata *data = calldata;
4712 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Harvey Harrison3110ff82008-05-02 13:42:44 -07004714 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004715 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004716 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 /* Do we need to do an open_to_lock_owner? */
4718 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004719 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004720 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004721 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004722 data->arg.open_stateid = &state->stateid;
4723 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004724 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004725 } else
4726 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004727 if (!nfs4_valid_open_stateid(state)) {
4728 data->rpc_status = -EBADF;
4729 task->tk_action = NULL;
4730 goto out_release_open_seqid;
4731 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004732 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004733 if (nfs4_setup_sequence(data->server,
4734 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004735 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004736 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004737 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004738out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004739 nfs_release_seqid(data->arg.open_seqid);
4740out_release_lock_seqid:
4741 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004742out_wait:
4743 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004744 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004745}
4746
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4748{
4749 struct nfs4_lockdata *data = calldata;
4750
Harvey Harrison3110ff82008-05-02 13:42:44 -07004751 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004752
Trond Myklebust14516c32010-07-31 14:29:06 -04004753 if (!nfs4_sequence_done(task, &data->res.seq_res))
4754 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004755
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004756 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004758 if (data->rpc_status == 0)
4759 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4760 else
4761 goto out;
4762 }
4763 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004764 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004765 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004766 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004767 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004768out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004769 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770}
4771
4772static void nfs4_lock_release(void *calldata)
4773{
4774 struct nfs4_lockdata *data = calldata;
4775
Harvey Harrison3110ff82008-05-02 13:42:44 -07004776 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004777 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004778 if (data->cancelled != 0) {
4779 struct rpc_task *task;
4780 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4781 data->arg.lock_seqid);
4782 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004783 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004784 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004785 } else
4786 nfs_free_seqid(data->arg.lock_seqid);
4787 nfs4_put_lock_state(data->lsp);
4788 put_nfs_open_context(data->ctx);
4789 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004790 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004791}
4792
4793static const struct rpc_call_ops nfs4_lock_ops = {
4794 .rpc_call_prepare = nfs4_lock_prepare,
4795 .rpc_call_done = nfs4_lock_done,
4796 .rpc_release = nfs4_lock_release,
4797};
4798
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004799static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4800{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004801 switch (error) {
4802 case -NFS4ERR_ADMIN_REVOKED:
4803 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004804 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004805 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004806 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004807 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004808 break;
4809 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004810 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004811 case -NFS4ERR_EXPIRED:
4812 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004813 };
4814}
4815
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004816static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004817{
4818 struct nfs4_lockdata *data;
4819 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004820 struct rpc_message msg = {
4821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4822 .rpc_cred = state->owner->so_cred,
4823 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004824 struct rpc_task_setup task_setup_data = {
4825 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004826 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004827 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004828 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004829 .flags = RPC_TASK_ASYNC,
4830 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831 int ret;
4832
Harvey Harrison3110ff82008-05-02 13:42:44 -07004833 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004834 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004835 fl->fl_u.nfs4_fl.owner,
4836 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004837 if (data == NULL)
4838 return -ENOMEM;
4839 if (IS_SETLKW(cmd))
4840 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004841 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004842 msg.rpc_argp = &data->arg;
4843 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004844 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004845 if (recovery_type > NFS_LOCK_NEW) {
4846 if (recovery_type == NFS_LOCK_RECLAIM)
4847 data->arg.reclaim = NFS_LOCK_RECLAIM;
4848 nfs4_set_sequence_privileged(&data->arg.seq_args);
4849 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004850 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004851 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004853 ret = nfs4_wait_for_completion_rpc_task(task);
4854 if (ret == 0) {
4855 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004856 if (ret)
4857 nfs4_handle_setlk_error(data->server, data->lsp,
4858 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004859 } else
4860 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004861 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004862 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004863 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
4866static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4867{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004868 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004869 struct nfs4_exception exception = {
4870 .inode = state->inode,
4871 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004872 int err;
4873
4874 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004875 /* Cache the lock if possible... */
4876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4877 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004878 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004879 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004880 break;
4881 nfs4_handle_exception(server, err, &exception);
4882 } while (exception.retry);
4883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884}
4885
4886static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4887{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004888 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004889 struct nfs4_exception exception = {
4890 .inode = state->inode,
4891 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004892 int err;
4893
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004894 err = nfs4_set_lock_state(state, request);
4895 if (err != 0)
4896 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004897 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004898 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4899 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004900 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004901 switch (err) {
4902 default:
4903 goto out;
4904 case -NFS4ERR_GRACE:
4905 case -NFS4ERR_DELAY:
4906 nfs4_handle_exception(server, err, &exception);
4907 err = 0;
4908 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004909 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004910out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004911 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004914#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004915/**
4916 * nfs41_check_expired_locks - possibly free a lock stateid
4917 *
4918 * @state: NFSv4 state for an inode
4919 *
4920 * Returns NFS_OK if recovery for this stateid is now finished.
4921 * Otherwise a negative NFS4ERR value is returned.
4922 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004923static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004924{
Chuck Levereb64cf92012-07-11 16:30:05 -04004925 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004926 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004927 struct nfs_server *server = NFS_SERVER(state->inode);
4928
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004929 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004930 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004931 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4932 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004933 /* Free the stateid unless the server
4934 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004935 if (status != -NFS4ERR_BAD_STATEID)
4936 nfs41_free_stateid(server,
4937 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004938 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004939 ret = status;
4940 }
4941 }
4942 };
4943
4944 return ret;
4945}
4946
4947static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4948{
4949 int status = NFS_OK;
4950
4951 if (test_bit(LK_STATE_IN_USE, &state->flags))
4952 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04004953 if (status != NFS_OK)
4954 status = nfs4_lock_expired(state, request);
4955 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004956}
4957#endif
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4960{
Trond Myklebust9a99af492013-02-04 20:17:49 -05004961 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004962 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004963 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004964 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004965 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004967 if ((fl_flags & FL_POSIX) &&
4968 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4969 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004970 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004971 status = nfs4_set_lock_state(state, request);
4972 if (status != 0)
4973 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004974 request->fl_flags |= FL_ACCESS;
4975 status = do_vfs_lock(request->fl_file, request);
4976 if (status < 0)
4977 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004978 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004979 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004980 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004981 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004982 request->fl_flags = fl_flags & ~FL_SLEEP;
4983 status = do_vfs_lock(request->fl_file, request);
4984 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004985 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05004986 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4987 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004988 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004989 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05004990 goto out;
4991 down_read(&nfsi->rwsem);
4992 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4993 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004994 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05004995 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004996 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004997 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004998 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004999 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5000 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005001out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005002 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005003out:
5004 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 return status;
5006}
5007
5008static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5009{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005010 struct nfs4_exception exception = {
5011 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005012 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005013 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 int err;
5015
5016 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005017 err = _nfs4_proc_setlk(state, cmd, request);
5018 if (err == -NFS4ERR_DENIED)
5019 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005021 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 } while (exception.retry);
5023 return err;
5024}
5025
5026static int
5027nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5028{
5029 struct nfs_open_context *ctx;
5030 struct nfs4_state *state;
5031 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5032 int status;
5033
5034 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005035 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 state = ctx->state;
5037
5038 if (request->fl_start < 0 || request->fl_end < 0)
5039 return -EINVAL;
5040
Trond Myklebustd9531262009-07-21 19:22:38 -04005041 if (IS_GETLK(cmd)) {
5042 if (state != NULL)
5043 return nfs4_proc_getlk(state, F_GETLK, request);
5044 return 0;
5045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5048 return -EINVAL;
5049
Trond Myklebustd9531262009-07-21 19:22:38 -04005050 if (request->fl_type == F_UNLCK) {
5051 if (state != NULL)
5052 return nfs4_proc_unlck(state, cmd, request);
5053 return 0;
5054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Trond Myklebustd9531262009-07-21 19:22:38 -04005056 if (state == NULL)
5057 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005058 /*
5059 * Don't rely on the VFS having checked the file open mode,
5060 * since it won't do this for flock() locks.
5061 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005062 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005063 case F_RDLCK:
5064 if (!(filp->f_mode & FMODE_READ))
5065 return -EBADF;
5066 break;
5067 case F_WRLCK:
5068 if (!(filp->f_mode & FMODE_WRITE))
5069 return -EBADF;
5070 }
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 do {
5073 status = nfs4_proc_setlk(state, cmd, request);
5074 if ((status != -EAGAIN) || IS_SETLK(cmd))
5075 break;
5076 timeout = nfs4_set_lock_task_retry(timeout);
5077 status = -ERESTARTSYS;
5078 if (signalled())
5079 break;
5080 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 return status;
5082}
5083
Trond Myklebust888e6942005-11-04 15:38:11 -05005084int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5085{
5086 struct nfs_server *server = NFS_SERVER(state->inode);
5087 struct nfs4_exception exception = { };
5088 int err;
5089
5090 err = nfs4_set_lock_state(state, fl);
5091 if (err != 0)
5092 goto out;
5093 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005094 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07005095 switch (err) {
5096 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005097 printk(KERN_ERR "NFS: %s: unhandled error "
5098 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07005099 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005100 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07005101 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005102 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005103 case -NFS4ERR_STALE_STATEID:
Trond Myklebust37380e42013-02-05 12:35:16 -05005104 set_bit(NFS_DELEGATED_STATE, &state->flags);
5105 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005106 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust37380e42013-02-05 12:35:16 -05005107 err = -EAGAIN;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005108 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05005109 case -NFS4ERR_BADSESSION:
5110 case -NFS4ERR_BADSLOT:
5111 case -NFS4ERR_BAD_HIGH_SLOT:
5112 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5113 case -NFS4ERR_DEADSESSION:
Trond Myklebust37380e42013-02-05 12:35:16 -05005114 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust9f594792012-05-27 13:02:53 -04005115 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebust37380e42013-02-05 12:35:16 -05005116 err = -EAGAIN;
Trond Myklebustd5122202009-06-17 13:22:58 -07005117 goto out;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005118 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07005119 case -NFS4ERR_ADMIN_REVOKED:
5120 case -NFS4ERR_BAD_STATEID:
5121 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005122 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005123 err = 0;
5124 goto out;
5125 case -ENOMEM:
5126 case -NFS4ERR_DENIED:
5127 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5128 err = 0;
5129 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07005130 }
Trond Myklebust37380e42013-02-05 12:35:16 -05005131 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust888e6942005-11-04 15:38:11 -05005132 err = nfs4_handle_exception(server, err, &exception);
5133 } while (exception.retry);
5134out:
5135 return err;
5136}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005137
Trond Myklebustcf470c32012-03-07 13:49:12 -05005138struct nfs_release_lockowner_data {
5139 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005140 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005141 struct nfs_release_lockowner_args args;
5142};
5143
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005144static void nfs4_release_lockowner_release(void *calldata)
5145{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005146 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005147 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005148 kfree(calldata);
5149}
5150
Trond Myklebust17280172012-03-11 13:11:00 -04005151static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005152 .rpc_release = nfs4_release_lockowner_release,
5153};
5154
Trond Myklebustcf470c32012-03-07 13:49:12 -05005155int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005156{
5157 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005158 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005159 struct rpc_message msg = {
5160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5161 };
5162
5163 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005164 return -EINVAL;
5165 data = kmalloc(sizeof(*data), GFP_NOFS);
5166 if (!data)
5167 return -ENOMEM;
5168 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005169 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005170 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5171 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5172 data->args.lock_owner.s_dev = server->s_dev;
5173 msg.rpc_argp = &data->args;
5174 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5175 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005176}
5177
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005178#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5179
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005180static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5181 const void *buf, size_t buflen,
5182 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005183{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005184 if (strcmp(key, "") != 0)
5185 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005186
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005187 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005188}
5189
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005190static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5191 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005192{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005193 if (strcmp(key, "") != 0)
5194 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005195
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005196 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005197}
5198
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005199static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5200 size_t list_len, const char *name,
5201 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005202{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005203 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005204
J. Bruce Fields096455a2006-03-20 23:23:42 -05005205 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5206 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005207
5208 if (list && len <= list_len)
5209 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005210 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005211}
5212
Andy Adamson533eb462011-06-13 18:25:56 -04005213/*
5214 * nfs_fhget will use either the mounted_on_fileid or the fileid
5215 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005216static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5217{
Andy Adamson533eb462011-06-13 18:25:56 -04005218 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5219 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5220 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005221 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005222 return;
5223
5224 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005225 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005226 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5227 fattr->nlink = 2;
5228}
5229
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005230static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5231 const struct qstr *name,
5232 struct nfs4_fs_locations *fs_locations,
5233 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005234{
5235 struct nfs_server *server = NFS_SERVER(dir);
5236 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005237 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005238 };
5239 struct nfs4_fs_locations_arg args = {
5240 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005241 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005242 .page = page,
5243 .bitmask = bitmask,
5244 };
Benny Halevy22958462009-04-01 09:22:02 -04005245 struct nfs4_fs_locations_res res = {
5246 .fs_locations = fs_locations,
5247 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005248 struct rpc_message msg = {
5249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5250 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005251 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005252 };
5253 int status;
5254
Harvey Harrison3110ff82008-05-02 13:42:44 -07005255 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005256
5257 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5258 * is not supported */
5259 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5260 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5261 else
5262 bitmask[0] |= FATTR4_WORD0_FILEID;
5263
Trond Myklebustc228fd32007-01-13 02:28:11 -05005264 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005265 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005266 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005267 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005268 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005269 return status;
5270}
5271
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005272int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5273 const struct qstr *name,
5274 struct nfs4_fs_locations *fs_locations,
5275 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005276{
5277 struct nfs4_exception exception = { };
5278 int err;
5279 do {
5280 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005281 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005282 &exception);
5283 } while (exception.retry);
5284 return err;
5285}
5286
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005287static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5288{
5289 int status;
5290 struct nfs4_secinfo_arg args = {
5291 .dir_fh = NFS_FH(dir),
5292 .name = name,
5293 };
5294 struct nfs4_secinfo_res res = {
5295 .flavors = flavors,
5296 };
5297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5299 .rpc_argp = &args,
5300 .rpc_resp = &res,
5301 };
5302
5303 dprintk("NFS call secinfo %s\n", name->name);
5304 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5305 dprintk("NFS reply secinfo: %d\n", status);
5306 return status;
5307}
5308
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005309int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5310 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005311{
5312 struct nfs4_exception exception = { };
5313 int err;
5314 do {
5315 err = nfs4_handle_exception(NFS_SERVER(dir),
5316 _nfs4_proc_secinfo(dir, name, flavors),
5317 &exception);
5318 } while (exception.retry);
5319 return err;
5320}
5321
Andy Adamson557134a2009-04-01 09:21:53 -04005322#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005323/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005324 * Check the exchange flags returned by the server for invalid flags, having
5325 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5326 * DS flags set.
5327 */
5328static int nfs4_check_cl_exchange_flags(u32 flags)
5329{
5330 if (flags & ~EXCHGID4_FLAG_MASK_R)
5331 goto out_inval;
5332 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5333 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5334 goto out_inval;
5335 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5336 goto out_inval;
5337 return NFS_OK;
5338out_inval:
5339 return -NFS4ERR_INVAL;
5340}
5341
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005342static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005343nfs41_same_server_scope(struct nfs41_server_scope *a,
5344 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005345{
5346 if (a->server_scope_sz == b->server_scope_sz &&
5347 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5348 return true;
5349
5350 return false;
5351}
5352
Andy Adamson357f54d2010-12-14 10:11:57 -05005353/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005354 * nfs4_proc_bind_conn_to_session()
5355 *
5356 * The 4.1 client currently uses the same TCP connection for the
5357 * fore and backchannel.
5358 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005359int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005360{
5361 int status;
5362 struct nfs41_bind_conn_to_session_res res;
5363 struct rpc_message msg = {
5364 .rpc_proc =
5365 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5366 .rpc_argp = clp,
5367 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005368 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005369 };
5370
5371 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005372
5373 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5374 if (unlikely(res.session == NULL)) {
5375 status = -ENOMEM;
5376 goto out;
5377 }
5378
5379 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5380 if (status == 0) {
5381 if (memcmp(res.session->sess_id.data,
5382 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5383 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5384 status = -EIO;
5385 goto out_session;
5386 }
5387 if (res.dir != NFS4_CDFS4_BOTH) {
5388 dprintk("NFS: %s: Unexpected direction from server\n",
5389 __func__);
5390 status = -EIO;
5391 goto out_session;
5392 }
5393 if (res.use_conn_in_rdma_mode) {
5394 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5395 __func__);
5396 status = -EIO;
5397 goto out_session;
5398 }
5399 }
5400out_session:
5401 kfree(res.session);
5402out:
5403 dprintk("<-- %s status= %d\n", __func__, status);
5404 return status;
5405}
5406
5407/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005408 * nfs4_proc_exchange_id()
5409 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005410 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5411 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005412 * Since the clientid has expired, all compounds using sessions
5413 * associated with the stale clientid will be returning
5414 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5415 * be in some phase of session reset.
5416 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005417int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005418{
5419 nfs4_verifier verifier;
5420 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005421 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005422 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005423 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005424 };
5425 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005426 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005427 };
5428 int status;
5429 struct rpc_message msg = {
5430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5431 .rpc_argp = &args,
5432 .rpc_resp = &res,
5433 .rpc_cred = cred,
5434 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005435
Chuck Leverf0920752012-05-21 22:45:41 -04005436 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005437 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5438 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005439 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5440 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5441 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005442
Chuck Leveracdeb692012-05-21 22:46:16 -04005443 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005444 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005445 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005446 status = -ENOMEM;
5447 goto out;
5448 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005449
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005450 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005451 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005452 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005453 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005454 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005455 }
5456
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005457 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005458 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005459 status = -ENOMEM;
5460 goto out_server_scope;
5461 }
5462
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005463 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005464 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005465 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005466
Chuck Lever177313f2012-05-21 22:44:58 -04005467 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005468 clp->cl_clientid = res.clientid;
5469 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5470 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5471 clp->cl_seqid = res.seqid;
5472
Chuck Leveracdeb692012-05-21 22:46:16 -04005473 kfree(clp->cl_serverowner);
5474 clp->cl_serverowner = res.server_owner;
5475 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005476
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005477 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005478 kfree(clp->cl_implid);
5479 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005480
Chuck Lever177313f2012-05-21 22:44:58 -04005481 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005482 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005483 res.server_scope)) {
5484 dprintk("%s: server_scope mismatch detected\n",
5485 __func__);
5486 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005487 kfree(clp->cl_serverscope);
5488 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005489 }
5490
Chuck Lever177313f2012-05-21 22:44:58 -04005491 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005492 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005493 goto out;
5494 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005495 } else
5496 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005497
Chuck Leveracdeb692012-05-21 22:46:16 -04005498out_server_owner:
5499 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005500out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005501 kfree(res.server_scope);
5502out:
Chuck Lever177313f2012-05-21 22:44:58 -04005503 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005504 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005505 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005506 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005507 clp->cl_implid->date.seconds,
5508 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005509 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005510 return status;
5511}
5512
Trond Myklebust66245532012-05-25 17:18:09 -04005513static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5514 struct rpc_cred *cred)
5515{
5516 struct rpc_message msg = {
5517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5518 .rpc_argp = clp,
5519 .rpc_cred = cred,
5520 };
5521 int status;
5522
5523 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5524 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005525 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005526 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5527 return status;
5528}
5529
5530static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5531 struct rpc_cred *cred)
5532{
5533 unsigned int loop;
5534 int ret;
5535
5536 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5537 ret = _nfs4_proc_destroy_clientid(clp, cred);
5538 switch (ret) {
5539 case -NFS4ERR_DELAY:
5540 case -NFS4ERR_CLIENTID_BUSY:
5541 ssleep(1);
5542 break;
5543 default:
5544 return ret;
5545 }
5546 }
5547 return 0;
5548}
5549
5550int nfs4_destroy_clientid(struct nfs_client *clp)
5551{
5552 struct rpc_cred *cred;
5553 int ret = 0;
5554
5555 if (clp->cl_mvops->minor_version < 1)
5556 goto out;
5557 if (clp->cl_exchange_flags == 0)
5558 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005559 if (clp->cl_preserve_clid)
5560 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005561 cred = nfs4_get_exchange_id_cred(clp);
5562 ret = nfs4_proc_destroy_clientid(clp, cred);
5563 if (cred)
5564 put_rpccred(cred);
5565 switch (ret) {
5566 case 0:
5567 case -NFS4ERR_STALE_CLIENTID:
5568 clp->cl_exchange_flags = 0;
5569 }
5570out:
5571 return ret;
5572}
5573
Andy Adamson2050f0c2009-04-01 09:22:30 -04005574struct nfs4_get_lease_time_data {
5575 struct nfs4_get_lease_time_args *args;
5576 struct nfs4_get_lease_time_res *res;
5577 struct nfs_client *clp;
5578};
5579
5580static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5581 void *calldata)
5582{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005583 struct nfs4_get_lease_time_data *data =
5584 (struct nfs4_get_lease_time_data *)calldata;
5585
5586 dprintk("--> %s\n", __func__);
5587 /* just setup sequence, do not trigger session recovery
5588 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005589 nfs41_setup_sequence(data->clp->cl_session,
5590 &data->args->la_seq_args,
5591 &data->res->lr_seq_res,
5592 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005593 dprintk("<-- %s\n", __func__);
5594}
5595
5596/*
5597 * Called from nfs4_state_manager thread for session setup, so don't recover
5598 * from sequence operation or clientid errors.
5599 */
5600static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5601{
5602 struct nfs4_get_lease_time_data *data =
5603 (struct nfs4_get_lease_time_data *)calldata;
5604
5605 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005606 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5607 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005608 switch (task->tk_status) {
5609 case -NFS4ERR_DELAY:
5610 case -NFS4ERR_GRACE:
5611 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5612 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5613 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005614 /* fall through */
5615 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005616 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005617 return;
5618 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005619 dprintk("<-- %s\n", __func__);
5620}
5621
Trond Myklebust17280172012-03-11 13:11:00 -04005622static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005623 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5624 .rpc_call_done = nfs4_get_lease_time_done,
5625};
5626
5627int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5628{
5629 struct rpc_task *task;
5630 struct nfs4_get_lease_time_args args;
5631 struct nfs4_get_lease_time_res res = {
5632 .lr_fsinfo = fsinfo,
5633 };
5634 struct nfs4_get_lease_time_data data = {
5635 .args = &args,
5636 .res = &res,
5637 .clp = clp,
5638 };
5639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5641 .rpc_argp = &args,
5642 .rpc_resp = &res,
5643 };
5644 struct rpc_task_setup task_setup = {
5645 .rpc_client = clp->cl_rpcclient,
5646 .rpc_message = &msg,
5647 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005648 .callback_data = &data,
5649 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005650 };
5651 int status;
5652
Trond Myklebust9d12b212012-01-17 22:04:25 -05005653 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005654 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005655 dprintk("--> %s\n", __func__);
5656 task = rpc_run_task(&task_setup);
5657
5658 if (IS_ERR(task))
5659 status = PTR_ERR(task);
5660 else {
5661 status = task->tk_status;
5662 rpc_put_task(task);
5663 }
5664 dprintk("<-- %s return %d\n", __func__, status);
5665
5666 return status;
5667}
5668
Andy Adamsonfc931582009-04-01 09:22:31 -04005669/*
5670 * Initialize the values to be used by the client in CREATE_SESSION
5671 * If nfs4_init_session set the fore channel request and response sizes,
5672 * use them.
5673 *
5674 * Set the back channel max_resp_sz_cached to zero to force the client to
5675 * always set csa_cachethis to FALSE because the current implementation
5676 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5677 */
5678static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5679{
5680 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005681 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5682 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005683
5684 if (mxrqst_sz == 0)
5685 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5686 if (mxresp_sz == 0)
5687 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5688 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005689 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5690 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005691 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005692 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005693
5694 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005695 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005696 __func__,
5697 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005698 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005699
5700 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005701 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5702 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5703 args->bc_attrs.max_resp_sz_cached = 0;
5704 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5705 args->bc_attrs.max_reqs = 1;
5706
5707 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5708 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5709 __func__,
5710 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5711 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5712 args->bc_attrs.max_reqs);
5713}
5714
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005715static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005716{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005717 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5718 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5719
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005720 if (rcvd->max_resp_sz > sent->max_resp_sz)
5721 return -EINVAL;
5722 /*
5723 * Our requested max_ops is the minimum we need; we're not
5724 * prepared to break up compounds into smaller pieces than that.
5725 * So, no point even trying to continue if the server won't
5726 * cooperate:
5727 */
5728 if (rcvd->max_ops < sent->max_ops)
5729 return -EINVAL;
5730 if (rcvd->max_reqs == 0)
5731 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005732 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5733 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005734 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005735}
5736
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005737static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5738{
5739 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5740 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005741
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005742 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5743 return -EINVAL;
5744 if (rcvd->max_resp_sz < sent->max_resp_sz)
5745 return -EINVAL;
5746 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5747 return -EINVAL;
5748 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005749 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005750 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005751 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005752 return -EINVAL;
5753 return 0;
5754}
Andy Adamson8d353012009-04-01 09:22:32 -04005755
Andy Adamson8d353012009-04-01 09:22:32 -04005756static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5757 struct nfs4_session *session)
5758{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005759 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005760
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005761 ret = nfs4_verify_fore_channel_attrs(args, session);
5762 if (ret)
5763 return ret;
5764 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005765}
5766
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005767static int _nfs4_proc_create_session(struct nfs_client *clp,
5768 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005769{
5770 struct nfs4_session *session = clp->cl_session;
5771 struct nfs41_create_session_args args = {
5772 .client = clp,
5773 .cb_program = NFS4_CALLBACK,
5774 };
5775 struct nfs41_create_session_res res = {
5776 .client = clp,
5777 };
5778 struct rpc_message msg = {
5779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5780 .rpc_argp = &args,
5781 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005782 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005783 };
5784 int status;
5785
5786 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005787 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005788
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005789 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005790
Trond Myklebust43095d32012-11-20 11:13:12 -05005791 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005792 /* Verify the session's negotiated channel_attrs values */
5793 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005794 /* Increment the clientid slot sequence id */
5795 clp->cl_seqid++;
5796 }
5797
5798 return status;
5799}
5800
5801/*
5802 * Issues a CREATE_SESSION operation to the server.
5803 * It is the responsibility of the caller to verify the session is
5804 * expired before calling this routine.
5805 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005806int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005807{
5808 int status;
5809 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005810 struct nfs4_session *session = clp->cl_session;
5811
5812 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5813
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005814 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005815 if (status)
5816 goto out;
5817
Andy Adamsonaacd5532011-11-09 13:58:21 -05005818 /* Init or reset the session slot tables */
5819 status = nfs4_setup_session_slot_tables(session);
5820 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005821 if (status)
5822 goto out;
5823
5824 ptr = (unsigned *)&session->sess_id.data[0];
5825 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5826 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005827out:
5828 dprintk("<-- %s\n", __func__);
5829 return status;
5830}
5831
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005832/*
5833 * Issue the over-the-wire RPC DESTROY_SESSION.
5834 * The caller must serialize access to this routine.
5835 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005836int nfs4_proc_destroy_session(struct nfs4_session *session,
5837 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005838{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005839 struct rpc_message msg = {
5840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5841 .rpc_argp = session,
5842 .rpc_cred = cred,
5843 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005844 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005845
5846 dprintk("--> nfs4_proc_destroy_session\n");
5847
5848 /* session is still being setup */
5849 if (session->clp->cl_cons_state != NFS_CS_READY)
5850 return status;
5851
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005852 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005853
5854 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005855 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005856 "Session has been destroyed regardless...\n", status);
5857
5858 dprintk("<-- nfs4_proc_destroy_session\n");
5859 return status;
5860}
5861
Trond Myklebust7b38c362012-05-23 13:23:31 -04005862/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005863 * Renew the cl_session lease.
5864 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005865struct nfs4_sequence_data {
5866 struct nfs_client *clp;
5867 struct nfs4_sequence_args args;
5868 struct nfs4_sequence_res res;
5869};
5870
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005871static void nfs41_sequence_release(void *data)
5872{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005873 struct nfs4_sequence_data *calldata = data;
5874 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005875
Alexandros Batsakis71358402010-02-05 03:45:05 -08005876 if (atomic_read(&clp->cl_count) > 1)
5877 nfs4_schedule_state_renewal(clp);
5878 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005879 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005880}
5881
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005882static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5883{
5884 switch(task->tk_status) {
5885 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005886 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5887 return -EAGAIN;
5888 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005889 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005890 }
5891 return 0;
5892}
5893
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005894static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005895{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005896 struct nfs4_sequence_data *calldata = data;
5897 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005898
Trond Myklebust14516c32010-07-31 14:29:06 -04005899 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5900 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005901
5902 if (task->tk_status < 0) {
5903 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005904 if (atomic_read(&clp->cl_count) == 1)
5905 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005906
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005907 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5908 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005909 return;
5910 }
5911 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005912 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005913out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914 dprintk("<-- %s\n", __func__);
5915}
5916
5917static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5918{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005919 struct nfs4_sequence_data *calldata = data;
5920 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005921 struct nfs4_sequence_args *args;
5922 struct nfs4_sequence_res *res;
5923
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005924 args = task->tk_msg.rpc_argp;
5925 res = task->tk_msg.rpc_resp;
5926
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005927 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005928}
5929
5930static const struct rpc_call_ops nfs41_sequence_ops = {
5931 .rpc_call_done = nfs41_sequence_call_done,
5932 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005933 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005934};
5935
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005936static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5937 struct rpc_cred *cred,
5938 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005939{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005940 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005941 struct rpc_message msg = {
5942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5943 .rpc_cred = cred,
5944 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005945 struct rpc_task_setup task_setup_data = {
5946 .rpc_client = clp->cl_rpcclient,
5947 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005948 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005949 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5950 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951
Alexandros Batsakis71358402010-02-05 03:45:05 -08005952 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005953 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005954 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005955 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005956 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005957 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005958 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005959 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005960 if (is_privileged)
5961 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005962 msg.rpc_argp = &calldata->args;
5963 msg.rpc_resp = &calldata->res;
5964 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005965 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005967 return rpc_run_task(&task_setup_data);
5968}
5969
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005970static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005971{
5972 struct rpc_task *task;
5973 int ret = 0;
5974
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005975 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5976 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005977 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005978 if (IS_ERR(task))
5979 ret = PTR_ERR(task);
5980 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005981 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005982 dprintk("<-- %s status=%d\n", __func__, ret);
5983 return ret;
5984}
5985
5986static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5987{
5988 struct rpc_task *task;
5989 int ret;
5990
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005991 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005992 if (IS_ERR(task)) {
5993 ret = PTR_ERR(task);
5994 goto out;
5995 }
5996 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005997 if (!ret) {
5998 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5999
6000 if (task->tk_status == 0)
6001 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006002 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006003 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006004 rpc_put_task(task);
6005out:
6006 dprintk("<-- %s status=%d\n", __func__, ret);
6007 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006008}
6009
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006010struct nfs4_reclaim_complete_data {
6011 struct nfs_client *clp;
6012 struct nfs41_reclaim_complete_args arg;
6013 struct nfs41_reclaim_complete_res res;
6014};
6015
6016static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6017{
6018 struct nfs4_reclaim_complete_data *calldata = data;
6019
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006020 nfs41_setup_sequence(calldata->clp->cl_session,
6021 &calldata->arg.seq_args,
6022 &calldata->res.seq_res,
6023 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006024}
6025
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006026static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6027{
6028 switch(task->tk_status) {
6029 case 0:
6030 case -NFS4ERR_COMPLETE_ALREADY:
6031 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6032 break;
6033 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006034 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006035 /* fall through */
6036 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006037 return -EAGAIN;
6038 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006039 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006040 }
6041 return 0;
6042}
6043
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006044static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6045{
6046 struct nfs4_reclaim_complete_data *calldata = data;
6047 struct nfs_client *clp = calldata->clp;
6048 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6049
6050 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006051 if (!nfs41_sequence_done(task, res))
6052 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006053
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006054 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6055 rpc_restart_call_prepare(task);
6056 return;
6057 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006058 dprintk("<-- %s\n", __func__);
6059}
6060
6061static void nfs4_free_reclaim_complete_data(void *data)
6062{
6063 struct nfs4_reclaim_complete_data *calldata = data;
6064
6065 kfree(calldata);
6066}
6067
6068static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6069 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6070 .rpc_call_done = nfs4_reclaim_complete_done,
6071 .rpc_release = nfs4_free_reclaim_complete_data,
6072};
6073
6074/*
6075 * Issue a global reclaim complete.
6076 */
6077static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6078{
6079 struct nfs4_reclaim_complete_data *calldata;
6080 struct rpc_task *task;
6081 struct rpc_message msg = {
6082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6083 };
6084 struct rpc_task_setup task_setup_data = {
6085 .rpc_client = clp->cl_rpcclient,
6086 .rpc_message = &msg,
6087 .callback_ops = &nfs4_reclaim_complete_call_ops,
6088 .flags = RPC_TASK_ASYNC,
6089 };
6090 int status = -ENOMEM;
6091
6092 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006093 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006094 if (calldata == NULL)
6095 goto out;
6096 calldata->clp = clp;
6097 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006098
Trond Myklebust9d12b212012-01-17 22:04:25 -05006099 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006100 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006101 msg.rpc_argp = &calldata->arg;
6102 msg.rpc_resp = &calldata->res;
6103 task_setup_data.callback_data = calldata;
6104 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006105 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006106 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006107 goto out;
6108 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006109 status = nfs4_wait_for_completion_rpc_task(task);
6110 if (status == 0)
6111 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006112 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006113 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114out:
6115 dprintk("<-- %s status=%d\n", __func__, status);
6116 return status;
6117}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006118
6119static void
6120nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6121{
6122 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006123 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006124 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006125
6126 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006127 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6128 * right now covering the LAYOUTGET we are about to send.
6129 * However, that is not so catastrophic, and there seems
6130 * to be no way to prevent it completely.
6131 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006132 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006133 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006134 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006135 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6136 NFS_I(lgp->args.inode)->layout,
6137 lgp->args.ctx->state)) {
6138 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006139 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006140}
6141
6142static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6143{
6144 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006145 struct inode *inode = lgp->args.inode;
6146 struct nfs_server *server = NFS_SERVER(inode);
6147 struct pnfs_layout_hdr *lo;
6148 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006149 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150
6151 dprintk("--> %s\n", __func__);
6152
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006153 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006154 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006155
6156 switch (task->tk_status) {
6157 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006158 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006159 case -NFS4ERR_LAYOUTTRYLATER:
6160 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006161 timeo = rpc_get_timeout(task->tk_client);
6162 giveup = lgp->args.timestamp + timeo;
6163 if (time_after(giveup, jiffies))
6164 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006165 break;
6166 case -NFS4ERR_EXPIRED:
6167 case -NFS4ERR_BAD_STATEID:
6168 spin_lock(&inode->i_lock);
6169 lo = NFS_I(inode)->layout;
6170 if (!lo || list_empty(&lo->plh_segs)) {
6171 spin_unlock(&inode->i_lock);
6172 /* If the open stateid was bad, then recover it. */
6173 state = lgp->args.ctx->state;
6174 } else {
6175 LIST_HEAD(head);
6176
6177 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6178 spin_unlock(&inode->i_lock);
6179 /* Mark the bad layout state as invalid, then
6180 * retry using the open stateid. */
6181 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006182 }
6183 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006184 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6185 rpc_restart_call_prepare(task);
6186out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006187 dprintk("<-- %s\n", __func__);
6188}
6189
Idan Kedar85541162012-08-02 11:47:10 +03006190static size_t max_response_pages(struct nfs_server *server)
6191{
6192 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6193 return nfs_page_array_len(0, max_resp_sz);
6194}
6195
6196static void nfs4_free_pages(struct page **pages, size_t size)
6197{
6198 int i;
6199
6200 if (!pages)
6201 return;
6202
6203 for (i = 0; i < size; i++) {
6204 if (!pages[i])
6205 break;
6206 __free_page(pages[i]);
6207 }
6208 kfree(pages);
6209}
6210
6211static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6212{
6213 struct page **pages;
6214 int i;
6215
6216 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6217 if (!pages) {
6218 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6219 return NULL;
6220 }
6221
6222 for (i = 0; i < size; i++) {
6223 pages[i] = alloc_page(gfp_flags);
6224 if (!pages[i]) {
6225 dprintk("%s: failed to allocate page\n", __func__);
6226 nfs4_free_pages(pages, size);
6227 return NULL;
6228 }
6229 }
6230
6231 return pages;
6232}
6233
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006234static void nfs4_layoutget_release(void *calldata)
6235{
6236 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006237 struct inode *inode = lgp->args.inode;
6238 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006239 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006240
6241 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006242 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006243 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006244 put_nfs_open_context(lgp->args.ctx);
6245 kfree(calldata);
6246 dprintk("<-- %s\n", __func__);
6247}
6248
6249static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6250 .rpc_call_prepare = nfs4_layoutget_prepare,
6251 .rpc_call_done = nfs4_layoutget_done,
6252 .rpc_release = nfs4_layoutget_release,
6253};
6254
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006255struct pnfs_layout_segment *
6256nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006257{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006258 struct inode *inode = lgp->args.inode;
6259 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006260 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006261 struct rpc_task *task;
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6264 .rpc_argp = &lgp->args,
6265 .rpc_resp = &lgp->res,
6266 };
6267 struct rpc_task_setup task_setup_data = {
6268 .rpc_client = server->client,
6269 .rpc_message = &msg,
6270 .callback_ops = &nfs4_layoutget_call_ops,
6271 .callback_data = lgp,
6272 .flags = RPC_TASK_ASYNC,
6273 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006274 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006275 int status = 0;
6276
6277 dprintk("--> %s\n", __func__);
6278
Idan Kedar85541162012-08-02 11:47:10 +03006279 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6280 if (!lgp->args.layout.pages) {
6281 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006282 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006283 }
6284 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006285 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006286
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006287 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006288 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006289 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006290
6291 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6292 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6293
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006294 task = rpc_run_task(&task_setup_data);
6295 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006296 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006297 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006298 if (status == 0)
6299 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006300 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6301 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006302 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006303 rpc_put_task(task);
6304 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006305 if (status)
6306 return ERR_PTR(status);
6307 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006308}
6309
Benny Halevycbe82602011-05-22 19:52:37 +03006310static void
6311nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6312{
6313 struct nfs4_layoutreturn *lrp = calldata;
6314
6315 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006316 nfs41_setup_sequence(lrp->clp->cl_session,
6317 &lrp->args.seq_args,
6318 &lrp->res.seq_res,
6319 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006320}
6321
6322static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6323{
6324 struct nfs4_layoutreturn *lrp = calldata;
6325 struct nfs_server *server;
6326
6327 dprintk("--> %s\n", __func__);
6328
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006329 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006330 return;
6331
6332 server = NFS_SERVER(lrp->args.inode);
6333 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006334 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006335 return;
6336 }
Benny Halevycbe82602011-05-22 19:52:37 +03006337 dprintk("<-- %s\n", __func__);
6338}
6339
6340static void nfs4_layoutreturn_release(void *calldata)
6341{
6342 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006343 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006344
6345 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006346 spin_lock(&lo->plh_inode->i_lock);
6347 if (lrp->res.lrs_present)
6348 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6349 lo->plh_block_lgets--;
6350 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006351 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006352 kfree(calldata);
6353 dprintk("<-- %s\n", __func__);
6354}
6355
6356static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6357 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6358 .rpc_call_done = nfs4_layoutreturn_done,
6359 .rpc_release = nfs4_layoutreturn_release,
6360};
6361
6362int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6363{
6364 struct rpc_task *task;
6365 struct rpc_message msg = {
6366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6367 .rpc_argp = &lrp->args,
6368 .rpc_resp = &lrp->res,
6369 };
6370 struct rpc_task_setup task_setup_data = {
6371 .rpc_client = lrp->clp->cl_rpcclient,
6372 .rpc_message = &msg,
6373 .callback_ops = &nfs4_layoutreturn_call_ops,
6374 .callback_data = lrp,
6375 };
6376 int status;
6377
6378 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006379 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006380 task = rpc_run_task(&task_setup_data);
6381 if (IS_ERR(task))
6382 return PTR_ERR(task);
6383 status = task->tk_status;
6384 dprintk("<-- %s status=%d\n", __func__, status);
6385 rpc_put_task(task);
6386 return status;
6387}
6388
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006389/*
6390 * Retrieve the list of Data Server devices from the MDS.
6391 */
6392static int _nfs4_getdevicelist(struct nfs_server *server,
6393 const struct nfs_fh *fh,
6394 struct pnfs_devicelist *devlist)
6395{
6396 struct nfs4_getdevicelist_args args = {
6397 .fh = fh,
6398 .layoutclass = server->pnfs_curr_ld->id,
6399 };
6400 struct nfs4_getdevicelist_res res = {
6401 .devlist = devlist,
6402 };
6403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6405 .rpc_argp = &args,
6406 .rpc_resp = &res,
6407 };
6408 int status;
6409
6410 dprintk("--> %s\n", __func__);
6411 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6412 &res.seq_res, 0);
6413 dprintk("<-- %s status=%d\n", __func__, status);
6414 return status;
6415}
6416
6417int nfs4_proc_getdevicelist(struct nfs_server *server,
6418 const struct nfs_fh *fh,
6419 struct pnfs_devicelist *devlist)
6420{
6421 struct nfs4_exception exception = { };
6422 int err;
6423
6424 do {
6425 err = nfs4_handle_exception(server,
6426 _nfs4_getdevicelist(server, fh, devlist),
6427 &exception);
6428 } while (exception.retry);
6429
6430 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6431 err, devlist->num_devs);
6432
6433 return err;
6434}
6435EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6436
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006437static int
6438_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6439{
6440 struct nfs4_getdeviceinfo_args args = {
6441 .pdev = pdev,
6442 };
6443 struct nfs4_getdeviceinfo_res res = {
6444 .pdev = pdev,
6445 };
6446 struct rpc_message msg = {
6447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6448 .rpc_argp = &args,
6449 .rpc_resp = &res,
6450 };
6451 int status;
6452
6453 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006454 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006455 dprintk("<-- %s status=%d\n", __func__, status);
6456
6457 return status;
6458}
6459
6460int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6461{
6462 struct nfs4_exception exception = { };
6463 int err;
6464
6465 do {
6466 err = nfs4_handle_exception(server,
6467 _nfs4_proc_getdeviceinfo(server, pdev),
6468 &exception);
6469 } while (exception.retry);
6470 return err;
6471}
6472EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6473
Andy Adamson863a3c62011-03-23 13:27:54 +00006474static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6475{
6476 struct nfs4_layoutcommit_data *data = calldata;
6477 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006478 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006479
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006480 nfs41_setup_sequence(session,
6481 &data->args.seq_args,
6482 &data->res.seq_res,
6483 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006484}
6485
6486static void
6487nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6488{
6489 struct nfs4_layoutcommit_data *data = calldata;
6490 struct nfs_server *server = NFS_SERVER(data->args.inode);
6491
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006492 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006493 return;
6494
6495 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006496 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6497 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6498 case -NFS4ERR_BADLAYOUT: /* no layout */
6499 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006500 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006501 break;
6502 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006503 nfs_post_op_update_inode_force_wcc(data->args.inode,
6504 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006505 break;
6506 default:
6507 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6508 rpc_restart_call_prepare(task);
6509 return;
6510 }
6511 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006512}
6513
6514static void nfs4_layoutcommit_release(void *calldata)
6515{
6516 struct nfs4_layoutcommit_data *data = calldata;
6517
Andy Adamsondb29c082011-07-30 20:52:38 -04006518 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006519 put_rpccred(data->cred);
6520 kfree(data);
6521}
6522
6523static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6524 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6525 .rpc_call_done = nfs4_layoutcommit_done,
6526 .rpc_release = nfs4_layoutcommit_release,
6527};
6528
6529int
Andy Adamsonef311532011-03-12 02:58:10 -05006530nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006531{
6532 struct rpc_message msg = {
6533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6534 .rpc_argp = &data->args,
6535 .rpc_resp = &data->res,
6536 .rpc_cred = data->cred,
6537 };
6538 struct rpc_task_setup task_setup_data = {
6539 .task = &data->task,
6540 .rpc_client = NFS_CLIENT(data->args.inode),
6541 .rpc_message = &msg,
6542 .callback_ops = &nfs4_layoutcommit_ops,
6543 .callback_data = data,
6544 .flags = RPC_TASK_ASYNC,
6545 };
6546 struct rpc_task *task;
6547 int status = 0;
6548
6549 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6550 "lbw: %llu inode %lu\n",
6551 data->task.tk_pid, sync,
6552 data->args.lastbytewritten,
6553 data->args.inode->i_ino);
6554
Trond Myklebust9d12b212012-01-17 22:04:25 -05006555 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006556 task = rpc_run_task(&task_setup_data);
6557 if (IS_ERR(task))
6558 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006559 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006560 goto out;
6561 status = nfs4_wait_for_completion_rpc_task(task);
6562 if (status != 0)
6563 goto out;
6564 status = task->tk_status;
6565out:
6566 dprintk("%s: status %d\n", __func__, status);
6567 rpc_put_task(task);
6568 return status;
6569}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006570
6571static int
6572_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6573 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6574{
6575 struct nfs41_secinfo_no_name_args args = {
6576 .style = SECINFO_STYLE_CURRENT_FH,
6577 };
6578 struct nfs4_secinfo_res res = {
6579 .flavors = flavors,
6580 };
6581 struct rpc_message msg = {
6582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6583 .rpc_argp = &args,
6584 .rpc_resp = &res,
6585 };
6586 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6587}
6588
6589static int
6590nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6591 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6592{
6593 struct nfs4_exception exception = { };
6594 int err;
6595 do {
6596 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6597 switch (err) {
6598 case 0:
6599 case -NFS4ERR_WRONGSEC:
6600 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006601 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006602 default:
6603 err = nfs4_handle_exception(server, err, &exception);
6604 }
6605 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006606out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006607 return err;
6608}
6609
6610static int
6611nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6612 struct nfs_fsinfo *info)
6613{
6614 int err;
6615 struct page *page;
6616 rpc_authflavor_t flavor;
6617 struct nfs4_secinfo_flavors *flavors;
6618
6619 page = alloc_page(GFP_KERNEL);
6620 if (!page) {
6621 err = -ENOMEM;
6622 goto out;
6623 }
6624
6625 flavors = page_address(page);
6626 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6627
6628 /*
6629 * Fall back on "guess and check" method if
6630 * the server doesn't support SECINFO_NO_NAME
6631 */
6632 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6633 err = nfs4_find_root_sec(server, fhandle, info);
6634 goto out_freepage;
6635 }
6636 if (err)
6637 goto out_freepage;
6638
6639 flavor = nfs_find_best_sec(flavors);
6640 if (err == 0)
6641 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6642
6643out_freepage:
6644 put_page(page);
6645 if (err == -EACCES)
6646 return -EPERM;
6647out:
6648 return err;
6649}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006650
6651static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006652{
6653 int status;
6654 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006655 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006656 };
6657 struct nfs41_test_stateid_res res;
6658 struct rpc_message msg = {
6659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6660 .rpc_argp = &args,
6661 .rpc_resp = &res,
6662 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006663
Chuck Lever38527b12012-07-11 16:30:23 -04006664 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006665 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006666 nfs4_set_sequence_privileged(&args.seq_args);
6667 status = nfs4_call_sync_sequence(server->client, server, &msg,
6668 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006669 if (status != NFS_OK) {
6670 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006671 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006672 }
6673 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006674 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006675}
6676
Chuck Lever38527b12012-07-11 16:30:23 -04006677/**
6678 * nfs41_test_stateid - perform a TEST_STATEID operation
6679 *
6680 * @server: server / transport on which to perform the operation
6681 * @stateid: state ID to test
6682 *
6683 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6684 * Otherwise a negative NFS4ERR value is returned if the operation
6685 * failed or the state ID is not currently valid.
6686 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006687static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006688{
6689 struct nfs4_exception exception = { };
6690 int err;
6691 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006692 err = _nfs41_test_stateid(server, stateid);
6693 if (err != -NFS4ERR_DELAY)
6694 break;
6695 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006696 } while (exception.retry);
6697 return err;
6698}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006699
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006700static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006701{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006702 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006703 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006704 };
6705 struct nfs41_free_stateid_res res;
6706 struct rpc_message msg = {
6707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6708 .rpc_argp = &args,
6709 .rpc_resp = &res,
6710 };
Chuck Lever38527b12012-07-11 16:30:23 -04006711 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006712
Chuck Lever38527b12012-07-11 16:30:23 -04006713 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006714 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006715 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006716 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006717 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006718 dprintk("NFS reply free_stateid: %d\n", status);
6719 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006720}
6721
Chuck Lever38527b12012-07-11 16:30:23 -04006722/**
6723 * nfs41_free_stateid - perform a FREE_STATEID operation
6724 *
6725 * @server: server / transport on which to perform the operation
6726 * @stateid: state ID to release
6727 *
6728 * Returns NFS_OK if the server freed "stateid". Otherwise a
6729 * negative NFS4ERR value is returned.
6730 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006731static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006732{
6733 struct nfs4_exception exception = { };
6734 int err;
6735 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006736 err = _nfs4_free_stateid(server, stateid);
6737 if (err != -NFS4ERR_DELAY)
6738 break;
6739 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006740 } while (exception.retry);
6741 return err;
6742}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006743
6744static bool nfs41_match_stateid(const nfs4_stateid *s1,
6745 const nfs4_stateid *s2)
6746{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006747 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006748 return false;
6749
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006750 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006751 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006752 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006753 return true;
6754
6755 return false;
6756}
6757
Andy Adamson557134a2009-04-01 09:21:53 -04006758#endif /* CONFIG_NFS_V4_1 */
6759
Trond Myklebust36281ca2012-03-04 18:13:56 -05006760static bool nfs4_match_stateid(const nfs4_stateid *s1,
6761 const nfs4_stateid *s2)
6762{
Trond Myklebustf597c532012-03-04 18:13:56 -05006763 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006764}
6765
6766
Trond Myklebust17280172012-03-11 13:11:00 -04006767static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006768 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006769 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 .recover_open = nfs4_open_reclaim,
6771 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006772 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006773 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006774 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775};
6776
Andy Adamson591d71c2009-04-01 09:22:47 -04006777#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006778static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006779 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6780 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6781 .recover_open = nfs4_open_reclaim,
6782 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006783 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006784 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006785 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006786 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006787};
6788#endif /* CONFIG_NFS_V4_1 */
6789
Trond Myklebust17280172012-03-11 13:11:00 -04006790static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006791 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006792 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 .recover_open = nfs4_open_expired,
6794 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006795 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006796 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797};
6798
Andy Adamson591d71c2009-04-01 09:22:47 -04006799#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006800static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006801 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6802 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006803 .recover_open = nfs41_open_expired,
6804 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006805 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006806 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006807};
6808#endif /* CONFIG_NFS_V4_1 */
6809
Trond Myklebust17280172012-03-11 13:11:00 -04006810static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006811 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006812 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006813 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006814};
6815
6816#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006817static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006818 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006819 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006820 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006821};
6822#endif
6823
Trond Myklebust97dc1352010-06-16 09:52:26 -04006824static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6825 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006826 .init_caps = NFS_CAP_READDIRPLUS
6827 | NFS_CAP_ATOMIC_OPEN
6828 | NFS_CAP_CHANGE_ATTR
6829 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006830 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006831 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006832 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006833 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6834 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6835 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006836};
6837
6838#if defined(CONFIG_NFS_V4_1)
6839static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6840 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006841 .init_caps = NFS_CAP_READDIRPLUS
6842 | NFS_CAP_ATOMIC_OPEN
6843 | NFS_CAP_CHANGE_ATTR
6844 | NFS_CAP_POSIX_LOCK,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006845 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006846 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006847 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006848 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6849 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6850 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006851};
6852#endif
6853
Trond Myklebust97dc1352010-06-16 09:52:26 -04006854const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6855 [0] = &nfs_v4_0_minor_ops,
6856#if defined(CONFIG_NFS_V4_1)
6857 [1] = &nfs_v4_1_minor_ops,
6858#endif
6859};
6860
Bryan Schumaker73a79702012-07-16 16:39:12 -04006861const struct inode_operations nfs4_dir_inode_operations = {
6862 .create = nfs_create,
6863 .lookup = nfs_lookup,
6864 .atomic_open = nfs_atomic_open,
6865 .link = nfs_link,
6866 .unlink = nfs_unlink,
6867 .symlink = nfs_symlink,
6868 .mkdir = nfs_mkdir,
6869 .rmdir = nfs_rmdir,
6870 .mknod = nfs_mknod,
6871 .rename = nfs_rename,
6872 .permission = nfs_permission,
6873 .getattr = nfs_getattr,
6874 .setattr = nfs_setattr,
6875 .getxattr = generic_getxattr,
6876 .setxattr = generic_setxattr,
6877 .listxattr = generic_listxattr,
6878 .removexattr = generic_removexattr,
6879};
6880
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006881static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006882 .permission = nfs_permission,
6883 .getattr = nfs_getattr,
6884 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006885 .getxattr = generic_getxattr,
6886 .setxattr = generic_setxattr,
6887 .listxattr = generic_listxattr,
6888 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006889};
6890
David Howells509de812006-08-22 20:06:11 -04006891const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 .version = 4, /* protocol version */
6893 .dentry_ops = &nfs4_dentry_operations,
6894 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006895 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006896 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006898 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006899 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 .getattr = nfs4_proc_getattr,
6901 .setattr = nfs4_proc_setattr,
6902 .lookup = nfs4_proc_lookup,
6903 .access = nfs4_proc_access,
6904 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 .create = nfs4_proc_create,
6906 .remove = nfs4_proc_remove,
6907 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006908 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 .unlink_done = nfs4_proc_unlink_done,
6910 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006911 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006912 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006913 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 .link = nfs4_proc_link,
6915 .symlink = nfs4_proc_symlink,
6916 .mkdir = nfs4_proc_mkdir,
6917 .rmdir = nfs4_proc_remove,
6918 .readdir = nfs4_proc_readdir,
6919 .mknod = nfs4_proc_mknod,
6920 .statfs = nfs4_proc_statfs,
6921 .fsinfo = nfs4_proc_fsinfo,
6922 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006923 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 .decode_dirent = nfs4_decode_dirent,
6925 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006926 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006927 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006928 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006930 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006931 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006932 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006934 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006935 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006937 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006938 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006939 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006940 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006941 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006942 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006943 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006944 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006945 .create_server = nfs4_create_server,
6946 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947};
6948
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006949static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6950 .prefix = XATTR_NAME_NFSV4_ACL,
6951 .list = nfs4_xattr_list_nfs4_acl,
6952 .get = nfs4_xattr_get_nfs4_acl,
6953 .set = nfs4_xattr_set_nfs4_acl,
6954};
6955
6956const struct xattr_handler *nfs4_xattr_handlers[] = {
6957 &nfs4_xattr_nfs4_acl_handler,
6958 NULL
6959};
6960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961/*
6962 * Local variables:
6963 * c-basic-offset: 8
6964 * End:
6965 */