blob: bdf3d07ce974098254562e8506d2a09435407eb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050096 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400108 case -NFS4ERR_ACCESS:
109 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700114 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400117 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * This is our standard bitmap for GETATTR requests.
122 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400123const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138};
139
Trond Myklebust1549210f2012-06-05 09:16:47 -0400140static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
156};
157
Andy Adamsone23008e2012-10-02 21:07:32 -0400158static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
162};
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
171};
172
Trond Myklebust4ce79712005-06-22 17:16:21 +0000173const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
176 0
177};
178
Fred Isamandae100c2011-07-30 20:52:37 -0400179const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700183 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Manoj Naik830b8e32006-06-09 09:34:25 -0400188const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
205};
206
Al Virobc4785c2006-10-19 23:28:51 -0700207static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct nfs4_readdir_arg *readdir)
209{
Al Viro0dbb4c62006-10-19 23:28:49 -0700210 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000213 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
216 }
217
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
222
223 /*
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
229 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800230 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800258 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Trond Myklebust65de8722008-12-23 15:21:44 -0500261static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
262{
263 int res = 0;
264
265 might_sleep();
266
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100271 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
276}
277
278/* This is the error handling routine for processes that are allowed
279 * to sleep.
280 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800281static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500282{
283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500284 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500285 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 int ret = errorcode;
287
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500292 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400294 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500295 exception->retry = 1;
296 return 0;
297 }
298 if (state == NULL)
299 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500304 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500307 if (state == NULL)
308 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400309 nfs_remove_bad_delegation(state->inode);
Trond Myklebust5d422302013-03-14 16:57:48 -0400310 ret = nfs4_schedule_stateid_recovery(server, state);
311 if (ret < 0)
312 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500313 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400314 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400315 if (state != NULL) {
316 ret = nfs4_schedule_stateid_recovery(server, state);
317 if (ret < 0)
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500321 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322 nfs4_schedule_lease_recovery(clp);
323 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500324#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400325 case -NFS4ERR_BADSESSION:
326 case -NFS4ERR_BADSLOT:
327 case -NFS4ERR_BAD_HIGH_SLOT:
328 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
329 case -NFS4ERR_DEADSESSION:
330 case -NFS4ERR_SEQ_FALSE_RETRY:
331 case -NFS4ERR_SEQ_MISORDERED:
332 dprintk("%s ERROR: %d Reset session\n", __func__,
333 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400334 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400335 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500336#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500337 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
341 */
342 ret = -EBUSY;
343 break;
344 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
347 ret = nfs4_delay(server->client, &exception->timeout);
348 if (ret != 0)
349 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400350 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500351 case -NFS4ERR_OLD_STATEID:
352 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800353 break;
354 case -NFS4ERR_BADOWNER:
355 /* The following works around a Linux server bug! */
356 case -NFS4ERR_BADNAME:
357 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
358 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
359 exception->retry = 1;
360 printk(KERN_WARNING "NFS: v4 server %s "
361 "does not accept raw "
362 "uid/gids. "
363 "Reenabling the idmapper.\n",
364 server->nfs_client->cl_hostname);
365 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500366 }
367 /* We failed to handle the error */
368 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500369wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500370 ret = nfs4_wait_clnt_recover(clp);
371 if (ret == 0)
372 exception->retry = 1;
373 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500374}
375
376
Trond Myklebust452e9352010-07-31 14:29:06 -0400377static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock(&clp->cl_lock);
380 if (time_before(clp->cl_last_renewal,timestamp))
381 clp->cl_last_renewal = timestamp;
382 spin_unlock(&clp->cl_lock);
383}
384
Trond Myklebust452e9352010-07-31 14:29:06 -0400385static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
386{
387 do_renew_lease(server->nfs_client, timestamp);
388}
389
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400390#if defined(CONFIG_NFS_V4_1)
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400393{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500394 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400395 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500396 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400397
Benny Halevydfb4f3092010-09-24 09:17:01 -0400398 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500401 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 return;
Andy Adamson13615872009-04-01 09:22:17 -0400403 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500404 tbl = res->sr_slot->table;
405 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500406
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500408 /* Be nice to the server: try to ensure that the last transmitted
409 * value for highest_user_slotid <= target_highest_slotid
410 */
411 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
412 send_new_highest_used_slotid = true;
413
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500414 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
415 send_new_highest_used_slotid = false;
416 goto out_unlock;
417 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500418 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500419
420 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
421 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500422out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500423 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400424 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500425 if (send_new_highest_used_slotid)
426 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400427}
428
Trond Myklebust14516c32010-07-31 14:29:06 -0400429static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500431 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500432 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400433 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500434 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500435 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436
Bryan Schumaker468f8612011-04-18 15:57:32 -0400437 /* don't increment the sequence number if the task wasn't sent */
438 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439 goto out;
440
Trond Myklebust933602e2012-11-16 12:12:38 -0500441 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500442 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500443
Trond Myklebustac20d162012-12-15 15:36:07 -0500444 if (slot->interrupted) {
445 slot->interrupted = 0;
446 interrupted = true;
447 }
448
Andy Adamson691daf32009-12-04 15:55:39 -0500449 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 switch (res->sr_status) {
451 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500453 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500455 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500456 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500459 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500461 case 1:
462 /*
463 * sr_status remains 1 if an RPC level error occurred.
464 * The server may or may not have processed the sequence
465 * operation..
466 * Mark the slot as having hosted an interrupted RPC call.
467 */
468 slot->interrupted = 1;
469 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500475 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500477 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500478 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500480 case -NFS4ERR_BADSLOT:
481 /*
482 * The slot id we used was probably retired. Try again
483 * using a different slot id.
484 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500485 goto retry_nowait;
486 case -NFS4ERR_SEQ_MISORDERED:
487 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500488 * Was the last operation on this sequence interrupted?
489 * If so, retry after bumping the sequence number.
490 */
491 if (interrupted) {
492 ++slot->seq_nr;
493 goto retry_nowait;
494 }
495 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500496 * Could this slot have been previously retired?
497 * If so, then the server may be expecting seq_nr = 1!
498 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500499 if (slot->seq_nr != 1) {
500 slot->seq_nr = 1;
501 goto retry_nowait;
502 }
503 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500504 case -NFS4ERR_SEQ_FALSE_RETRY:
505 ++slot->seq_nr;
506 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400507 default:
508 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500509 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 }
511out:
512 /* The session may be reset by one of the error handlers. */
513 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400514 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500515 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500516retry_nowait:
517 if (rpc_restart_call_prepare(task)) {
518 task->tk_status = 0;
519 ret = 0;
520 }
521 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400522out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400523 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400524 goto out;
525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
526 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527}
528
Trond Myklebust14516c32010-07-31 14:29:06 -0400529static int nfs4_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400531{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500532 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 return 1;
534 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400535}
536
Trond Myklebust9d12b212012-01-17 22:04:25 -0500537static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
539{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500540 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400542 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500543 if (cache_reply)
544 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 res->sr_slot = NULL;
546}
547
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400548static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
549{
550 args->sa_privileged = 1;
551}
552
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000553int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556 struct rpc_task *task)
557{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 struct nfs4_slot *slot;
559 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560
561 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400563 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400564 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 tbl = &session->fc_slot_table;
567
Trond Myklebust69d206b2012-11-22 13:21:02 -0500568 task->tk_timeout = 0;
569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400571 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400572 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500573 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500574 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400575 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400576 }
577
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500578 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500579 if (IS_ERR(slot)) {
580 /* If out of memory, try again in 1/4 second */
581 if (slot == ERR_PTR(-ENOMEM))
582 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400584 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 }
586 spin_unlock(&tbl->slot_tbl_lock);
587
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500588 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
591 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500594 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400595 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 /*
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
599 */
600 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400601out_success:
602 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400604out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 /* Privileged tasks are queued with top priority */
606 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400607 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
608 NULL, RPC_PRIORITY_PRIVILEGED);
609 else
610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400611 spin_unlock(&tbl->slot_tbl_lock);
612 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000614EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615
Trond Myklebust035168a2010-06-16 09:52:26 -0400616int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Trond Myklebust035168a2010-06-16 09:52:26 -0400621 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 int ret = 0;
623
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400624 if (session == NULL) {
625 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400628
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500629 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400630 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500631 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebust9d12b212012-01-17 22:04:25 -0500633 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634out:
635 dprintk("<-- %s status=%d\n", __func__, ret);
636 return ret;
637}
638
639struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400640 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643};
644
645static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400648 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
651
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400652 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653}
654
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660}
661
Trond Myklebust17280172012-03-11 13:11:00 -0400662static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665};
666
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400671 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672{
673 int ret;
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400676 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .seq_args = args,
678 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 };
680 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000681 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
685 };
686
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 task = rpc_run_task(&task_setup);
688 if (IS_ERR(task))
689 ret = PTR_ERR(task);
690 else {
691 ret = task->tk_status;
692 rpc_put_task(task);
693 }
694 return ret;
695}
696
Trond Myklebustdf896452010-06-16 09:52:26 -0400697#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400698static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500699void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
701{
702}
703
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400704static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
705{
706}
707
708
Trond Myklebust14516c32010-07-31 14:29:06 -0400709static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400711{
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714#endif /* CONFIG_NFS_V4_1 */
715
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400716static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000717int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400721 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724}
725
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400726static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400734 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400736 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400738
Trond Myklebust38478b22006-05-25 01:40:57 -0400739static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700745 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400746 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700747 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000748 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400753 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400762 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100766 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400767 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100768 int rpc_status;
769 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770};
771
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400772static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
774{
775 if (err != -EINVAL)
776 return false;
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
778 return false;
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
781 return true;
782}
783
784static enum open_claim_type4
785nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
787{
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
789 return claim;
790 switch (claim) {
791 default:
792 return claim;
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
799 }
800}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801
802static void nfs4_init_opendata_res(struct nfs4_opendata *p)
803{
804 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400807 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400808 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400809 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811}
812
Al Viro82a2c1b2011-06-22 18:30:55 -0400813static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400815 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400816 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818{
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
823
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (p == NULL)
826 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p->o_arg.seqid == NULL)
829 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
841 * are cached */
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700844 }
David Howells7539bba2006-08-22 20:06:09 -0400845 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
858 break;
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
864 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500865 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500866 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400867
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500870
871 verf[0] = jiffies;
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400879 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881 return p;
882err_free:
883 kfree(p);
884err:
885 dput(parent);
886 return NULL;
887}
888
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400889static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400893 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894
895 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 nfs4_put_state_owner(p->owner);
899 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400900 dput(p->dentry);
901 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500902 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 kfree(p);
904}
905
906static void nfs4_opendata_put(struct nfs4_opendata *p)
907{
908 if (p != NULL)
909 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910}
911
Trond Myklebust06f814a2006-01-03 09:55:07 +0100912static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
913{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100914 int ret;
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917 return ret;
918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921{
922 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923
Trond Myklebust536e43d2012-01-17 22:04:26 -0500924 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 goto out;
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400930 break;
931 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 return ret;
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400944{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500945 if (delegation == NULL)
946 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
952 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500953 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 1;
955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100958{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
968 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
986 }
987}
988
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400990{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400993 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 /*
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1001 */
1002 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001003 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1006 }
1007 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001012 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001016{
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001022
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1027
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001032 goto no_delegation_unlock;
1033
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
Trond Myklebustb7391f42008-12-23 15:21:52 -05001039 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001041 ret = 1;
1042no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044no_delegation:
1045 rcu_read_unlock();
1046
1047 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001049 ret = 1;
1050 }
1051
1052 return ret;
1053}
1054
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057{
1058 struct nfs_delegation *delegation;
1059
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 rcu_read_unlock();
1064 return;
1065 }
1066 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001067 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068}
1069
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071{
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1079
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001085 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001086 goto out_return_state;
1087 }
1088 spin_unlock(&state->owner->so_lock);
1089 }
Trond Myklebust34310432008-12-23 15:21:38 -05001090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001092 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001093 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001094 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001096 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001097 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001106 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
1109 return ERR_PTR(ret);
1110out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1113}
1114
Andy Adamsone23008e2012-10-02 21:07:32 -04001115static void
1116nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1117{
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1121
1122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1131 clp->cl_hostname);
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140}
1141
1142/*
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1145 */
1146static struct nfs4_state *
1147_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1148{
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1151 int ret;
1152
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1155 goto err;
1156 }
1157
1158 ret = -ESTALE;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1162 goto err;
1163
1164 ret = -ENOMEM;
1165 state = nfs4_get_open_state(inode, data->owner);
1166 if (state == NULL)
1167 goto err;
1168
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1170 if (ret)
1171 goto err;
1172
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1176 data->o_arg.fmode);
1177
1178 return state;
1179err:
1180 return ERR_PTR(ret);
1181
1182}
1183
1184static struct nfs4_state *
1185_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001186{
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001189 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001192 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 goto out;
1194 }
1195
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001200 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001201 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001202 goto err;
1203 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 state = nfs4_get_open_state(inode, data->owner);
1205 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001209 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001212out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001213 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001215err_put_inode:
1216 iput(inode);
1217err:
1218 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219}
1220
Andy Adamsone23008e2012-10-02 21:07:32 -04001221static struct nfs4_state *
1222nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1223{
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1227}
1228
Trond Myklebust864472e2006-01-03 09:55:15 +01001229static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1230{
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1233
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1237 continue;
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1240 return ctx;
1241 }
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1244}
1245
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001246static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001248{
1249 struct nfs4_opendata *opendata;
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1257 return opendata;
1258}
1259
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001261{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001262 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 int ret;
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001270 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 if (ret != 0)
1272 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001273 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001276 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001277 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 return 0;
1279}
1280
1281static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1282{
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 int ret;
1285
1286 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 smp_rmb();
1289 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 if (ret != 0)
1293 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001294 if (newstate != state)
1295 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 }
1297 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 if (ret != 0)
1301 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001302 if (newstate != state)
1303 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 }
1305 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 if (ret != 0)
1309 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001310 if (newstate != state)
1311 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001313 /*
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1316 */
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001319 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001322 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001323 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 return 0;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * OPEN_RECLAIM:
1329 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001331static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001333 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001334 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int status;
1337
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001345 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001346 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001349 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return status;
1351}
1352
Trond Myklebust539cd032007-06-05 11:46:42 -04001353static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1357 int err;
1358 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001359 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1361 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001362 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001363 break;
1364 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } while (exception.retry);
1366 return err;
1367}
1368
Trond Myklebust864472e2006-01-03 09:55:15 +01001369static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1370{
1371 struct nfs_open_context *ctx;
1372 int ret;
1373
1374 ctx = nfs4_state_find_open_context(state);
1375 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001376 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001377 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 put_nfs_open_context(ctx);
1379 return ret;
1380}
1381
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001382static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001384 switch (err) {
1385 default:
1386 printk(KERN_ERR "NFS: %s: unhandled error "
1387 "%d.\n", __func__, err);
1388 case 0:
1389 case -ENOENT:
1390 case -ESTALE:
1391 break;
1392 case -NFS4ERR_BADSESSION:
1393 case -NFS4ERR_BADSLOT:
1394 case -NFS4ERR_BAD_HIGH_SLOT:
1395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1396 case -NFS4ERR_DEADSESSION:
1397 set_bit(NFS_DELEGATED_STATE, &state->flags);
1398 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1399 return -EAGAIN;
1400 case -NFS4ERR_STALE_CLIENTID:
1401 case -NFS4ERR_STALE_STATEID:
1402 set_bit(NFS_DELEGATED_STATE, &state->flags);
1403 case -NFS4ERR_EXPIRED:
1404 /* Don't recall a delegation if it was lost */
1405 nfs4_schedule_lease_recovery(server->nfs_client);
1406 return -EAGAIN;
1407 case -NFS4ERR_DELEG_REVOKED:
1408 case -NFS4ERR_ADMIN_REVOKED:
1409 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001410 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001411 nfs_inode_find_state_and_recover(state->inode,
1412 stateid);
1413 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001414 return 0;
1415 case -NFS4ERR_DELAY:
1416 case -NFS4ERR_GRACE:
1417 set_bit(NFS_DELEGATED_STATE, &state->flags);
1418 ssleep(1);
1419 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001420 case -ENOMEM:
1421 case -NFS4ERR_DENIED:
1422 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1423 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return err;
1426}
1427
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001428int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1429{
1430 struct nfs_server *server = NFS_SERVER(state->inode);
1431 struct nfs4_opendata *opendata;
1432 int err;
1433
1434 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1435 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1436 if (IS_ERR(opendata))
1437 return PTR_ERR(opendata);
1438 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1439 err = nfs4_open_recover(opendata, state);
1440 nfs4_opendata_put(opendata);
1441 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1442}
1443
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001444static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1445{
1446 struct nfs4_opendata *data = calldata;
1447
1448 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001449 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001450 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001451 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001452 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001453 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001455}
1456
1457static void nfs4_open_confirm_release(void *calldata)
1458{
1459 struct nfs4_opendata *data = calldata;
1460 struct nfs4_state *state = NULL;
1461
1462 /* If this request hasn't been cancelled, do nothing */
1463 if (data->cancelled == 0)
1464 goto out_free;
1465 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001466 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001467 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001468 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001469 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001470 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001471out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001472 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473}
1474
1475static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476 .rpc_call_done = nfs4_open_confirm_done,
1477 .rpc_release = nfs4_open_confirm_release,
1478};
1479
1480/*
1481 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1482 */
1483static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1484{
1485 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1489 .rpc_argp = &data->c_arg,
1490 .rpc_resp = &data->c_res,
1491 .rpc_cred = data->owner->so_cred,
1492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001493 struct rpc_task_setup task_setup_data = {
1494 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001496 .callback_ops = &nfs4_open_confirm_ops,
1497 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001498 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 .flags = RPC_TASK_ASYNC,
1500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int status;
1502
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001503 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001504 data->rpc_done = 0;
1505 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001506 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001507 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001508 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001509 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001510 status = nfs4_wait_for_completion_rpc_task(task);
1511 if (status != 0) {
1512 data->cancelled = 1;
1513 smp_wmb();
1514 } else
1515 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001516 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return status;
1518}
1519
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 struct nfs4_opendata *data = calldata;
1523 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001526 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527 /*
1528 * Check if we still need to send an OPEN call, or if we can use
1529 * a delegation instead.
1530 */
1531 if (data->state != NULL) {
1532 struct nfs_delegation *delegation;
1533
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001534 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001535 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001536 rcu_read_lock();
1537 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001538 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1539 can_open_delegated(delegation, data->o_arg.fmode))
1540 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001541 rcu_read_unlock();
1542 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001543 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001544 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001545 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001547 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001548 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1549 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001550 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001551 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001552 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001553 &data->o_res.seq_res,
1554 task) != 0)
1555 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001556 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001557unlock_no_action:
1558 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001559out_no_action:
1560 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001561out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001562 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001563}
1564
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001565static void nfs4_open_done(struct rpc_task *task, void *calldata)
1566{
1567 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001570
Trond Myklebust14516c32010-07-31 14:29:06 -04001571 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1572 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001573
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001575 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1576 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001577 case S_IFREG:
1578 break;
1579 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001581 break;
1582 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001584 break;
1585 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001587 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001588 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001589 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001590 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1591 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001592 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001593 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001595
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596static void nfs4_open_release(void *calldata)
1597{
1598 struct nfs4_opendata *data = calldata;
1599 struct nfs4_state *state = NULL;
1600
1601 /* If this request hasn't been cancelled, do nothing */
1602 if (data->cancelled == 0)
1603 goto out_free;
1604 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001605 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 goto out_free;
1607 /* In case we need an open_confirm, no cleanup! */
1608 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1609 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001611 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001612 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001614 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615}
1616
1617static const struct rpc_call_ops nfs4_open_ops = {
1618 .rpc_call_prepare = nfs4_open_prepare,
1619 .rpc_call_done = nfs4_open_done,
1620 .rpc_release = nfs4_open_release,
1621};
1622
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624{
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_server *server = NFS_SERVER(dir);
1627 struct nfs_openargs *o_arg = &data->o_arg;
1628 struct nfs_openres *o_res = &data->o_res;
1629 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001630 struct rpc_message msg = {
1631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1632 .rpc_argp = o_arg,
1633 .rpc_resp = o_res,
1634 .rpc_cred = data->owner->so_cred,
1635 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001636 struct rpc_task_setup task_setup_data = {
1637 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001638 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001639 .callback_ops = &nfs4_open_ops,
1640 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001641 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001642 .flags = RPC_TASK_ASYNC,
1643 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001644 int status;
1645
Trond Myklebust9d12b212012-01-17 22:04:25 -05001646 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001647 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001648 data->rpc_done = 0;
1649 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001650 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001651 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001652 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001653 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001654 if (IS_ERR(task))
1655 return PTR_ERR(task);
1656 status = nfs4_wait_for_completion_rpc_task(task);
1657 if (status != 0) {
1658 data->cancelled = 1;
1659 smp_wmb();
1660 } else
1661 status = data->rpc_status;
1662 rpc_put_task(task);
1663
1664 return status;
1665}
1666
1667static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1668{
1669 struct inode *dir = data->dir->d_inode;
1670 struct nfs_openres *o_res = &data->o_res;
1671 int status;
1672
1673 status = nfs4_run_open_task(data, 1);
1674 if (status != 0 || !data->rpc_done)
1675 return status;
1676
Trond Myklebust6926afd2012-01-07 13:22:46 -05001677 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1678
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001679 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1680 status = _nfs4_proc_open_confirm(data);
1681 if (status != 0)
1682 return status;
1683 }
1684
1685 return status;
1686}
1687
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001688static int nfs4_opendata_access(struct rpc_cred *cred,
1689 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001690 struct nfs4_state *state, fmode_t fmode,
1691 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001692{
1693 struct nfs_access_entry cache;
1694 u32 mask;
1695
1696 /* access call failed or for some reason the server doesn't
1697 * support any access modes -- defer access call until later */
1698 if (opendata->o_res.access_supported == 0)
1699 return 0;
1700
1701 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001702 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001703 * write mode bits, but POSIX allows the creating process to write.
1704 * use openflags to check for exec, because fmode won't
1705 * always have FMODE_EXEC set when file open for exec. */
1706 if (openflags & __FMODE_EXEC) {
1707 /* ONLY check for exec rights */
1708 mask = MAY_EXEC;
1709 } else if (fmode & FMODE_READ)
1710 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001711
1712 cache.cred = cred;
1713 cache.jiffies = jiffies;
1714 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1715 nfs_access_add_cache(state->inode, &cache);
1716
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001717 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001718 return 0;
1719
1720 /* even though OPEN succeeded, access is denied. Close the file */
1721 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001722 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001723}
1724
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001725/*
1726 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1727 */
1728static int _nfs4_proc_open(struct nfs4_opendata *data)
1729{
1730 struct inode *dir = data->dir->d_inode;
1731 struct nfs_server *server = NFS_SERVER(dir);
1732 struct nfs_openargs *o_arg = &data->o_arg;
1733 struct nfs_openres *o_res = &data->o_res;
1734 int status;
1735
1736 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001737 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001739 if (status != 0) {
1740 if (status == -NFS4ERR_BADNAME &&
1741 !(o_arg->open_flags & O_CREAT))
1742 return -ENOENT;
1743 return status;
1744 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745
Trond Myklebust6926afd2012-01-07 13:22:46 -05001746 nfs_fattr_map_and_free_names(server, &data->f_attr);
1747
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001748 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001749 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001750 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1751 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001753 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001758 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Andy Adamsond83217c2011-03-01 01:34:17 +00001762static int nfs4_recover_expired_lease(struct nfs_server *server)
1763{
1764 return nfs4_client_recover_expired_lease(server->nfs_client);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * OPEN_EXPIRED:
1769 * reclaim state on the server after a network partition.
1770 * Assumes caller holds the appropriate lock
1771 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001772static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001774 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001775 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001777 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001778 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001779 if (IS_ERR(opendata))
1780 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001781 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001782 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001783 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001784 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001788static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001789{
Trond Myklebust539cd032007-06-05 11:46:42 -04001790 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001791 struct nfs4_exception exception = { };
1792 int err;
1793
1794 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001795 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001796 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1797 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001798 switch (err) {
1799 default:
1800 goto out;
1801 case -NFS4ERR_GRACE:
1802 case -NFS4ERR_DELAY:
1803 nfs4_handle_exception(server, err, &exception);
1804 err = 0;
1805 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001806 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001807out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001808 return err;
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Trond Myklebust864472e2006-01-03 09:55:15 +01001816 ctx = nfs4_state_find_open_context(state);
1817 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001818 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001819 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 put_nfs_open_context(ctx);
1821 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001824#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001825static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001826{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001827 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001828 nfs4_stateid *stateid = &state->stateid;
1829 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001830
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001831 /* If a state reset has been done, test_stateid is unneeded */
1832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1833 return;
1834
1835 status = nfs41_test_stateid(server, stateid);
1836 if (status != NFS_OK) {
1837 /* Free the stateid unless the server explicitly
1838 * informs us the stateid is unrecognized. */
1839 if (status != -NFS4ERR_BAD_STATEID)
1840 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001841 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001842
Bryan Schumaker69388672012-09-26 15:25:52 -04001843 write_seqlock(&state->seqlock);
1844 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1845 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001846 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1847 }
1848}
1849
1850/**
1851 * nfs41_check_open_stateid - possibly free an open stateid
1852 *
1853 * @state: NFSv4 state for an inode
1854 *
1855 * Returns NFS_OK if recovery for this stateid is now finished.
1856 * Otherwise a negative NFS4ERR value is returned.
1857 */
1858static int nfs41_check_open_stateid(struct nfs4_state *state)
1859{
1860 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001861 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 int status;
1863
1864 /* If a state reset has been done, test_stateid is unneeded */
1865 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1867 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1868 return -NFS4ERR_BAD_STATEID;
1869
1870 status = nfs41_test_stateid(server, stateid);
1871 if (status != NFS_OK) {
1872 /* Free the stateid unless the server explicitly
1873 * informs us the stateid is unrecognized. */
1874 if (status != -NFS4ERR_BAD_STATEID)
1875 nfs41_free_stateid(server, stateid);
1876
1877 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1879 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001880 }
1881 return status;
1882}
1883
1884static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1885{
Chuck Levereb64cf92012-07-11 16:30:05 -04001886 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001887
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001888 nfs41_clear_delegation_stateid(state);
1889 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001890 if (status != NFS_OK)
1891 status = nfs4_open_expired(sp, state);
1892 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001893}
1894#endif
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001897 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1898 * fields corresponding to attributes that were used to store the verifier.
1899 * Make sure we clobber those fields in the later setattr call
1900 */
1901static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1902{
1903 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1904 !(sattr->ia_valid & ATTR_ATIME_SET))
1905 sattr->ia_valid |= ATTR_ATIME;
1906
1907 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1908 !(sattr->ia_valid & ATTR_MTIME_SET))
1909 sattr->ia_valid |= ATTR_MTIME;
1910}
1911
Trond Myklebustc21443c2013-02-07 14:26:21 -05001912static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1913 fmode_t fmode,
1914 int flags,
1915 struct nfs4_state **res)
1916{
1917 struct nfs4_state_owner *sp = opendata->owner;
1918 struct nfs_server *server = sp->so_server;
1919 struct nfs4_state *state;
1920 unsigned int seq;
1921 int ret;
1922
1923 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1924
1925 ret = _nfs4_proc_open(opendata);
1926 if (ret != 0)
1927 goto out;
1928
1929 state = nfs4_opendata_to_nfs4_state(opendata);
1930 ret = PTR_ERR(state);
1931 if (IS_ERR(state))
1932 goto out;
1933 if (server->caps & NFS_CAP_POSIX_LOCK)
1934 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1935
1936 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1937 if (ret != 0)
1938 goto out;
1939
Trond Myklebust91876b12013-03-28 14:01:33 -04001940 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001941 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001942 *res = state;
1943out:
1944 return ret;
1945}
1946
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001947/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 */
Andy Adamson82be4172012-05-23 05:02:35 -04001950static int _nfs4_do_open(struct inode *dir,
1951 struct dentry *dentry,
1952 fmode_t fmode,
1953 int flags,
1954 struct iattr *sattr,
1955 struct rpc_cred *cred,
1956 struct nfs4_state **res,
1957 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct nfs4_state_owner *sp;
1960 struct nfs4_state *state = NULL;
1961 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001962 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001963 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001964 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001968 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1969 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1971 goto out_err;
1972 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001973 status = nfs4_recover_expired_lease(server);
1974 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001975 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001976 if (dentry->d_inode != NULL)
1977 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001978 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001979 if (dentry->d_inode)
1980 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001981 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001982 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001983 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001984 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Andy Adamson82be4172012-05-23 05:02:35 -04001986 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1987 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1988 if (!opendata->f_attr.mdsthreshold)
1989 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04001990 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04001991 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001992 if (dentry->d_inode != NULL)
1993 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001994
Trond Myklebustc21443c2013-02-07 14:26:21 -05001995 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001996 if (status != 0)
1997 goto err_opendata_put;
1998
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001999 if (opendata->o_arg.open_flags & O_EXCL) {
2000 nfs4_exclusive_attrset(opendata, sattr);
2001
2002 nfs_fattr_init(opendata->o_res.f_attr);
2003 status = nfs4_do_setattr(state->inode, cred,
2004 opendata->o_res.f_attr, sattr,
2005 state);
2006 if (status == 0)
2007 nfs_setattr_update_inode(state->inode, sattr);
2008 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2009 }
Andy Adamson82be4172012-05-23 05:02:35 -04002010
2011 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2012 *ctx_th = opendata->f_attr.mdsthreshold;
2013 else
2014 kfree(opendata->f_attr.mdsthreshold);
2015 opendata->f_attr.mdsthreshold = NULL;
2016
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002017 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *res = state;
2020 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002021err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002022 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002023 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002024err_put_state_owner:
2025 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 *res = NULL;
2028 return status;
2029}
2030
2031
Andy Adamson82be4172012-05-23 05:02:35 -04002032static struct nfs4_state *nfs4_do_open(struct inode *dir,
2033 struct dentry *dentry,
2034 fmode_t fmode,
2035 int flags,
2036 struct iattr *sattr,
2037 struct rpc_cred *cred,
2038 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002040 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 struct nfs4_exception exception = { };
2042 struct nfs4_state *res;
2043 int status;
2044
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002045 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002047 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2048 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (status == 0)
2050 break;
2051 /* NOTE: BAD_SEQID means the server and client disagree about the
2052 * book-keeping w.r.t. state-changing operations
2053 * (OPEN/CLOSE/LOCK/LOCKU...)
2054 * It is actually a sign of a bug on the client or on the server.
2055 *
2056 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002057 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * have unhashed the old state_owner for us, and that we can
2059 * therefore safely retry using a new one. We should still warn
2060 * the user though...
2061 */
2062 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002063 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002064 " returned a bad sequence-id error!\n",
2065 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 exception.retry = 1;
2067 continue;
2068 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002069 /*
2070 * BAD_STATEID on OPEN means that the server cancelled our
2071 * state before it received the OPEN_CONFIRM.
2072 * Recover by retrying the request as per the discussion
2073 * on Page 181 of RFC3530.
2074 */
2075 if (status == -NFS4ERR_BAD_STATEID) {
2076 exception.retry = 1;
2077 continue;
2078 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002079 if (status == -EAGAIN) {
2080 /* We must have found a delegation */
2081 exception.retry = 1;
2082 continue;
2083 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002084 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2085 continue;
2086 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 status, &exception));
2088 } while (exception.retry);
2089 return res;
2090}
2091
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002092static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2093 struct nfs_fattr *fattr, struct iattr *sattr,
2094 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002096 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002098 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 .iap = sattr,
2100 .server = server,
2101 .bitmask = server->attr_bitmask,
2102 };
2103 struct nfs_setattrres res = {
2104 .fattr = fattr,
2105 .server = server,
2106 };
2107 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2109 .rpc_argp = &arg,
2110 .rpc_resp = &res,
2111 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002114 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Trond Myklebust0e574af2005-10-27 22:12:38 -04002116 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Trond Myklebust5d422302013-03-14 16:57:48 -04002118 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002119 struct nfs_lockowner lockowner = {
2120 .l_owner = current->files,
2121 .l_pid = current->tgid,
2122 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002123 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002124 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002125 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2126 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002127 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002128 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Bryan Schumaker7c513052011-03-24 17:12:24 +00002131 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002132 if (status == 0 && state != NULL)
2133 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002134 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002137static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2138 struct nfs_fattr *fattr, struct iattr *sattr,
2139 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002141 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002142 struct nfs4_exception exception = {
2143 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002144 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 int err;
2147 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002148 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2149 switch (err) {
2150 case -NFS4ERR_OPENMODE:
2151 if (state && !(state->state & FMODE_WRITE)) {
2152 err = -EBADF;
2153 if (sattr->ia_valid & ATTR_OPEN)
2154 err = -EACCES;
2155 goto out;
2156 }
2157 }
2158 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002160out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return err;
2162}
2163
2164struct nfs4_closedata {
2165 struct inode *inode;
2166 struct nfs4_state *state;
2167 struct nfs_closeargs arg;
2168 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002169 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002170 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002171 bool roc;
2172 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173};
2174
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002175static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002176{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002177 struct nfs4_closedata *calldata = data;
2178 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002179 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002180
Fred Isamanf7e89172011-01-06 11:36:32 +00002181 if (calldata->roc)
2182 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002183 nfs4_put_open_state(calldata->state);
2184 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002185 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002186 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002187 kfree(calldata);
2188}
2189
Trond Myklebust88069f72009-12-08 08:33:16 -05002190static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2191 fmode_t fmode)
2192{
2193 spin_lock(&state->owner->so_lock);
2194 if (!(fmode & FMODE_READ))
2195 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2196 if (!(fmode & FMODE_WRITE))
2197 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2198 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2199 spin_unlock(&state->owner->so_lock);
2200}
2201
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002202static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002204 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct nfs_server *server = NFS_SERVER(calldata->inode);
2207
Chuck Levera3ca5652012-03-01 17:00:40 -05002208 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002209 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* hmm. we are done with the inode, and in the process of freeing
2212 * the state_owner. we keep this around to process errors
2213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 switch (task->tk_status) {
2215 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002216 if (calldata->roc)
2217 pnfs_roc_set_barrier(state->inode,
2218 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002219 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002220 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002221 nfs4_close_clear_stateid_flags(state,
2222 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002225 case -NFS4ERR_OLD_STATEID:
2226 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002228 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002231 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2232 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002234 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002235 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002236 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002239static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002241 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002242 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002243 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002244 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002245
Chuck Levera3ca5652012-03-01 17:00:40 -05002246 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002247 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002248 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002249
Trond Myklebust88069f72009-12-08 08:33:16 -05002250 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2251 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002252 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002253 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002254 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002255 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002256 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2257 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2258 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002259 }
2260 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002261 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2262 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2263 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002264 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002265 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002266 if (!nfs4_valid_open_stateid(state))
2267 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002268 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002269
2270 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002271 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002272 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002273 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002274
Fred Isamanf7e89172011-01-06 11:36:32 +00002275 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002276 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002277 if (calldata->roc &&
Trond Myklebust7fdab062012-09-20 20:15:57 -04002278 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002279 goto out_wait;
Fred Isamanf7e89172011-01-06 11:36:32 +00002280 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002281
Trond Myklebust516a6af2005-10-27 22:12:41 -04002282 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002283 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002284 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002285 &calldata->arg.seq_args,
2286 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002287 task) != 0)
2288 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002289 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002290 return;
2291out_no_action:
2292 task->tk_action = NULL;
2293out_wait:
2294 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002297static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002298 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002299 .rpc_call_done = nfs4_close_done,
2300 .rpc_release = nfs4_free_closedata,
2301};
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303/*
2304 * It is possible for data to be read/written from a mem-mapped file
2305 * after the sys_close call (which hits the vfs layer as a flush).
2306 * This means that we can't safely call nfsv4 close on a file until
2307 * the inode is cleared. This in turn means that we are not good
2308 * NFSv4 citizens - we do not indicate to the server to update the file's
2309 * share state even when we are done with one of the three share
2310 * stateid's in the inode.
2311 *
2312 * NOTE: Caller must be holding the sp->so_owner semaphore!
2313 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002314int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002316 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002318 struct nfs4_state_owner *sp = state->owner;
2319 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002320 struct rpc_message msg = {
2321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2322 .rpc_cred = state->owner->so_cred,
2323 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002324 struct rpc_task_setup task_setup_data = {
2325 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002326 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002327 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002328 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002329 .flags = RPC_TASK_ASYNC,
2330 };
Trond Myklebust95121352005-10-18 14:20:12 -07002331 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002333 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002335 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002336 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002337 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002339 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002340 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002342 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002343 if (calldata->arg.seqid == NULL)
2344 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002345 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002346 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002347 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002348 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002349 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002350 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002351 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002352
Trond Myklebust1174dd12010-12-21 10:52:24 -05002353 msg.rpc_argp = &calldata->arg;
2354 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002355 task_setup_data.callback_data = calldata;
2356 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002357 if (IS_ERR(task))
2358 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002359 status = 0;
2360 if (wait)
2361 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002362 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002363 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002364out_free_calldata:
2365 kfree(calldata);
2366out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002367 nfs4_put_open_state(state);
2368 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002369 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Trond Myklebust2b484292010-09-17 10:56:51 -04002372static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002373nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct nfs4_state *state;
2376
Trond Myklebust565277f2007-10-15 18:17:53 -04002377 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002378 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2379 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002380 if (IS_ERR(state))
2381 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002382 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002383 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Trond Myklebust1185a552009-12-03 15:54:02 -05002386static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002387{
2388 if (ctx->state == NULL)
2389 return;
2390 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002391 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002392 else
Al Viro643168c2011-06-22 18:20:23 -04002393 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2397{
Benny Halevy43652ad2009-04-01 09:21:54 -04002398 struct nfs4_server_caps_arg args = {
2399 .fhandle = fhandle,
2400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct nfs4_server_caps_res res = {};
2402 struct rpc_message msg = {
2403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002404 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 .rpc_resp = &res,
2406 };
2407 int status;
2408
Bryan Schumaker7c513052011-03-24 17:12:24 +00002409 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (status == 0) {
2411 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002412 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2413 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2414 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2415 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2416 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2418 server->caps |= NFS_CAP_ACLS;
2419 if (res.has_links != 0)
2420 server->caps |= NFS_CAP_HARDLINKS;
2421 if (res.has_symlinks != 0)
2422 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002423 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2424 server->caps |= NFS_CAP_FILEID;
2425 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2426 server->caps |= NFS_CAP_MODE;
2427 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2428 server->caps |= NFS_CAP_NLINK;
2429 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2430 server->caps |= NFS_CAP_OWNER;
2431 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2432 server->caps |= NFS_CAP_OWNER_GROUP;
2433 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2434 server->caps |= NFS_CAP_ATIME;
2435 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2436 server->caps |= NFS_CAP_CTIME;
2437 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2438 server->caps |= NFS_CAP_MTIME;
2439
Trond Myklebusta65318b2009-03-11 14:10:28 -04002440 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2441 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2442 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002444 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return status;
2448}
2449
Trond Myklebust55a97592006-06-09 09:34:19 -04002450int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 struct nfs4_exception exception = { };
2453 int err;
2454 do {
2455 err = nfs4_handle_exception(server,
2456 _nfs4_server_capabilities(server, fhandle),
2457 &exception);
2458 } while (exception.retry);
2459 return err;
2460}
2461
2462static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2463 struct nfs_fsinfo *info)
2464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct nfs4_lookup_root_arg args = {
2466 .bitmask = nfs4_fattr_bitmap,
2467 };
2468 struct nfs4_lookup_res res = {
2469 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002470 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .fh = fhandle,
2472 };
2473 struct rpc_message msg = {
2474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2475 .rpc_argp = &args,
2476 .rpc_resp = &res,
2477 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002478
Trond Myklebust0e574af2005-10-27 22:12:38 -04002479 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002480 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
2483static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2484 struct nfs_fsinfo *info)
2485{
2486 struct nfs4_exception exception = { };
2487 int err;
2488 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002489 err = _nfs4_lookup_root(server, fhandle, info);
2490 switch (err) {
2491 case 0:
2492 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002493 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002494 default:
2495 err = nfs4_handle_exception(server, err, &exception);
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002498out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return err;
2500}
2501
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002502static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2503 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2504{
2505 struct rpc_auth *auth;
2506 int ret;
2507
2508 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002509 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002510 ret = -EIO;
2511 goto out;
2512 }
2513 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002514out:
2515 return ret;
2516}
2517
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002518static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002519 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002521 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002522 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002524 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002525 if (len < 0)
2526 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002527
2528 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002529 /* AUTH_UNIX is the default flavor if none was specified,
2530 * thus has already been tried. */
2531 if (flav_array[i] == RPC_AUTH_UNIX)
2532 continue;
2533
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002534 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002535 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002536 continue;
2537 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002538 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002539 /*
2540 * -EACCESS could mean that the user doesn't have correct permissions
2541 * to access the mount. It could also mean that we tried to mount
2542 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2543 * existing mount programs don't handle -EACCES very well so it should
2544 * be mapped to -EPERM instead.
2545 */
2546 if (status == -EACCES)
2547 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002548 return status;
2549}
2550
2551/*
2552 * get the file handle for the "/" directory on the server
2553 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002554int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2555 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002556{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002557 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002558 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002559 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2560 /*
2561 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2562 * by nfs4_map_errors() as this function exits.
2563 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002564 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (status == 0)
2566 status = nfs4_server_capabilities(server, fhandle);
2567 if (status == 0)
2568 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002569 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Bryan Schumakerbae36242012-05-10 15:07:31 -04002572static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2573 struct nfs_fsinfo *info)
2574{
2575 int error;
2576 struct nfs_fattr *fattr = info->fattr;
2577
2578 error = nfs4_server_capabilities(server, mntfh);
2579 if (error < 0) {
2580 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2581 return error;
2582 }
2583
2584 error = nfs4_proc_getattr(server, mntfh, fattr);
2585 if (error < 0) {
2586 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2587 return error;
2588 }
2589
2590 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2591 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2592 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2593
2594 return error;
2595}
2596
Manoj Naik6b97fd32006-06-09 09:34:29 -04002597/*
2598 * Get locations and (maybe) other attributes of a referral.
2599 * Note that we'll actually follow the referral later when
2600 * we detect fsid mismatch in inode revalidation
2601 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002602static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2603 const struct qstr *name, struct nfs_fattr *fattr,
2604 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002605{
2606 int status = -ENOMEM;
2607 struct page *page = NULL;
2608 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002609
2610 page = alloc_page(GFP_KERNEL);
2611 if (page == NULL)
2612 goto out;
2613 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2614 if (locations == NULL)
2615 goto out;
2616
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002617 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002618 if (status != 0)
2619 goto out;
2620 /* Make sure server returned a different fsid for the referral */
2621 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002622 dprintk("%s: server did not return a different fsid for"
2623 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002624 status = -EIO;
2625 goto out;
2626 }
Andy Adamson533eb462011-06-13 18:25:56 -04002627 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2628 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002629
Andy Adamson533eb462011-06-13 18:25:56 -04002630 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002631 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002632 memset(fhandle, 0, sizeof(struct nfs_fh));
2633out:
2634 if (page)
2635 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002636 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 return status;
2638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2641{
2642 struct nfs4_getattr_arg args = {
2643 .fh = fhandle,
2644 .bitmask = server->attr_bitmask,
2645 };
2646 struct nfs4_getattr_res res = {
2647 .fattr = fattr,
2648 .server = server,
2649 };
2650 struct rpc_message msg = {
2651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2652 .rpc_argp = &args,
2653 .rpc_resp = &res,
2654 };
2655
Trond Myklebust0e574af2005-10-27 22:12:38 -04002656 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002657 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2661{
2662 struct nfs4_exception exception = { };
2663 int err;
2664 do {
2665 err = nfs4_handle_exception(server,
2666 _nfs4_proc_getattr(server, fhandle, fattr),
2667 &exception);
2668 } while (exception.retry);
2669 return err;
2670}
2671
2672/*
2673 * The file is not closed if it is opened due to the a request to change
2674 * the size of the file. The open call will not be needed once the
2675 * VFS layer lookup-intents are implemented.
2676 *
2677 * Close is called when the inode is destroyed.
2678 * If we haven't opened the file for O_WRONLY, we
2679 * need to in the size_change case to obtain a stateid.
2680 *
2681 * Got race?
2682 * Because OPEN is always done by name in nfsv4, it is
2683 * possible that we opened a different file by the same
2684 * name. We can recognize this race condition, but we
2685 * can't do anything about it besides returning an error.
2686 *
2687 * This will be fixed with VFS changes (lookup-intent).
2688 */
2689static int
2690nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2691 struct iattr *sattr)
2692{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002693 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002694 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002695 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 int status;
2697
Benny Halevy8a1636c2010-07-14 15:43:57 -04002698 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002699 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002700
Trond Myklebust0e574af2005-10-27 22:12:38 -04002701 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Andy Adamson26699402012-05-30 16:12:24 -04002703 /* Deal with open(O_TRUNC) */
2704 if (sattr->ia_valid & ATTR_OPEN)
2705 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2706
2707 /* Optimization: if the end result is no change, don't RPC */
2708 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2709 return 0;
2710
Trond Myklebustd5308382005-11-04 15:33:38 -05002711 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002712 if (sattr->ia_valid & ATTR_FILE) {
2713 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002714
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002715 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002716 if (ctx) {
2717 cred = ctx->cred;
2718 state = ctx->state;
2719 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002720 }
2721
2722 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002723 if (status == 0)
2724 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return status;
2726}
2727
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002728static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2729 const struct qstr *name, struct nfs_fh *fhandle,
2730 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002731{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002732 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002733 int status;
2734 struct nfs4_lookup_arg args = {
2735 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002736 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002737 .name = name,
2738 };
2739 struct nfs4_lookup_res res = {
2740 .server = server,
2741 .fattr = fattr,
2742 .fh = fhandle,
2743 };
2744 struct rpc_message msg = {
2745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2746 .rpc_argp = &args,
2747 .rpc_resp = &res,
2748 };
2749
2750 nfs_fattr_init(fattr);
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002753 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 dprintk("NFS reply lookup: %d\n", status);
2755 return status;
2756}
2757
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002758static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002759{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002760 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002761 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002762 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2763 fattr->nlink = 2;
2764}
2765
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002766static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2767 struct qstr *name, struct nfs_fh *fhandle,
2768 struct nfs_fattr *fattr)
2769{
2770 struct nfs4_exception exception = { };
2771 struct rpc_clnt *client = *clnt;
2772 int err;
2773 do {
2774 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2775 switch (err) {
2776 case -NFS4ERR_BADNAME:
2777 err = -ENOENT;
2778 goto out;
2779 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002780 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002781 goto out;
2782 case -NFS4ERR_WRONGSEC:
2783 err = -EPERM;
2784 if (client != *clnt)
2785 goto out;
2786
2787 client = nfs4_create_sec_client(client, dir, name);
2788 if (IS_ERR(client))
2789 return PTR_ERR(client);
2790
2791 exception.retry = 1;
2792 break;
2793 default:
2794 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2795 }
2796 } while (exception.retry);
2797
2798out:
2799 if (err == 0)
2800 *clnt = client;
2801 else if (client != *clnt)
2802 rpc_shutdown_client(client);
2803
2804 return err;
2805}
2806
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002807static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002808 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002810 int status;
2811 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002812
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002813 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2814 if (client != NFS_CLIENT(dir)) {
2815 rpc_shutdown_client(client);
2816 nfs_fixup_secinfo_attributes(fattr);
2817 }
2818 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002821struct rpc_clnt *
2822nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2823 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2824{
2825 int status;
2826 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2827
2828 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2829 if (status < 0) {
2830 rpc_shutdown_client(client);
2831 return ERR_PTR(status);
2832 }
2833 return client;
2834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2837{
Trond Myklebust76b32992007-08-10 17:45:11 -04002838 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct nfs4_accessargs args = {
2840 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002841 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002843 struct nfs4_accessres res = {
2844 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002845 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct rpc_message msg = {
2847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2848 .rpc_argp = &args,
2849 .rpc_resp = &res,
2850 .rpc_cred = entry->cred,
2851 };
2852 int mode = entry->mask;
2853 int status;
2854
2855 /*
2856 * Determine which access bits we want to ask for...
2857 */
2858 if (mode & MAY_READ)
2859 args.access |= NFS4_ACCESS_READ;
2860 if (S_ISDIR(inode->i_mode)) {
2861 if (mode & MAY_WRITE)
2862 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2863 if (mode & MAY_EXEC)
2864 args.access |= NFS4_ACCESS_LOOKUP;
2865 } else {
2866 if (mode & MAY_WRITE)
2867 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2868 if (mode & MAY_EXEC)
2869 args.access |= NFS4_ACCESS_EXECUTE;
2870 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002871
2872 res.fattr = nfs_alloc_fattr();
2873 if (res.fattr == NULL)
2874 return -ENOMEM;
2875
Bryan Schumaker7c513052011-03-24 17:12:24 +00002876 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002878 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002879 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002881 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return status;
2883}
2884
2885static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2886{
2887 struct nfs4_exception exception = { };
2888 int err;
2889 do {
2890 err = nfs4_handle_exception(NFS_SERVER(inode),
2891 _nfs4_proc_access(inode, entry),
2892 &exception);
2893 } while (exception.retry);
2894 return err;
2895}
2896
2897/*
2898 * TODO: For the time being, we don't try to get any attributes
2899 * along with any of the zero-copy operations READ, READDIR,
2900 * READLINK, WRITE.
2901 *
2902 * In the case of the first three, we want to put the GETATTR
2903 * after the read-type operation -- this is because it is hard
2904 * to predict the length of a GETATTR response in v4, and thus
2905 * align the READ data correctly. This means that the GETATTR
2906 * may end up partially falling into the page cache, and we should
2907 * shift it into the 'tail' of the xdr_buf before processing.
2908 * To do this efficiently, we need to know the total length
2909 * of data received, which doesn't seem to be available outside
2910 * of the RPC layer.
2911 *
2912 * In the case of WRITE, we also want to put the GETATTR after
2913 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002914 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 *
2916 * Both of these changes to the XDR layer would in fact be quite
2917 * minor, but I decided to leave them for a subsequent patch.
2918 */
2919static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2920 unsigned int pgbase, unsigned int pglen)
2921{
2922 struct nfs4_readlink args = {
2923 .fh = NFS_FH(inode),
2924 .pgbase = pgbase,
2925 .pglen = pglen,
2926 .pages = &page,
2927 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002928 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 struct rpc_message msg = {
2930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2931 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002932 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 };
2934
Bryan Schumaker7c513052011-03-24 17:12:24 +00002935 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 -07002936}
2937
2938static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2939 unsigned int pgbase, unsigned int pglen)
2940{
2941 struct nfs4_exception exception = { };
2942 int err;
2943 do {
2944 err = nfs4_handle_exception(NFS_SERVER(inode),
2945 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2946 &exception);
2947 } while (exception.retry);
2948 return err;
2949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002952 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954static int
2955nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002956 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002958 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 int status = 0;
2961
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002962 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2963 if (IS_ERR(ctx))
2964 return PTR_ERR(ctx);
2965
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002966 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002967 state = nfs4_do_open(dir, dentry, ctx->mode,
2968 flags, sattr, ctx->cred,
2969 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002970 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (IS_ERR(state)) {
2972 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002975 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002976 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002977 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002979 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
2983static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2984{
Trond Myklebust16e42952005-10-27 22:12:44 -04002985 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002986 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07002988 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002990 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002991 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2995 .rpc_argp = &args,
2996 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002998 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002999
Bryan Schumaker7c513052011-03-24 17:12:24 +00003000 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003001 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003002 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return status;
3004}
3005
3006static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3007{
3008 struct nfs4_exception exception = { };
3009 int err;
3010 do {
3011 err = nfs4_handle_exception(NFS_SERVER(dir),
3012 _nfs4_proc_remove(dir, name),
3013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003018static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003020 struct nfs_server *server = NFS_SERVER(dir);
3021 struct nfs_removeargs *args = msg->rpc_argp;
3022 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003024 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003026 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003029static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3030{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003031 nfs4_setup_sequence(NFS_SERVER(data->dir),
3032 &data->args.seq_args,
3033 &data->res.seq_res,
3034 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003037static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003039 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3040
Trond Myklebust14516c32010-07-31 14:29:06 -04003041 if (!nfs4_sequence_done(task, &res->seq_res))
3042 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003043 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003044 return 0;
3045 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003046 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Jeff Laytond3d41522010-09-17 17:31:57 -04003049static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3050{
3051 struct nfs_server *server = NFS_SERVER(dir);
3052 struct nfs_renameargs *arg = msg->rpc_argp;
3053 struct nfs_renameres *res = msg->rpc_resp;
3054
3055 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003056 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003057 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003058}
3059
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003060static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3061{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003062 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3063 &data->args.seq_args,
3064 &data->res.seq_res,
3065 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003066}
3067
3068static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3069 struct inode *new_dir)
3070{
3071 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3072
3073 if (!nfs4_sequence_done(task, &res->seq_res))
3074 return 0;
3075 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3076 return 0;
3077
3078 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003079 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003080 return 1;
3081}
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3084 struct inode *new_dir, struct qstr *new_name)
3085{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003086 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003087 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 .old_dir = NFS_FH(old_dir),
3089 .new_dir = NFS_FH(new_dir),
3090 .old_name = old_name,
3091 .new_name = new_name,
3092 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003093 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003094 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 struct rpc_message msg = {
3097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3098 .rpc_argp = &arg,
3099 .rpc_resp = &res,
3100 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003101 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Bryan Schumaker7c513052011-03-24 17:12:24 +00003103 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (!status) {
3105 update_changeattr(old_dir, &res.old_cinfo);
3106 update_changeattr(new_dir, &res.new_cinfo);
3107 }
3108 return status;
3109}
3110
3111static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3112 struct inode *new_dir, struct qstr *new_name)
3113{
3114 struct nfs4_exception exception = { };
3115 int err;
3116 do {
3117 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3118 _nfs4_proc_rename(old_dir, old_name,
3119 new_dir, new_name),
3120 &exception);
3121 } while (exception.retry);
3122 return err;
3123}
3124
3125static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3126{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003127 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct nfs4_link_arg arg = {
3129 .fh = NFS_FH(inode),
3130 .dir_fh = NFS_FH(dir),
3131 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003132 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003134 struct nfs4_link_res res = {
3135 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 struct rpc_message msg = {
3138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3139 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003140 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003142 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Trond Myklebust136f2622010-04-16 16:22:49 -04003144 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003145 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003146 goto out;
3147
Bryan Schumaker7c513052011-03-24 17:12:24 +00003148 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003149 if (!status) {
3150 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003151 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003152 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003153out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003154 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return status;
3156}
3157
3158static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3159{
3160 struct nfs4_exception exception = { };
3161 int err;
3162 do {
3163 err = nfs4_handle_exception(NFS_SERVER(inode),
3164 _nfs4_proc_link(inode, dir, name),
3165 &exception);
3166 } while (exception.retry);
3167 return err;
3168}
3169
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003170struct nfs4_createdata {
3171 struct rpc_message msg;
3172 struct nfs4_create_arg arg;
3173 struct nfs4_create_res res;
3174 struct nfs_fh fh;
3175 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003176};
3177
3178static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3179 struct qstr *name, struct iattr *sattr, u32 ftype)
3180{
3181 struct nfs4_createdata *data;
3182
3183 data = kzalloc(sizeof(*data), GFP_KERNEL);
3184 if (data != NULL) {
3185 struct nfs_server *server = NFS_SERVER(dir);
3186
3187 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3188 data->msg.rpc_argp = &data->arg;
3189 data->msg.rpc_resp = &data->res;
3190 data->arg.dir_fh = NFS_FH(dir);
3191 data->arg.server = server;
3192 data->arg.name = name;
3193 data->arg.attrs = sattr;
3194 data->arg.ftype = ftype;
3195 data->arg.bitmask = server->attr_bitmask;
3196 data->res.server = server;
3197 data->res.fh = &data->fh;
3198 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003200 }
3201 return data;
3202}
3203
3204static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3205{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003206 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003207 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003208 if (status == 0) {
3209 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3211 }
3212 return status;
3213}
3214
3215static void nfs4_free_createdata(struct nfs4_createdata *data)
3216{
3217 kfree(data);
3218}
3219
Chuck Lever4f390c12006-08-22 20:06:22 -04003220static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003221 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003223 struct nfs4_createdata *data;
3224 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Chuck Lever94a6d752006-08-22 20:06:23 -04003226 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003228
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 status = -ENOMEM;
3230 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3231 if (data == NULL)
3232 goto out;
3233
3234 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3235 data->arg.u.symlink.pages = &page;
3236 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003238 status = nfs4_do_create(dir, dentry, data);
3239
3240 nfs4_free_createdata(data);
3241out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return status;
3243}
3244
Chuck Lever4f390c12006-08-22 20:06:22 -04003245static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003246 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 struct nfs4_exception exception = { };
3249 int err;
3250 do {
3251 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003252 _nfs4_proc_symlink(dir, dentry, page,
3253 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 &exception);
3255 } while (exception.retry);
3256 return err;
3257}
3258
3259static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3260 struct iattr *sattr)
3261{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003262 struct nfs4_createdata *data;
3263 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003265 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3266 if (data == NULL)
3267 goto out;
3268
3269 status = nfs4_do_create(dir, dentry, data);
3270
3271 nfs4_free_createdata(data);
3272out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return status;
3274}
3275
3276static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3277 struct iattr *sattr)
3278{
3279 struct nfs4_exception exception = { };
3280 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003281
3282 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 do {
3284 err = nfs4_handle_exception(NFS_SERVER(dir),
3285 _nfs4_proc_mkdir(dir, dentry, sattr),
3286 &exception);
3287 } while (exception.retry);
3288 return err;
3289}
3290
3291static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003292 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 struct inode *dir = dentry->d_inode;
3295 struct nfs4_readdir_arg args = {
3296 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003297 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .pgbase = 0,
3299 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003300 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003301 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 };
3303 struct nfs4_readdir_res res;
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3306 .rpc_argp = &args,
3307 .rpc_resp = &res,
3308 .rpc_cred = cred,
3309 };
3310 int status;
3311
Harvey Harrison3110ff82008-05-02 13:42:44 -07003312 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003313 dentry->d_parent->d_name.name,
3314 dentry->d_name.name,
3315 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003316 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003318 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 -05003319 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003320 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003321 status += args.pgbase;
3322 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003323
3324 nfs_invalidate_atime(dir);
3325
Harvey Harrison3110ff82008-05-02 13:42:44 -07003326 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return status;
3328}
3329
3330static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003331 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 struct nfs4_exception exception = { };
3334 int err;
3335 do {
3336 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3337 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003338 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 &exception);
3340 } while (exception.retry);
3341 return err;
3342}
3343
3344static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3345 struct iattr *sattr, dev_t rdev)
3346{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003347 struct nfs4_createdata *data;
3348 int mode = sattr->ia_mode;
3349 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003351 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3352 if (data == NULL)
3353 goto out;
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003356 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003358 data->arg.ftype = NF4BLK;
3359 data->arg.u.device.specdata1 = MAJOR(rdev);
3360 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003363 data->arg.ftype = NF4CHR;
3364 data->arg.u.device.specdata1 = MAJOR(rdev);
3365 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003366 } else if (!S_ISSOCK(mode)) {
3367 status = -EINVAL;
3368 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003371 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003372out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003373 nfs4_free_createdata(data);
3374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return status;
3376}
3377
3378static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3379 struct iattr *sattr, dev_t rdev)
3380{
3381 struct nfs4_exception exception = { };
3382 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003383
3384 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 do {
3386 err = nfs4_handle_exception(NFS_SERVER(dir),
3387 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3388 &exception);
3389 } while (exception.retry);
3390 return err;
3391}
3392
3393static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3394 struct nfs_fsstat *fsstat)
3395{
3396 struct nfs4_statfs_arg args = {
3397 .fh = fhandle,
3398 .bitmask = server->attr_bitmask,
3399 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003400 struct nfs4_statfs_res res = {
3401 .fsstat = fsstat,
3402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3405 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003406 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 };
3408
Trond Myklebust0e574af2005-10-27 22:12:38 -04003409 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003410 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
3413static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3414{
3415 struct nfs4_exception exception = { };
3416 int err;
3417 do {
3418 err = nfs4_handle_exception(server,
3419 _nfs4_proc_statfs(server, fhandle, fsstat),
3420 &exception);
3421 } while (exception.retry);
3422 return err;
3423}
3424
3425static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fsinfo *fsinfo)
3427{
3428 struct nfs4_fsinfo_arg args = {
3429 .fh = fhandle,
3430 .bitmask = server->attr_bitmask,
3431 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003432 struct nfs4_fsinfo_res res = {
3433 .fsinfo = fsinfo,
3434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 struct rpc_message msg = {
3436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3437 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 };
3440
Bryan Schumaker7c513052011-03-24 17:12:24 +00003441 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442}
3443
3444static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3445{
3446 struct nfs4_exception exception = { };
3447 int err;
3448
3449 do {
3450 err = nfs4_handle_exception(server,
3451 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3452 &exception);
3453 } while (exception.retry);
3454 return err;
3455}
3456
3457static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3458{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003459 int error;
3460
Trond Myklebust0e574af2005-10-27 22:12:38 -04003461 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003462 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003463 if (error == 0) {
3464 /* block layout checks this! */
3465 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003466 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003467 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003468
3469 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
3472static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_pathconf *pathconf)
3474{
3475 struct nfs4_pathconf_arg args = {
3476 .fh = fhandle,
3477 .bitmask = server->attr_bitmask,
3478 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003479 struct nfs4_pathconf_res res = {
3480 .pathconf = pathconf,
3481 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3484 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003485 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 };
3487
3488 /* None of the pathconf attributes are mandatory to implement */
3489 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3490 memset(pathconf, 0, sizeof(*pathconf));
3491 return 0;
3492 }
3493
Trond Myklebust0e574af2005-10-27 22:12:38 -04003494 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003495 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
3498static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3499 struct nfs_pathconf *pathconf)
3500{
3501 struct nfs4_exception exception = { };
3502 int err;
3503
3504 do {
3505 err = nfs4_handle_exception(server,
3506 _nfs4_proc_pathconf(server, fhandle, pathconf),
3507 &exception);
3508 } while (exception.retry);
3509 return err;
3510}
3511
Trond Myklebust5521abf2013-03-16 20:54:34 -04003512int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003513 const struct nfs_open_context *ctx,
3514 const struct nfs_lock_context *l_ctx,
3515 fmode_t fmode)
3516{
3517 const struct nfs_lockowner *lockowner = NULL;
3518
3519 if (l_ctx != NULL)
3520 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003521 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003522}
3523EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3524
Trond Myklebust5521abf2013-03-16 20:54:34 -04003525static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3526 const struct nfs_open_context *ctx,
3527 const struct nfs_lock_context *l_ctx,
3528 fmode_t fmode)
3529{
3530 nfs4_stateid current_stateid;
3531
3532 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3533 return false;
3534 return nfs4_stateid_match(stateid, &current_stateid);
3535}
3536
3537static bool nfs4_error_stateid_expired(int err)
3538{
3539 switch (err) {
3540 case -NFS4ERR_DELEG_REVOKED:
3541 case -NFS4ERR_ADMIN_REVOKED:
3542 case -NFS4ERR_BAD_STATEID:
3543 case -NFS4ERR_STALE_STATEID:
3544 case -NFS4ERR_OLD_STATEID:
3545 case -NFS4ERR_OPENMODE:
3546 case -NFS4ERR_EXPIRED:
3547 return true;
3548 }
3549 return false;
3550}
3551
Benny Halevyd20581a2011-05-22 19:52:03 +03003552void __nfs4_read_done_cb(struct nfs_read_data *data)
3553{
Fred Isamancd841602012-04-20 14:47:44 -04003554 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003555}
3556
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003557static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Fred Isamancd841602012-04-20 14:47:44 -04003559 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003561 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003562 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003563 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003565
Benny Halevyd20581a2011-05-22 19:52:03 +03003566 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003568 renew_lease(server, data->timestamp);
3569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
Trond Myklebust5521abf2013-03-16 20:54:34 -04003572static bool nfs4_read_stateid_changed(struct rpc_task *task,
3573 struct nfs_readargs *args)
3574{
3575
3576 if (!nfs4_error_stateid_expired(task->tk_status) ||
3577 nfs4_stateid_is_current(&args->stateid,
3578 args->context,
3579 args->lock_context,
3580 FMODE_READ))
3581 return false;
3582 rpc_restart_call_prepare(task);
3583 return true;
3584}
3585
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003586static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3587{
3588
3589 dprintk("--> %s\n", __func__);
3590
3591 if (!nfs4_sequence_done(task, &data->res.seq_res))
3592 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003593 if (nfs4_read_stateid_changed(task, &data->args))
3594 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003595 return data->read_done_cb ? data->read_done_cb(task, data) :
3596 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003597}
3598
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003599static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003602 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003603 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003604 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003607static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3608{
Trond Myklebust9b206142013-03-17 15:52:00 -04003609 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003610 &data->args.seq_args,
3611 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003612 task))
3613 return;
3614 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3615 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
Fred Isamanb029bc92011-03-03 15:13:42 +00003618static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Fred Isamancd841602012-04-20 14:47:44 -04003620 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003622 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003623 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003624 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003626 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003628 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003629 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Trond Myklebust5521abf2013-03-16 20:54:34 -04003633static bool nfs4_write_stateid_changed(struct rpc_task *task,
3634 struct nfs_writeargs *args)
3635{
3636
3637 if (!nfs4_error_stateid_expired(task->tk_status) ||
3638 nfs4_stateid_is_current(&args->stateid,
3639 args->context,
3640 args->lock_context,
3641 FMODE_WRITE))
3642 return false;
3643 rpc_restart_call_prepare(task);
3644 return true;
3645}
3646
Fred Isamanb029bc92011-03-03 15:13:42 +00003647static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3648{
3649 if (!nfs4_sequence_done(task, &data->res.seq_res))
3650 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003651 if (nfs4_write_stateid_changed(task, &data->args))
3652 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003653 return data->write_done_cb ? data->write_done_cb(task, data) :
3654 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003655}
3656
Trond Myklebust5a37f852012-04-28 14:55:16 -04003657static
3658bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003659{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003660 const struct nfs_pgio_header *hdr = data->header;
3661
3662 /* Don't request attributes for pNFS or O_DIRECT writes */
3663 if (data->ds_clp != NULL || hdr->dreq != NULL)
3664 return false;
3665 /* Otherwise, request attributes if and only if we don't hold
3666 * a delegation
3667 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003668 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003669}
Fred Isamana69aef12011-03-03 15:13:47 +00003670
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003671static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Fred Isamancd841602012-04-20 14:47:44 -04003673 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003674
Trond Myklebust5a37f852012-04-28 14:55:16 -04003675 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003676 data->args.bitmask = NULL;
3677 data->res.fattr = NULL;
3678 } else
3679 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003680
Fred Isamanb029bc92011-03-03 15:13:42 +00003681 if (!data->write_done_cb)
3682 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003683 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 data->timestamp = jiffies;
3685
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003686 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003687 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003690static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3691{
Trond Myklebust9b206142013-03-17 15:52:00 -04003692 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003693 &data->args.seq_args,
3694 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003695 task))
3696 return;
3697 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3698 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
Fred Isaman0b7c0152012-04-20 14:47:39 -04003701static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3702{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003703 nfs4_setup_sequence(NFS_SERVER(data->inode),
3704 &data->args.seq_args,
3705 &data->res.seq_res,
3706 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Fred Isaman0b7c0152012-04-20 14:47:39 -04003709static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003712
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003713 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003714 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003715 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
Fred Isaman0b7c0152012-04-20 14:47:39 -04003720static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003721{
3722 if (!nfs4_sequence_done(task, &data->res.seq_res))
3723 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003724 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003725}
3726
Fred Isaman0b7c0152012-04-20 14:47:39 -04003727static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003729 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003730
Fred Isaman0b7c0152012-04-20 14:47:39 -04003731 if (data->commit_done_cb == NULL)
3732 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003733 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003734 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003735 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003738struct nfs4_renewdata {
3739 struct nfs_client *client;
3740 unsigned long timestamp;
3741};
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743/*
3744 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3745 * standalone procedure for queueing an asynchronous RENEW.
3746 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003747static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003748{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003749 struct nfs4_renewdata *data = calldata;
3750 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003751
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003752 if (atomic_read(&clp->cl_count) > 1)
3753 nfs4_schedule_state_renewal(clp);
3754 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003755 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003756}
3757
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003758static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003760 struct nfs4_renewdata *data = calldata;
3761 struct nfs_client *clp = data->client;
3762 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003765 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003766 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3767 return;
3768 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003769 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003770 return;
3771 }
3772 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003774 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003777static const struct rpc_call_ops nfs4_renew_ops = {
3778 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003779 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003780};
3781
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003782static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
3784 struct rpc_message msg = {
3785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3786 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003787 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003789 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003791 if (renew_flags == 0)
3792 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003793 if (!atomic_inc_not_zero(&clp->cl_count))
3794 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003795 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003796 if (data == NULL)
3797 return -ENOMEM;
3798 data->client = clp;
3799 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003801 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003804static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
3806 struct rpc_message msg = {
3807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3808 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003809 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 };
3811 unsigned long now = jiffies;
3812 int status;
3813
3814 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3815 if (status < 0)
3816 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003817 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 return 0;
3819}
3820
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821static inline int nfs4_server_supports_acls(struct nfs_server *server)
3822{
3823 return (server->caps & NFS_CAP_ACLS)
3824 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3825 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3826}
3827
Trond Myklebust21f498c2012-08-24 10:59:25 -04003828/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3829 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003830 * the stack.
3831 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003832#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003833
Neil Hormane9e3d722011-03-04 19:26:03 -05003834static int buf_to_pages_noslab(const void *buf, size_t buflen,
3835 struct page **pages, unsigned int *pgbase)
3836{
3837 struct page *newpage, **spages;
3838 int rc = 0;
3839 size_t len;
3840 spages = pages;
3841
3842 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003843 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003844 newpage = alloc_page(GFP_KERNEL);
3845
3846 if (newpage == NULL)
3847 goto unwind;
3848 memcpy(page_address(newpage), buf, len);
3849 buf += len;
3850 buflen -= len;
3851 *pages++ = newpage;
3852 rc++;
3853 } while (buflen != 0);
3854
3855 return rc;
3856
3857unwind:
3858 for(; rc > 0; rc--)
3859 __free_page(spages[rc-1]);
3860 return -ENOMEM;
3861}
3862
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003863struct nfs4_cached_acl {
3864 int cached;
3865 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003866 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003867};
3868
3869static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003870{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003871 struct nfs_inode *nfsi = NFS_I(inode);
3872
3873 spin_lock(&inode->i_lock);
3874 kfree(nfsi->nfs4_acl);
3875 nfsi->nfs4_acl = acl;
3876 spin_unlock(&inode->i_lock);
3877}
3878
3879static void nfs4_zap_acl_attr(struct inode *inode)
3880{
3881 nfs4_set_cached_acl(inode, NULL);
3882}
3883
3884static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3885{
3886 struct nfs_inode *nfsi = NFS_I(inode);
3887 struct nfs4_cached_acl *acl;
3888 int ret = -ENOENT;
3889
3890 spin_lock(&inode->i_lock);
3891 acl = nfsi->nfs4_acl;
3892 if (acl == NULL)
3893 goto out;
3894 if (buf == NULL) /* user is just asking for length */
3895 goto out_len;
3896 if (acl->cached == 0)
3897 goto out;
3898 ret = -ERANGE; /* see getxattr(2) man page */
3899 if (acl->len > buflen)
3900 goto out;
3901 memcpy(buf, acl->data, acl->len);
3902out_len:
3903 ret = acl->len;
3904out:
3905 spin_unlock(&inode->i_lock);
3906 return ret;
3907}
3908
Sachin Prabhu5794d212012-04-17 14:36:40 +01003909static 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 +00003910{
3911 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003912 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003913
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003914 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003915 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003916 if (acl == NULL)
3917 goto out;
3918 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003919 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003920 } else {
3921 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3922 if (acl == NULL)
3923 goto out;
3924 acl->cached = 0;
3925 }
3926 acl->len = acl_len;
3927out:
3928 nfs4_set_cached_acl(inode, acl);
3929}
3930
Andy Adamsonbf118a32011-12-07 11:55:27 -05003931/*
3932 * The getxattr API returns the required buffer length when called with a
3933 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3934 * the required buf. On a NULL buf, we send a page of data to the server
3935 * guessing that the ACL request can be serviced by a page. If so, we cache
3936 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3937 * the cache. If not so, we throw away the page, and cache the required
3938 * length. The next getxattr call will then produce another round trip to
3939 * the server, this time with the input buf of the required size.
3940 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003941static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003942{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003943 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003944 struct nfs_getaclargs args = {
3945 .fh = NFS_FH(inode),
3946 .acl_pages = pages,
3947 .acl_len = buflen,
3948 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003949 struct nfs_getaclres res = {
3950 .acl_len = buflen,
3951 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3954 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003955 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003956 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003957 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3958 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003959
Andy Adamsonbf118a32011-12-07 11:55:27 -05003960 /* As long as we're doing a round trip to the server anyway,
3961 * let's be prepared for a page of acl data. */
3962 if (npages == 0)
3963 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003964 if (npages > ARRAY_SIZE(pages))
3965 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003966
Andy Adamsonbf118a32011-12-07 11:55:27 -05003967 for (i = 0; i < npages; i++) {
3968 pages[i] = alloc_page(GFP_KERNEL);
3969 if (!pages[i])
3970 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003971 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003972
3973 /* for decoding across pages */
3974 res.acl_scratch = alloc_page(GFP_KERNEL);
3975 if (!res.acl_scratch)
3976 goto out_free;
3977
Andy Adamsonbf118a32011-12-07 11:55:27 -05003978 args.acl_len = npages * PAGE_SIZE;
3979 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003980
Peng Taode040be2012-01-10 22:42:47 +08003981 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003982 __func__, buf, buflen, npages, args.acl_len);
3983 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3984 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003985 if (ret)
3986 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003987
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003988 /* Handle the case where the passed-in buffer is too short */
3989 if (res.acl_flags & NFS4_ACL_TRUNC) {
3990 /* Did the user only issue a request for the acl length? */
3991 if (buf == NULL)
3992 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003993 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003994 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003995 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003996 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01003997 if (buf) {
3998 if (res.acl_len > buflen) {
3999 ret = -ERANGE;
4000 goto out_free;
4001 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004002 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004003 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004004out_ok:
4005 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004006out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004007 for (i = 0; i < npages; i++)
4008 if (pages[i])
4009 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004010 if (res.acl_scratch)
4011 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004012 return ret;
4013}
4014
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004015static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4016{
4017 struct nfs4_exception exception = { };
4018 ssize_t ret;
4019 do {
4020 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4021 if (ret >= 0)
4022 break;
4023 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4024 } while (exception.retry);
4025 return ret;
4026}
4027
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004028static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4029{
4030 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004031 int ret;
4032
4033 if (!nfs4_server_supports_acls(server))
4034 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004035 ret = nfs_revalidate_inode(server, inode);
4036 if (ret < 0)
4037 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004038 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4039 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004040 ret = nfs4_read_cached_acl(inode, buf, buflen);
4041 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004042 /* -ENOENT is returned if there is no ACL or if there is an ACL
4043 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004044 return ret;
4045 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004046}
4047
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004048static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004049{
4050 struct nfs_server *server = NFS_SERVER(inode);
4051 struct page *pages[NFS4ACL_MAXPAGES];
4052 struct nfs_setaclargs arg = {
4053 .fh = NFS_FH(inode),
4054 .acl_pages = pages,
4055 .acl_len = buflen,
4056 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004057 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004058 struct rpc_message msg = {
4059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4060 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004061 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004062 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004063 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004064 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004065
4066 if (!nfs4_server_supports_acls(server))
4067 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004068 if (npages > ARRAY_SIZE(pages))
4069 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004070 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4071 if (i < 0)
4072 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004073 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004074 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004075
4076 /*
4077 * Free each page after tx, so the only ref left is
4078 * held by the network stack
4079 */
4080 for (; i > 0; i--)
4081 put_page(pages[i-1]);
4082
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004083 /*
4084 * Acl update can result in inode attribute update.
4085 * so mark the attribute cache invalid.
4086 */
4087 spin_lock(&inode->i_lock);
4088 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4089 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004090 nfs_access_zap_cache(inode);
4091 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004092 return ret;
4093}
4094
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004095static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4096{
4097 struct nfs4_exception exception = { };
4098 int err;
4099 do {
4100 err = nfs4_handle_exception(NFS_SERVER(inode),
4101 __nfs4_proc_set_acl(inode, buf, buflen),
4102 &exception);
4103 } while (exception.retry);
4104 return err;
4105}
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004108nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004110 struct nfs_client *clp = server->nfs_client;
4111
4112 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return 0;
4114 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004115 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004116 case -NFS4ERR_ADMIN_REVOKED:
4117 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004118 if (state == NULL)
4119 break;
4120 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004121 case -NFS4ERR_OPENMODE:
4122 if (state == NULL)
4123 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004124 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4125 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004126 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004127 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004128 if (state != NULL) {
4129 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4130 goto stateid_invalid;
4131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004133 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004134 nfs4_schedule_lease_recovery(clp);
4135 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004136#if defined(CONFIG_NFS_V4_1)
4137 case -NFS4ERR_BADSESSION:
4138 case -NFS4ERR_BADSLOT:
4139 case -NFS4ERR_BAD_HIGH_SLOT:
4140 case -NFS4ERR_DEADSESSION:
4141 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4142 case -NFS4ERR_SEQ_FALSE_RETRY:
4143 case -NFS4ERR_SEQ_MISORDERED:
4144 dprintk("%s ERROR %d, Reset session\n", __func__,
4145 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004146 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004147 task->tk_status = 0;
4148 return -EAGAIN;
4149#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004151 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004152 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4154 task->tk_status = 0;
4155 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004156 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 case -NFS4ERR_OLD_STATEID:
4158 task->tk_status = 0;
4159 return -EAGAIN;
4160 }
4161 task->tk_status = nfs4_map_errors(task->tk_status);
4162 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004163stateid_invalid:
4164 task->tk_status = -EIO;
4165 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004166wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004167 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004168 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4169 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4170 task->tk_status = 0;
4171 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
Chuck Leverf0920752012-05-21 22:45:41 -04004174static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4175 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004176{
4177 __be32 verf[2];
4178
Chuck Lever2c820d92012-05-21 22:45:33 -04004179 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4180 /* An impossible timestamp guarantees this value
4181 * will never match a generated boot time. */
4182 verf[0] = 0;
4183 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4184 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004185 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4186 verf[0] = (__be32)nn->boot_time.tv_sec;
4187 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004188 }
Chuck Levercd937102012-03-02 17:14:31 -05004189 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4190}
4191
Chuck Levere984a552012-09-14 17:24:21 -04004192static unsigned int
4193nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4194 char *buf, size_t len)
4195{
4196 unsigned int result;
4197
4198 rcu_read_lock();
4199 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4200 clp->cl_ipaddr,
4201 rpc_peeraddr2str(clp->cl_rpcclient,
4202 RPC_DISPLAY_ADDR),
4203 rpc_peeraddr2str(clp->cl_rpcclient,
4204 RPC_DISPLAY_PROTO));
4205 rcu_read_unlock();
4206 return result;
4207}
4208
4209static unsigned int
4210nfs4_init_uniform_client_string(const struct nfs_client *clp,
4211 char *buf, size_t len)
4212{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004213 char *nodename = clp->cl_rpcclient->cl_nodename;
4214
4215 if (nfs4_client_id_uniquifier[0] != '\0')
4216 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004217 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4218 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004219 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004220}
4221
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004222/**
4223 * nfs4_proc_setclientid - Negotiate client ID
4224 * @clp: state data structure
4225 * @program: RPC program for NFSv4 callback service
4226 * @port: IP port number for NFS4 callback service
4227 * @cred: RPC credential to use for this call
4228 * @res: where to place the result
4229 *
4230 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4231 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004232int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4233 unsigned short port, struct rpc_cred *cred,
4234 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
4236 nfs4_verifier sc_verifier;
4237 struct nfs4_setclientid setclientid = {
4238 .sc_verifier = &sc_verifier,
4239 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004240 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 };
4242 struct rpc_message msg = {
4243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4244 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004245 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004246 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004248 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Chuck Leverde734832012-07-11 16:30:50 -04004250 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004251 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004252 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4253 setclientid.sc_name_len =
4254 nfs4_init_uniform_client_string(clp,
4255 setclientid.sc_name,
4256 sizeof(setclientid.sc_name));
4257 else
4258 setclientid.sc_name_len =
4259 nfs4_init_nonuniform_client_string(clp,
4260 setclientid.sc_name,
4261 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004262 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004263 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004264 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004265 sizeof(setclientid.sc_netid),
4266 rpc_peeraddr2str(clp->cl_rpcclient,
4267 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004268 rcu_read_unlock();
4269 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004270 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 clp->cl_ipaddr, port >> 8, port & 255);
4272
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004273 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4274 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4275 setclientid.sc_name_len, setclientid.sc_name);
4276 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4277 dprintk("NFS reply setclientid: %d\n", status);
4278 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004281/**
4282 * nfs4_proc_setclientid_confirm - Confirm client ID
4283 * @clp: state data structure
4284 * @res: result of a previous SETCLIENTID
4285 * @cred: RPC credential to use for this call
4286 *
4287 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4288 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004289int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004290 struct nfs4_setclientid_res *arg,
4291 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
4293 struct nfs_fsinfo fsinfo;
4294 struct rpc_message msg = {
4295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004296 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004298 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 };
4300 unsigned long now;
4301 int status;
4302
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004303 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4304 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4305 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004307 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 if (status == 0) {
4309 spin_lock(&clp->cl_lock);
4310 clp->cl_lease_time = fsinfo.lease_time * HZ;
4311 clp->cl_last_renewal = now;
4312 spin_unlock(&clp->cl_lock);
4313 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004314 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return status;
4316}
4317
Trond Myklebustfe650402006-01-03 09:55:18 +01004318struct nfs4_delegreturndata {
4319 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004320 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004321 struct nfs_fh fh;
4322 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004323 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004324 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004325 int rpc_status;
4326};
4327
Trond Myklebustfe650402006-01-03 09:55:18 +01004328static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4329{
4330 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004331
Trond Myklebust14516c32010-07-31 14:29:06 -04004332 if (!nfs4_sequence_done(task, &data->res.seq_res))
4333 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004334
Ricardo Labiaga79708862009-12-07 09:23:21 -05004335 switch (task->tk_status) {
4336 case -NFS4ERR_STALE_STATEID:
4337 case -NFS4ERR_EXPIRED:
4338 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004339 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004340 break;
4341 default:
4342 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4343 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004344 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004345 return;
4346 }
4347 }
4348 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004349}
4350
4351static void nfs4_delegreturn_release(void *calldata)
4352{
Trond Myklebustfe650402006-01-03 09:55:18 +01004353 kfree(calldata);
4354}
4355
Andy Adamson938e1012009-04-01 09:22:28 -04004356#if defined(CONFIG_NFS_V4_1)
4357static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4358{
4359 struct nfs4_delegreturndata *d_data;
4360
4361 d_data = (struct nfs4_delegreturndata *)data;
4362
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004363 nfs4_setup_sequence(d_data->res.server,
4364 &d_data->args.seq_args,
4365 &d_data->res.seq_res,
4366 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004367}
4368#endif /* CONFIG_NFS_V4_1 */
4369
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004370static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004371#if defined(CONFIG_NFS_V4_1)
4372 .rpc_call_prepare = nfs4_delegreturn_prepare,
4373#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004374 .rpc_call_done = nfs4_delegreturn_done,
4375 .rpc_release = nfs4_delegreturn_release,
4376};
4377
Trond Myklebuste6f81072008-01-24 18:14:34 -05004378static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004379{
4380 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004381 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004382 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004383 struct rpc_message msg = {
4384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4385 .rpc_cred = cred,
4386 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004387 struct rpc_task_setup task_setup_data = {
4388 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004389 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004390 .callback_ops = &nfs4_delegreturn_ops,
4391 .flags = RPC_TASK_ASYNC,
4392 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004393 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004394
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004395 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004396 if (data == NULL)
4397 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004398 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004399 data->args.fhandle = &data->fh;
4400 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004401 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004402 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004403 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004404 data->res.fattr = &data->fattr;
4405 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004406 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004407 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004408 data->rpc_status = 0;
4409
Trond Myklebustc970aa82007-07-14 15:39:59 -04004410 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004411 msg.rpc_argp = &data->args;
4412 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004413 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004414 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004415 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004416 if (!issync)
4417 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004418 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004419 if (status != 0)
4420 goto out;
4421 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004422 if (status == 0)
4423 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4424 else
4425 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004426out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004427 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004428 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
Trond Myklebuste6f81072008-01-24 18:14:34 -05004431int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
4433 struct nfs_server *server = NFS_SERVER(inode);
4434 struct nfs4_exception exception = { };
4435 int err;
4436 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004437 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 switch (err) {
4439 case -NFS4ERR_STALE_STATEID:
4440 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 case 0:
4442 return 0;
4443 }
4444 err = nfs4_handle_exception(server, err, &exception);
4445 } while (exception.retry);
4446 return err;
4447}
4448
4449#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4450#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4451
4452/*
4453 * sleep, with exponential backoff, and retry the LOCK operation.
4454 */
4455static unsigned long
4456nfs4_set_lock_task_retry(unsigned long timeout)
4457{
Jeff Laytond3103102011-12-01 22:44:39 +01004458 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 timeout <<= 1;
4460 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4461 return NFS4_LOCK_MAXTIMEOUT;
4462 return timeout;
4463}
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4466{
4467 struct inode *inode = state->inode;
4468 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004469 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004470 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004472 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004474 struct nfs_lockt_res res = {
4475 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 };
4477 struct rpc_message msg = {
4478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4479 .rpc_argp = &arg,
4480 .rpc_resp = &res,
4481 .rpc_cred = state->owner->so_cred,
4482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct nfs4_lock_state *lsp;
4484 int status;
4485
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004486 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004487 status = nfs4_set_lock_state(state, request);
4488 if (status != 0)
4489 goto out;
4490 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004491 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004492 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004493 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004494 switch (status) {
4495 case 0:
4496 request->fl_type = F_UNLCK;
4497 break;
4498 case -NFS4ERR_DENIED:
4499 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004501 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004502out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 return status;
4504}
4505
4506static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4507{
4508 struct nfs4_exception exception = { };
4509 int err;
4510
4511 do {
4512 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4513 _nfs4_proc_getlk(state, cmd, request),
4514 &exception);
4515 } while (exception.retry);
4516 return err;
4517}
4518
4519static int do_vfs_lock(struct file *file, struct file_lock *fl)
4520{
4521 int res = 0;
4522 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4523 case FL_POSIX:
4524 res = posix_lock_file_wait(file, fl);
4525 break;
4526 case FL_FLOCK:
4527 res = flock_lock_file_wait(file, fl);
4528 break;
4529 default:
4530 BUG();
4531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return res;
4533}
4534
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004535struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004536 struct nfs_locku_args arg;
4537 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004538 struct nfs4_lock_state *lsp;
4539 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004540 struct file_lock fl;
4541 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004542 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004543};
4544
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004545static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4546 struct nfs_open_context *ctx,
4547 struct nfs4_lock_state *lsp,
4548 struct nfs_seqid *seqid)
4549{
4550 struct nfs4_unlockdata *p;
4551 struct inode *inode = lsp->ls_state->inode;
4552
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004553 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004554 if (p == NULL)
4555 return NULL;
4556 p->arg.fh = NFS_FH(inode);
4557 p->arg.fl = &p->fl;
4558 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004559 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004560 p->arg.stateid = &lsp->ls_stateid;
4561 p->lsp = lsp;
4562 atomic_inc(&lsp->ls_count);
4563 /* Ensure we don't close file until we're done freeing locks! */
4564 p->ctx = get_nfs_open_context(ctx);
4565 memcpy(&p->fl, fl, sizeof(p->fl));
4566 p->server = NFS_SERVER(inode);
4567 return p;
4568}
4569
Trond Myklebust06f814a2006-01-03 09:55:07 +01004570static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004571{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004572 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004573 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004574 nfs4_put_lock_state(calldata->lsp);
4575 put_nfs_open_context(calldata->ctx);
4576 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004577}
4578
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004579static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004580{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004581 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004582
Trond Myklebust14516c32010-07-31 14:29:06 -04004583 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4584 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004585 switch (task->tk_status) {
4586 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004587 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4588 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004589 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004590 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004591 case -NFS4ERR_BAD_STATEID:
4592 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004593 case -NFS4ERR_STALE_STATEID:
4594 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004595 break;
4596 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004597 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004598 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004599 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004600 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601}
4602
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004603static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004605 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004607 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004608 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004609 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004610 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004611 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004612 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004613 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004614 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004615 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004616 &calldata->res.seq_res,
4617 task) != 0)
4618 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004619 return;
4620out_no_action:
4621 task->tk_action = NULL;
4622out_wait:
4623 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624}
4625
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004626static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004627 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004628 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004629 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004630};
4631
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4633 struct nfs_open_context *ctx,
4634 struct nfs4_lock_state *lsp,
4635 struct nfs_seqid *seqid)
4636{
4637 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4640 .rpc_cred = ctx->cred,
4641 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004642 struct rpc_task_setup task_setup_data = {
4643 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004644 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004645 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004646 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004647 .flags = RPC_TASK_ASYNC,
4648 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004649
Frank Filz137d6ac2007-07-09 15:32:29 -07004650 /* Ensure this is an unlock - when canceling a lock, the
4651 * canceled lock is passed in, and it won't be an unlock.
4652 */
4653 fl->fl_type = F_UNLCK;
4654
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004655 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4656 if (data == NULL) {
4657 nfs_free_seqid(seqid);
4658 return ERR_PTR(-ENOMEM);
4659 }
4660
Trond Myklebust9d12b212012-01-17 22:04:25 -05004661 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004662 msg.rpc_argp = &data->arg;
4663 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004664 task_setup_data.callback_data = data;
4665 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004666}
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4669{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004670 struct inode *inode = state->inode;
4671 struct nfs4_state_owner *sp = state->owner;
4672 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004673 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004674 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004675 struct rpc_task *task;
4676 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004677 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Trond Myklebust9b073572006-06-29 16:38:34 -04004679 status = nfs4_set_lock_state(state, request);
4680 /* Unlock _before_ we do the RPC call */
4681 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004682 /* Exclude nfs_delegation_claim_locks() */
4683 mutex_lock(&sp->so_delegreturn_mutex);
4684 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004685 down_read(&nfsi->rwsem);
4686 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4687 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004688 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004689 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004690 }
4691 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004692 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004693 if (status != 0)
4694 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004695 /* Is this a delegated lock? */
4696 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004697 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004698 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004699 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004700 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004701 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004702 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004703 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004704 status = PTR_ERR(task);
4705 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004706 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004707 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004708 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004709out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004710 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004714struct nfs4_lockdata {
4715 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004716 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004717 struct nfs4_lock_state *lsp;
4718 struct nfs_open_context *ctx;
4719 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004720 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004721 int rpc_status;
4722 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004723 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004724};
4725
4726static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004727 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4728 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004729{
4730 struct nfs4_lockdata *p;
4731 struct inode *inode = lsp->ls_state->inode;
4732 struct nfs_server *server = NFS_SERVER(inode);
4733
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004734 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004735 if (p == NULL)
4736 return NULL;
4737
4738 p->arg.fh = NFS_FH(inode);
4739 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004740 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004741 if (p->arg.open_seqid == NULL)
4742 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004743 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004744 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004745 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004746 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004747 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004748 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004749 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004750 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004751 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004752 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004753 atomic_inc(&lsp->ls_count);
4754 p->ctx = get_nfs_open_context(ctx);
4755 memcpy(&p->fl, fl, sizeof(p->fl));
4756 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004757out_free_seqid:
4758 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004759out_free:
4760 kfree(p);
4761 return NULL;
4762}
4763
4764static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4765{
4766 struct nfs4_lockdata *data = calldata;
4767 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Harvey Harrison3110ff82008-05-02 13:42:44 -07004769 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004770 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004771 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772 /* Do we need to do an open_to_lock_owner? */
4773 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004774 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004775 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004776 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004777 data->arg.open_stateid = &state->stateid;
4778 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004779 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004780 } else
4781 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004782 if (!nfs4_valid_open_stateid(state)) {
4783 data->rpc_status = -EBADF;
4784 task->tk_action = NULL;
4785 goto out_release_open_seqid;
4786 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004787 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004788 if (nfs4_setup_sequence(data->server,
4789 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004790 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004791 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004792 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004793out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004794 nfs_release_seqid(data->arg.open_seqid);
4795out_release_lock_seqid:
4796 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004797out_wait:
4798 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004799 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004800}
4801
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004802static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4803{
4804 struct nfs4_lockdata *data = calldata;
4805
Harvey Harrison3110ff82008-05-02 13:42:44 -07004806 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004807
Trond Myklebust14516c32010-07-31 14:29:06 -04004808 if (!nfs4_sequence_done(task, &data->res.seq_res))
4809 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004810
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004811 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004813 if (data->rpc_status == 0)
4814 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4815 else
4816 goto out;
4817 }
4818 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004819 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004820 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004821 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004822 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004823out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004824 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004825}
4826
4827static void nfs4_lock_release(void *calldata)
4828{
4829 struct nfs4_lockdata *data = calldata;
4830
Harvey Harrison3110ff82008-05-02 13:42:44 -07004831 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004832 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004833 if (data->cancelled != 0) {
4834 struct rpc_task *task;
4835 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4836 data->arg.lock_seqid);
4837 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004838 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004839 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004840 } else
4841 nfs_free_seqid(data->arg.lock_seqid);
4842 nfs4_put_lock_state(data->lsp);
4843 put_nfs_open_context(data->ctx);
4844 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004845 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004846}
4847
4848static const struct rpc_call_ops nfs4_lock_ops = {
4849 .rpc_call_prepare = nfs4_lock_prepare,
4850 .rpc_call_done = nfs4_lock_done,
4851 .rpc_release = nfs4_lock_release,
4852};
4853
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004854static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4855{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004856 switch (error) {
4857 case -NFS4ERR_ADMIN_REVOKED:
4858 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004859 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004860 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004861 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004862 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004863 break;
4864 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004865 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004866 case -NFS4ERR_EXPIRED:
4867 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004868 };
4869}
4870
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004871static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004872{
4873 struct nfs4_lockdata *data;
4874 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004875 struct rpc_message msg = {
4876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4877 .rpc_cred = state->owner->so_cred,
4878 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004879 struct rpc_task_setup task_setup_data = {
4880 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004881 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004882 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004883 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004884 .flags = RPC_TASK_ASYNC,
4885 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004886 int ret;
4887
Harvey Harrison3110ff82008-05-02 13:42:44 -07004888 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004889 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004890 fl->fl_u.nfs4_fl.owner,
4891 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004892 if (data == NULL)
4893 return -ENOMEM;
4894 if (IS_SETLKW(cmd))
4895 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004896 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004897 msg.rpc_argp = &data->arg;
4898 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004899 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004900 if (recovery_type > NFS_LOCK_NEW) {
4901 if (recovery_type == NFS_LOCK_RECLAIM)
4902 data->arg.reclaim = NFS_LOCK_RECLAIM;
4903 nfs4_set_sequence_privileged(&data->arg.seq_args);
4904 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004905 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004906 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004907 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004908 ret = nfs4_wait_for_completion_rpc_task(task);
4909 if (ret == 0) {
4910 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004911 if (ret)
4912 nfs4_handle_setlk_error(data->server, data->lsp,
4913 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004914 } else
4915 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004916 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004917 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
4921static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4922{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004923 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004924 struct nfs4_exception exception = {
4925 .inode = state->inode,
4926 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004927 int err;
4928
4929 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004930 /* Cache the lock if possible... */
4931 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4932 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004933 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004934 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004935 break;
4936 nfs4_handle_exception(server, err, &exception);
4937 } while (exception.retry);
4938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
4940
4941static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4942{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004943 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004944 struct nfs4_exception exception = {
4945 .inode = state->inode,
4946 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004947 int err;
4948
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004949 err = nfs4_set_lock_state(state, request);
4950 if (err != 0)
4951 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004952 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4954 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004955 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004956 switch (err) {
4957 default:
4958 goto out;
4959 case -NFS4ERR_GRACE:
4960 case -NFS4ERR_DELAY:
4961 nfs4_handle_exception(server, err, &exception);
4962 err = 0;
4963 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004964 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004965out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004969#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004970/**
4971 * nfs41_check_expired_locks - possibly free a lock stateid
4972 *
4973 * @state: NFSv4 state for an inode
4974 *
4975 * Returns NFS_OK if recovery for this stateid is now finished.
4976 * Otherwise a negative NFS4ERR value is returned.
4977 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004978static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004979{
Chuck Levereb64cf92012-07-11 16:30:05 -04004980 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004981 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004982 struct nfs_server *server = NFS_SERVER(state->inode);
4983
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004984 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04004985 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004986 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4987 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004988 /* Free the stateid unless the server
4989 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04004990 if (status != -NFS4ERR_BAD_STATEID)
4991 nfs41_free_stateid(server,
4992 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004993 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004994 ret = status;
4995 }
4996 }
4997 };
4998
4999 return ret;
5000}
5001
5002static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5003{
5004 int status = NFS_OK;
5005
5006 if (test_bit(LK_STATE_IN_USE, &state->flags))
5007 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005008 if (status != NFS_OK)
5009 status = nfs4_lock_expired(state, request);
5010 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005011}
5012#endif
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5015{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005016 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005017 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005018 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005019 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005020 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005022 if ((fl_flags & FL_POSIX) &&
5023 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5024 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005025 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005026 status = nfs4_set_lock_state(state, request);
5027 if (status != 0)
5028 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005029 request->fl_flags |= FL_ACCESS;
5030 status = do_vfs_lock(request->fl_file, request);
5031 if (status < 0)
5032 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005033 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005034 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005035 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005036 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005037 request->fl_flags = fl_flags & ~FL_SLEEP;
5038 status = do_vfs_lock(request->fl_file, request);
5039 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005040 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005041 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5042 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005043 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005044 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005045 goto out;
5046 down_read(&nfsi->rwsem);
5047 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5048 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005049 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005050 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005051 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005052 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005053 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005054 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5055 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005056out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005057 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058out:
5059 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return status;
5061}
5062
5063static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5064{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005065 struct nfs4_exception exception = {
5066 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005067 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 int err;
5070
5071 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005072 err = _nfs4_proc_setlk(state, cmd, request);
5073 if (err == -NFS4ERR_DENIED)
5074 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005076 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 } while (exception.retry);
5078 return err;
5079}
5080
5081static int
5082nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5083{
5084 struct nfs_open_context *ctx;
5085 struct nfs4_state *state;
5086 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5087 int status;
5088
5089 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005090 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 state = ctx->state;
5092
5093 if (request->fl_start < 0 || request->fl_end < 0)
5094 return -EINVAL;
5095
Trond Myklebustd9531262009-07-21 19:22:38 -04005096 if (IS_GETLK(cmd)) {
5097 if (state != NULL)
5098 return nfs4_proc_getlk(state, F_GETLK, request);
5099 return 0;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5103 return -EINVAL;
5104
Trond Myklebustd9531262009-07-21 19:22:38 -04005105 if (request->fl_type == F_UNLCK) {
5106 if (state != NULL)
5107 return nfs4_proc_unlck(state, cmd, request);
5108 return 0;
5109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Trond Myklebustd9531262009-07-21 19:22:38 -04005111 if (state == NULL)
5112 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005113 /*
5114 * Don't rely on the VFS having checked the file open mode,
5115 * since it won't do this for flock() locks.
5116 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005117 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005118 case F_RDLCK:
5119 if (!(filp->f_mode & FMODE_READ))
5120 return -EBADF;
5121 break;
5122 case F_WRLCK:
5123 if (!(filp->f_mode & FMODE_WRITE))
5124 return -EBADF;
5125 }
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 do {
5128 status = nfs4_proc_setlk(state, cmd, request);
5129 if ((status != -EAGAIN) || IS_SETLK(cmd))
5130 break;
5131 timeout = nfs4_set_lock_task_retry(timeout);
5132 status = -ERESTARTSYS;
5133 if (signalled())
5134 break;
5135 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 return status;
5137}
5138
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005139int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005140{
5141 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005142 int err;
5143
5144 err = nfs4_set_lock_state(state, fl);
5145 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005146 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005147 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005148 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005149}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005150
Trond Myklebustcf470c32012-03-07 13:49:12 -05005151struct nfs_release_lockowner_data {
5152 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005153 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005154 struct nfs_release_lockowner_args args;
5155};
5156
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005157static void nfs4_release_lockowner_release(void *calldata)
5158{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005159 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005160 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005161 kfree(calldata);
5162}
5163
Trond Myklebust17280172012-03-11 13:11:00 -04005164static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005165 .rpc_release = nfs4_release_lockowner_release,
5166};
5167
Trond Myklebustcf470c32012-03-07 13:49:12 -05005168int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005169{
5170 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005171 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005172 struct rpc_message msg = {
5173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5174 };
5175
5176 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005177 return -EINVAL;
5178 data = kmalloc(sizeof(*data), GFP_NOFS);
5179 if (!data)
5180 return -ENOMEM;
5181 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005182 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005183 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5184 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5185 data->args.lock_owner.s_dev = server->s_dev;
5186 msg.rpc_argp = &data->args;
5187 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5188 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005189}
5190
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005191#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5192
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005193static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5194 const void *buf, size_t buflen,
5195 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005196{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005197 if (strcmp(key, "") != 0)
5198 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005199
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005200 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005201}
5202
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005203static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5204 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005205{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005206 if (strcmp(key, "") != 0)
5207 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005208
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005209 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005210}
5211
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005212static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5213 size_t list_len, const char *name,
5214 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005215{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005216 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005217
J. Bruce Fields096455a2006-03-20 23:23:42 -05005218 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5219 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005220
5221 if (list && len <= list_len)
5222 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005223 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005224}
5225
Andy Adamson533eb462011-06-13 18:25:56 -04005226/*
5227 * nfs_fhget will use either the mounted_on_fileid or the fileid
5228 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005229static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5230{
Andy Adamson533eb462011-06-13 18:25:56 -04005231 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5232 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5233 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005234 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005235 return;
5236
5237 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005238 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005239 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5240 fattr->nlink = 2;
5241}
5242
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005243static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5244 const struct qstr *name,
5245 struct nfs4_fs_locations *fs_locations,
5246 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005247{
5248 struct nfs_server *server = NFS_SERVER(dir);
5249 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005250 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005251 };
5252 struct nfs4_fs_locations_arg args = {
5253 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005254 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005255 .page = page,
5256 .bitmask = bitmask,
5257 };
Benny Halevy22958462009-04-01 09:22:02 -04005258 struct nfs4_fs_locations_res res = {
5259 .fs_locations = fs_locations,
5260 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005261 struct rpc_message msg = {
5262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5263 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005264 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005265 };
5266 int status;
5267
Harvey Harrison3110ff82008-05-02 13:42:44 -07005268 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005269
5270 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5271 * is not supported */
5272 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5273 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5274 else
5275 bitmask[0] |= FATTR4_WORD0_FILEID;
5276
Trond Myklebustc228fd32007-01-13 02:28:11 -05005277 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005278 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005279 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005280 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005281 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005282 return status;
5283}
5284
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005285int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5286 const struct qstr *name,
5287 struct nfs4_fs_locations *fs_locations,
5288 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005289{
5290 struct nfs4_exception exception = { };
5291 int err;
5292 do {
5293 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005294 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005295 &exception);
5296 } while (exception.retry);
5297 return err;
5298}
5299
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005300static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5301{
5302 int status;
5303 struct nfs4_secinfo_arg args = {
5304 .dir_fh = NFS_FH(dir),
5305 .name = name,
5306 };
5307 struct nfs4_secinfo_res res = {
5308 .flavors = flavors,
5309 };
5310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5312 .rpc_argp = &args,
5313 .rpc_resp = &res,
5314 };
5315
5316 dprintk("NFS call secinfo %s\n", name->name);
5317 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5318 dprintk("NFS reply secinfo: %d\n", status);
5319 return status;
5320}
5321
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005322int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5323 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005324{
5325 struct nfs4_exception exception = { };
5326 int err;
5327 do {
5328 err = nfs4_handle_exception(NFS_SERVER(dir),
5329 _nfs4_proc_secinfo(dir, name, flavors),
5330 &exception);
5331 } while (exception.retry);
5332 return err;
5333}
5334
Andy Adamson557134a2009-04-01 09:21:53 -04005335#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005336/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005337 * Check the exchange flags returned by the server for invalid flags, having
5338 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5339 * DS flags set.
5340 */
5341static int nfs4_check_cl_exchange_flags(u32 flags)
5342{
5343 if (flags & ~EXCHGID4_FLAG_MASK_R)
5344 goto out_inval;
5345 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5346 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5347 goto out_inval;
5348 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5349 goto out_inval;
5350 return NFS_OK;
5351out_inval:
5352 return -NFS4ERR_INVAL;
5353}
5354
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005355static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005356nfs41_same_server_scope(struct nfs41_server_scope *a,
5357 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005358{
5359 if (a->server_scope_sz == b->server_scope_sz &&
5360 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5361 return true;
5362
5363 return false;
5364}
5365
Andy Adamson357f54d2010-12-14 10:11:57 -05005366/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005367 * nfs4_proc_bind_conn_to_session()
5368 *
5369 * The 4.1 client currently uses the same TCP connection for the
5370 * fore and backchannel.
5371 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005372int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005373{
5374 int status;
5375 struct nfs41_bind_conn_to_session_res res;
5376 struct rpc_message msg = {
5377 .rpc_proc =
5378 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5379 .rpc_argp = clp,
5380 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005381 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005382 };
5383
5384 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005385
5386 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5387 if (unlikely(res.session == NULL)) {
5388 status = -ENOMEM;
5389 goto out;
5390 }
5391
5392 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5393 if (status == 0) {
5394 if (memcmp(res.session->sess_id.data,
5395 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5396 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5397 status = -EIO;
5398 goto out_session;
5399 }
5400 if (res.dir != NFS4_CDFS4_BOTH) {
5401 dprintk("NFS: %s: Unexpected direction from server\n",
5402 __func__);
5403 status = -EIO;
5404 goto out_session;
5405 }
5406 if (res.use_conn_in_rdma_mode) {
5407 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5408 __func__);
5409 status = -EIO;
5410 goto out_session;
5411 }
5412 }
5413out_session:
5414 kfree(res.session);
5415out:
5416 dprintk("<-- %s status= %d\n", __func__, status);
5417 return status;
5418}
5419
5420/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005421 * nfs4_proc_exchange_id()
5422 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005423 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5424 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005425 * Since the clientid has expired, all compounds using sessions
5426 * associated with the stale clientid will be returning
5427 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5428 * be in some phase of session reset.
5429 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005430int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005431{
5432 nfs4_verifier verifier;
5433 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005434 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005435 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005436 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005437 };
5438 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005439 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005440 };
5441 int status;
5442 struct rpc_message msg = {
5443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5444 .rpc_argp = &args,
5445 .rpc_resp = &res,
5446 .rpc_cred = cred,
5447 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005448
Chuck Leverf0920752012-05-21 22:45:41 -04005449 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005450 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5451 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5453 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5454 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005455
Chuck Leveracdeb692012-05-21 22:46:16 -04005456 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005457 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005458 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005459 status = -ENOMEM;
5460 goto out;
5461 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005462
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005463 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005464 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005465 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005466 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005467 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005468 }
5469
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005470 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005471 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005472 status = -ENOMEM;
5473 goto out_server_scope;
5474 }
5475
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005476 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005477 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005478 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005479
Chuck Lever177313f2012-05-21 22:44:58 -04005480 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005481 clp->cl_clientid = res.clientid;
5482 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5483 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5484 clp->cl_seqid = res.seqid;
5485
Chuck Leveracdeb692012-05-21 22:46:16 -04005486 kfree(clp->cl_serverowner);
5487 clp->cl_serverowner = res.server_owner;
5488 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005489
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005490 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005491 kfree(clp->cl_implid);
5492 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005493
Chuck Lever177313f2012-05-21 22:44:58 -04005494 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005495 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005496 res.server_scope)) {
5497 dprintk("%s: server_scope mismatch detected\n",
5498 __func__);
5499 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005500 kfree(clp->cl_serverscope);
5501 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005502 }
5503
Chuck Lever177313f2012-05-21 22:44:58 -04005504 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005505 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005506 goto out;
5507 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005508 } else
5509 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005510
Chuck Leveracdeb692012-05-21 22:46:16 -04005511out_server_owner:
5512 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005513out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005514 kfree(res.server_scope);
5515out:
Chuck Lever177313f2012-05-21 22:44:58 -04005516 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005517 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005518 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005519 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005520 clp->cl_implid->date.seconds,
5521 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005522 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005523 return status;
5524}
5525
Trond Myklebust66245532012-05-25 17:18:09 -04005526static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5527 struct rpc_cred *cred)
5528{
5529 struct rpc_message msg = {
5530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5531 .rpc_argp = clp,
5532 .rpc_cred = cred,
5533 };
5534 int status;
5535
5536 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5537 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005538 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005539 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5540 return status;
5541}
5542
5543static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5544 struct rpc_cred *cred)
5545{
5546 unsigned int loop;
5547 int ret;
5548
5549 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5550 ret = _nfs4_proc_destroy_clientid(clp, cred);
5551 switch (ret) {
5552 case -NFS4ERR_DELAY:
5553 case -NFS4ERR_CLIENTID_BUSY:
5554 ssleep(1);
5555 break;
5556 default:
5557 return ret;
5558 }
5559 }
5560 return 0;
5561}
5562
5563int nfs4_destroy_clientid(struct nfs_client *clp)
5564{
5565 struct rpc_cred *cred;
5566 int ret = 0;
5567
5568 if (clp->cl_mvops->minor_version < 1)
5569 goto out;
5570 if (clp->cl_exchange_flags == 0)
5571 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005572 if (clp->cl_preserve_clid)
5573 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005574 cred = nfs4_get_exchange_id_cred(clp);
5575 ret = nfs4_proc_destroy_clientid(clp, cred);
5576 if (cred)
5577 put_rpccred(cred);
5578 switch (ret) {
5579 case 0:
5580 case -NFS4ERR_STALE_CLIENTID:
5581 clp->cl_exchange_flags = 0;
5582 }
5583out:
5584 return ret;
5585}
5586
Andy Adamson2050f0c2009-04-01 09:22:30 -04005587struct nfs4_get_lease_time_data {
5588 struct nfs4_get_lease_time_args *args;
5589 struct nfs4_get_lease_time_res *res;
5590 struct nfs_client *clp;
5591};
5592
5593static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5594 void *calldata)
5595{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005596 struct nfs4_get_lease_time_data *data =
5597 (struct nfs4_get_lease_time_data *)calldata;
5598
5599 dprintk("--> %s\n", __func__);
5600 /* just setup sequence, do not trigger session recovery
5601 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005602 nfs41_setup_sequence(data->clp->cl_session,
5603 &data->args->la_seq_args,
5604 &data->res->lr_seq_res,
5605 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005606 dprintk("<-- %s\n", __func__);
5607}
5608
5609/*
5610 * Called from nfs4_state_manager thread for session setup, so don't recover
5611 * from sequence operation or clientid errors.
5612 */
5613static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5614{
5615 struct nfs4_get_lease_time_data *data =
5616 (struct nfs4_get_lease_time_data *)calldata;
5617
5618 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005619 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5620 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005621 switch (task->tk_status) {
5622 case -NFS4ERR_DELAY:
5623 case -NFS4ERR_GRACE:
5624 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5625 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5626 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005627 /* fall through */
5628 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005629 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005630 return;
5631 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005632 dprintk("<-- %s\n", __func__);
5633}
5634
Trond Myklebust17280172012-03-11 13:11:00 -04005635static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5637 .rpc_call_done = nfs4_get_lease_time_done,
5638};
5639
5640int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5641{
5642 struct rpc_task *task;
5643 struct nfs4_get_lease_time_args args;
5644 struct nfs4_get_lease_time_res res = {
5645 .lr_fsinfo = fsinfo,
5646 };
5647 struct nfs4_get_lease_time_data data = {
5648 .args = &args,
5649 .res = &res,
5650 .clp = clp,
5651 };
5652 struct rpc_message msg = {
5653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5654 .rpc_argp = &args,
5655 .rpc_resp = &res,
5656 };
5657 struct rpc_task_setup task_setup = {
5658 .rpc_client = clp->cl_rpcclient,
5659 .rpc_message = &msg,
5660 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005661 .callback_data = &data,
5662 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005663 };
5664 int status;
5665
Trond Myklebust9d12b212012-01-17 22:04:25 -05005666 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005667 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005668 dprintk("--> %s\n", __func__);
5669 task = rpc_run_task(&task_setup);
5670
5671 if (IS_ERR(task))
5672 status = PTR_ERR(task);
5673 else {
5674 status = task->tk_status;
5675 rpc_put_task(task);
5676 }
5677 dprintk("<-- %s return %d\n", __func__, status);
5678
5679 return status;
5680}
5681
Andy Adamsonfc931582009-04-01 09:22:31 -04005682/*
5683 * Initialize the values to be used by the client in CREATE_SESSION
5684 * If nfs4_init_session set the fore channel request and response sizes,
5685 * use them.
5686 *
5687 * Set the back channel max_resp_sz_cached to zero to force the client to
5688 * always set csa_cachethis to FALSE because the current implementation
5689 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5690 */
5691static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5692{
5693 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005694 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5695 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005696
5697 if (mxrqst_sz == 0)
5698 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5699 if (mxresp_sz == 0)
5700 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5701 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005702 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5703 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005704 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005705 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005706
5707 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005708 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005709 __func__,
5710 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005711 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005712
5713 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005714 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5715 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5716 args->bc_attrs.max_resp_sz_cached = 0;
5717 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5718 args->bc_attrs.max_reqs = 1;
5719
5720 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5721 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5722 __func__,
5723 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5724 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5725 args->bc_attrs.max_reqs);
5726}
5727
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005728static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005729{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005730 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5731 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5732
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005733 if (rcvd->max_resp_sz > sent->max_resp_sz)
5734 return -EINVAL;
5735 /*
5736 * Our requested max_ops is the minimum we need; we're not
5737 * prepared to break up compounds into smaller pieces than that.
5738 * So, no point even trying to continue if the server won't
5739 * cooperate:
5740 */
5741 if (rcvd->max_ops < sent->max_ops)
5742 return -EINVAL;
5743 if (rcvd->max_reqs == 0)
5744 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005745 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5746 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005747 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005748}
5749
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005750static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5751{
5752 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5753 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005754
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005755 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5756 return -EINVAL;
5757 if (rcvd->max_resp_sz < sent->max_resp_sz)
5758 return -EINVAL;
5759 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5760 return -EINVAL;
5761 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005762 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005763 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005764 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005765 return -EINVAL;
5766 return 0;
5767}
Andy Adamson8d353012009-04-01 09:22:32 -04005768
Andy Adamson8d353012009-04-01 09:22:32 -04005769static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5770 struct nfs4_session *session)
5771{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005772 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005773
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774 ret = nfs4_verify_fore_channel_attrs(args, session);
5775 if (ret)
5776 return ret;
5777 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005778}
5779
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005780static int _nfs4_proc_create_session(struct nfs_client *clp,
5781 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005782{
5783 struct nfs4_session *session = clp->cl_session;
5784 struct nfs41_create_session_args args = {
5785 .client = clp,
5786 .cb_program = NFS4_CALLBACK,
5787 };
5788 struct nfs41_create_session_res res = {
5789 .client = clp,
5790 };
5791 struct rpc_message msg = {
5792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5793 .rpc_argp = &args,
5794 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005795 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005796 };
5797 int status;
5798
5799 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005800 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005801
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005802 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005803
Trond Myklebust43095d32012-11-20 11:13:12 -05005804 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005805 /* Verify the session's negotiated channel_attrs values */
5806 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005807 /* Increment the clientid slot sequence id */
5808 clp->cl_seqid++;
5809 }
5810
5811 return status;
5812}
5813
5814/*
5815 * Issues a CREATE_SESSION operation to the server.
5816 * It is the responsibility of the caller to verify the session is
5817 * expired before calling this routine.
5818 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005819int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005820{
5821 int status;
5822 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005823 struct nfs4_session *session = clp->cl_session;
5824
5825 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5826
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005827 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005828 if (status)
5829 goto out;
5830
Andy Adamsonaacd5532011-11-09 13:58:21 -05005831 /* Init or reset the session slot tables */
5832 status = nfs4_setup_session_slot_tables(session);
5833 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005834 if (status)
5835 goto out;
5836
5837 ptr = (unsigned *)&session->sess_id.data[0];
5838 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5839 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005840out:
5841 dprintk("<-- %s\n", __func__);
5842 return status;
5843}
5844
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005845/*
5846 * Issue the over-the-wire RPC DESTROY_SESSION.
5847 * The caller must serialize access to this routine.
5848 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005849int nfs4_proc_destroy_session(struct nfs4_session *session,
5850 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005851{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005852 struct rpc_message msg = {
5853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5854 .rpc_argp = session,
5855 .rpc_cred = cred,
5856 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005857 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005858
5859 dprintk("--> nfs4_proc_destroy_session\n");
5860
5861 /* session is still being setup */
5862 if (session->clp->cl_cons_state != NFS_CS_READY)
5863 return status;
5864
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005865 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005866
5867 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005868 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005869 "Session has been destroyed regardless...\n", status);
5870
5871 dprintk("<-- nfs4_proc_destroy_session\n");
5872 return status;
5873}
5874
Trond Myklebust7b38c362012-05-23 13:23:31 -04005875/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005876 * Renew the cl_session lease.
5877 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005878struct nfs4_sequence_data {
5879 struct nfs_client *clp;
5880 struct nfs4_sequence_args args;
5881 struct nfs4_sequence_res res;
5882};
5883
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005884static void nfs41_sequence_release(void *data)
5885{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005886 struct nfs4_sequence_data *calldata = data;
5887 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005888
Alexandros Batsakis71358402010-02-05 03:45:05 -08005889 if (atomic_read(&clp->cl_count) > 1)
5890 nfs4_schedule_state_renewal(clp);
5891 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005892 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005893}
5894
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005895static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5896{
5897 switch(task->tk_status) {
5898 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005899 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5900 return -EAGAIN;
5901 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005902 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005903 }
5904 return 0;
5905}
5906
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005907static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005908{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005909 struct nfs4_sequence_data *calldata = data;
5910 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005911
Trond Myklebust14516c32010-07-31 14:29:06 -04005912 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5913 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005914
5915 if (task->tk_status < 0) {
5916 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005917 if (atomic_read(&clp->cl_count) == 1)
5918 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005919
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005920 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5921 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005922 return;
5923 }
5924 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005925 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005926out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005927 dprintk("<-- %s\n", __func__);
5928}
5929
5930static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5931{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005932 struct nfs4_sequence_data *calldata = data;
5933 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005934 struct nfs4_sequence_args *args;
5935 struct nfs4_sequence_res *res;
5936
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005937 args = task->tk_msg.rpc_argp;
5938 res = task->tk_msg.rpc_resp;
5939
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005940 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005941}
5942
5943static const struct rpc_call_ops nfs41_sequence_ops = {
5944 .rpc_call_done = nfs41_sequence_call_done,
5945 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005946 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005947};
5948
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005949static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5950 struct rpc_cred *cred,
5951 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005952{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005953 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954 struct rpc_message msg = {
5955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5956 .rpc_cred = cred,
5957 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005958 struct rpc_task_setup task_setup_data = {
5959 .rpc_client = clp->cl_rpcclient,
5960 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005961 .callback_ops = &nfs41_sequence_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005962 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5963 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005964
Alexandros Batsakis71358402010-02-05 03:45:05 -08005965 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005966 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005967 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005968 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005969 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005970 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005971 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005972 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005973 if (is_privileged)
5974 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005975 msg.rpc_argp = &calldata->args;
5976 msg.rpc_resp = &calldata->res;
5977 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005978 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005979
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005980 return rpc_run_task(&task_setup_data);
5981}
5982
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005983static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005984{
5985 struct rpc_task *task;
5986 int ret = 0;
5987
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005988 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5989 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005990 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005991 if (IS_ERR(task))
5992 ret = PTR_ERR(task);
5993 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005994 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005995 dprintk("<-- %s status=%d\n", __func__, ret);
5996 return ret;
5997}
5998
5999static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6000{
6001 struct rpc_task *task;
6002 int ret;
6003
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006004 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006005 if (IS_ERR(task)) {
6006 ret = PTR_ERR(task);
6007 goto out;
6008 }
6009 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006010 if (!ret) {
6011 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6012
6013 if (task->tk_status == 0)
6014 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006015 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006016 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006017 rpc_put_task(task);
6018out:
6019 dprintk("<-- %s status=%d\n", __func__, ret);
6020 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006021}
6022
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006023struct nfs4_reclaim_complete_data {
6024 struct nfs_client *clp;
6025 struct nfs41_reclaim_complete_args arg;
6026 struct nfs41_reclaim_complete_res res;
6027};
6028
6029static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6030{
6031 struct nfs4_reclaim_complete_data *calldata = data;
6032
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006033 nfs41_setup_sequence(calldata->clp->cl_session,
6034 &calldata->arg.seq_args,
6035 &calldata->res.seq_res,
6036 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006037}
6038
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006039static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6040{
6041 switch(task->tk_status) {
6042 case 0:
6043 case -NFS4ERR_COMPLETE_ALREADY:
6044 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6045 break;
6046 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006047 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006048 /* fall through */
6049 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006050 return -EAGAIN;
6051 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006052 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006053 }
6054 return 0;
6055}
6056
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006057static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6058{
6059 struct nfs4_reclaim_complete_data *calldata = data;
6060 struct nfs_client *clp = calldata->clp;
6061 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6062
6063 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006064 if (!nfs41_sequence_done(task, res))
6065 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006066
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006067 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6068 rpc_restart_call_prepare(task);
6069 return;
6070 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006071 dprintk("<-- %s\n", __func__);
6072}
6073
6074static void nfs4_free_reclaim_complete_data(void *data)
6075{
6076 struct nfs4_reclaim_complete_data *calldata = data;
6077
6078 kfree(calldata);
6079}
6080
6081static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6082 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6083 .rpc_call_done = nfs4_reclaim_complete_done,
6084 .rpc_release = nfs4_free_reclaim_complete_data,
6085};
6086
6087/*
6088 * Issue a global reclaim complete.
6089 */
6090static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6091{
6092 struct nfs4_reclaim_complete_data *calldata;
6093 struct rpc_task *task;
6094 struct rpc_message msg = {
6095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6096 };
6097 struct rpc_task_setup task_setup_data = {
6098 .rpc_client = clp->cl_rpcclient,
6099 .rpc_message = &msg,
6100 .callback_ops = &nfs4_reclaim_complete_call_ops,
6101 .flags = RPC_TASK_ASYNC,
6102 };
6103 int status = -ENOMEM;
6104
6105 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006106 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006107 if (calldata == NULL)
6108 goto out;
6109 calldata->clp = clp;
6110 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006111
Trond Myklebust9d12b212012-01-17 22:04:25 -05006112 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006113 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006114 msg.rpc_argp = &calldata->arg;
6115 msg.rpc_resp = &calldata->res;
6116 task_setup_data.callback_data = calldata;
6117 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006118 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006119 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006120 goto out;
6121 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006122 status = nfs4_wait_for_completion_rpc_task(task);
6123 if (status == 0)
6124 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006125 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006126 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006127out:
6128 dprintk("<-- %s status=%d\n", __func__, status);
6129 return status;
6130}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006131
6132static void
6133nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6134{
6135 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006136 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006137 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006138
6139 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006140 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6141 * right now covering the LAYOUTGET we are about to send.
6142 * However, that is not so catastrophic, and there seems
6143 * to be no way to prevent it completely.
6144 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006145 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006146 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006147 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006148 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6149 NFS_I(lgp->args.inode)->layout,
6150 lgp->args.ctx->state)) {
6151 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006152 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006153}
6154
6155static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6156{
6157 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006158 struct inode *inode = lgp->args.inode;
6159 struct nfs_server *server = NFS_SERVER(inode);
6160 struct pnfs_layout_hdr *lo;
6161 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006162 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006163
6164 dprintk("--> %s\n", __func__);
6165
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006166 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006167 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006168
6169 switch (task->tk_status) {
6170 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006171 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172 case -NFS4ERR_LAYOUTTRYLATER:
6173 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006174 timeo = rpc_get_timeout(task->tk_client);
6175 giveup = lgp->args.timestamp + timeo;
6176 if (time_after(giveup, jiffies))
6177 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006178 break;
6179 case -NFS4ERR_EXPIRED:
6180 case -NFS4ERR_BAD_STATEID:
6181 spin_lock(&inode->i_lock);
6182 lo = NFS_I(inode)->layout;
6183 if (!lo || list_empty(&lo->plh_segs)) {
6184 spin_unlock(&inode->i_lock);
6185 /* If the open stateid was bad, then recover it. */
6186 state = lgp->args.ctx->state;
6187 } else {
6188 LIST_HEAD(head);
6189
6190 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6191 spin_unlock(&inode->i_lock);
6192 /* Mark the bad layout state as invalid, then
6193 * retry using the open stateid. */
6194 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006195 }
6196 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006197 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6198 rpc_restart_call_prepare(task);
6199out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006200 dprintk("<-- %s\n", __func__);
6201}
6202
Idan Kedar85541162012-08-02 11:47:10 +03006203static size_t max_response_pages(struct nfs_server *server)
6204{
6205 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6206 return nfs_page_array_len(0, max_resp_sz);
6207}
6208
6209static void nfs4_free_pages(struct page **pages, size_t size)
6210{
6211 int i;
6212
6213 if (!pages)
6214 return;
6215
6216 for (i = 0; i < size; i++) {
6217 if (!pages[i])
6218 break;
6219 __free_page(pages[i]);
6220 }
6221 kfree(pages);
6222}
6223
6224static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6225{
6226 struct page **pages;
6227 int i;
6228
6229 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6230 if (!pages) {
6231 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6232 return NULL;
6233 }
6234
6235 for (i = 0; i < size; i++) {
6236 pages[i] = alloc_page(gfp_flags);
6237 if (!pages[i]) {
6238 dprintk("%s: failed to allocate page\n", __func__);
6239 nfs4_free_pages(pages, size);
6240 return NULL;
6241 }
6242 }
6243
6244 return pages;
6245}
6246
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006247static void nfs4_layoutget_release(void *calldata)
6248{
6249 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006250 struct inode *inode = lgp->args.inode;
6251 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006252 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006253
6254 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006255 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006256 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006257 put_nfs_open_context(lgp->args.ctx);
6258 kfree(calldata);
6259 dprintk("<-- %s\n", __func__);
6260}
6261
6262static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6263 .rpc_call_prepare = nfs4_layoutget_prepare,
6264 .rpc_call_done = nfs4_layoutget_done,
6265 .rpc_release = nfs4_layoutget_release,
6266};
6267
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006268struct pnfs_layout_segment *
6269nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006270{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006271 struct inode *inode = lgp->args.inode;
6272 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006273 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006274 struct rpc_task *task;
6275 struct rpc_message msg = {
6276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6277 .rpc_argp = &lgp->args,
6278 .rpc_resp = &lgp->res,
6279 };
6280 struct rpc_task_setup task_setup_data = {
6281 .rpc_client = server->client,
6282 .rpc_message = &msg,
6283 .callback_ops = &nfs4_layoutget_call_ops,
6284 .callback_data = lgp,
6285 .flags = RPC_TASK_ASYNC,
6286 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006287 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006288 int status = 0;
6289
6290 dprintk("--> %s\n", __func__);
6291
Idan Kedar85541162012-08-02 11:47:10 +03006292 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6293 if (!lgp->args.layout.pages) {
6294 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006295 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006296 }
6297 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006298 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006299
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006300 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006301 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006302 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006303
6304 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6305 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6306
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006307 task = rpc_run_task(&task_setup_data);
6308 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006309 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006310 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006311 if (status == 0)
6312 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006313 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6314 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006315 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006316 rpc_put_task(task);
6317 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006318 if (status)
6319 return ERR_PTR(status);
6320 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006321}
6322
Benny Halevycbe82602011-05-22 19:52:37 +03006323static void
6324nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6325{
6326 struct nfs4_layoutreturn *lrp = calldata;
6327
6328 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006329 nfs41_setup_sequence(lrp->clp->cl_session,
6330 &lrp->args.seq_args,
6331 &lrp->res.seq_res,
6332 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006333}
6334
6335static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6336{
6337 struct nfs4_layoutreturn *lrp = calldata;
6338 struct nfs_server *server;
6339
6340 dprintk("--> %s\n", __func__);
6341
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006342 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006343 return;
6344
6345 server = NFS_SERVER(lrp->args.inode);
6346 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006347 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006348 return;
6349 }
Benny Halevycbe82602011-05-22 19:52:37 +03006350 dprintk("<-- %s\n", __func__);
6351}
6352
6353static void nfs4_layoutreturn_release(void *calldata)
6354{
6355 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006356 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006357
6358 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006359 spin_lock(&lo->plh_inode->i_lock);
6360 if (lrp->res.lrs_present)
6361 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6362 lo->plh_block_lgets--;
6363 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006364 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006365 kfree(calldata);
6366 dprintk("<-- %s\n", __func__);
6367}
6368
6369static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6370 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6371 .rpc_call_done = nfs4_layoutreturn_done,
6372 .rpc_release = nfs4_layoutreturn_release,
6373};
6374
6375int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6376{
6377 struct rpc_task *task;
6378 struct rpc_message msg = {
6379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6380 .rpc_argp = &lrp->args,
6381 .rpc_resp = &lrp->res,
6382 };
6383 struct rpc_task_setup task_setup_data = {
6384 .rpc_client = lrp->clp->cl_rpcclient,
6385 .rpc_message = &msg,
6386 .callback_ops = &nfs4_layoutreturn_call_ops,
6387 .callback_data = lrp,
6388 };
6389 int status;
6390
6391 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006392 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006393 task = rpc_run_task(&task_setup_data);
6394 if (IS_ERR(task))
6395 return PTR_ERR(task);
6396 status = task->tk_status;
6397 dprintk("<-- %s status=%d\n", __func__, status);
6398 rpc_put_task(task);
6399 return status;
6400}
6401
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006402/*
6403 * Retrieve the list of Data Server devices from the MDS.
6404 */
6405static int _nfs4_getdevicelist(struct nfs_server *server,
6406 const struct nfs_fh *fh,
6407 struct pnfs_devicelist *devlist)
6408{
6409 struct nfs4_getdevicelist_args args = {
6410 .fh = fh,
6411 .layoutclass = server->pnfs_curr_ld->id,
6412 };
6413 struct nfs4_getdevicelist_res res = {
6414 .devlist = devlist,
6415 };
6416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6418 .rpc_argp = &args,
6419 .rpc_resp = &res,
6420 };
6421 int status;
6422
6423 dprintk("--> %s\n", __func__);
6424 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6425 &res.seq_res, 0);
6426 dprintk("<-- %s status=%d\n", __func__, status);
6427 return status;
6428}
6429
6430int nfs4_proc_getdevicelist(struct nfs_server *server,
6431 const struct nfs_fh *fh,
6432 struct pnfs_devicelist *devlist)
6433{
6434 struct nfs4_exception exception = { };
6435 int err;
6436
6437 do {
6438 err = nfs4_handle_exception(server,
6439 _nfs4_getdevicelist(server, fh, devlist),
6440 &exception);
6441 } while (exception.retry);
6442
6443 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6444 err, devlist->num_devs);
6445
6446 return err;
6447}
6448EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6449
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006450static int
6451_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6452{
6453 struct nfs4_getdeviceinfo_args args = {
6454 .pdev = pdev,
6455 };
6456 struct nfs4_getdeviceinfo_res res = {
6457 .pdev = pdev,
6458 };
6459 struct rpc_message msg = {
6460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6461 .rpc_argp = &args,
6462 .rpc_resp = &res,
6463 };
6464 int status;
6465
6466 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006467 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006468 dprintk("<-- %s status=%d\n", __func__, status);
6469
6470 return status;
6471}
6472
6473int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6474{
6475 struct nfs4_exception exception = { };
6476 int err;
6477
6478 do {
6479 err = nfs4_handle_exception(server,
6480 _nfs4_proc_getdeviceinfo(server, pdev),
6481 &exception);
6482 } while (exception.retry);
6483 return err;
6484}
6485EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6486
Andy Adamson863a3c62011-03-23 13:27:54 +00006487static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6488{
6489 struct nfs4_layoutcommit_data *data = calldata;
6490 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006491 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006492
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006493 nfs41_setup_sequence(session,
6494 &data->args.seq_args,
6495 &data->res.seq_res,
6496 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006497}
6498
6499static void
6500nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6501{
6502 struct nfs4_layoutcommit_data *data = calldata;
6503 struct nfs_server *server = NFS_SERVER(data->args.inode);
6504
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006505 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006506 return;
6507
6508 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006509 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6510 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6511 case -NFS4ERR_BADLAYOUT: /* no layout */
6512 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006513 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006514 break;
6515 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006516 nfs_post_op_update_inode_force_wcc(data->args.inode,
6517 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006518 break;
6519 default:
6520 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6521 rpc_restart_call_prepare(task);
6522 return;
6523 }
6524 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006525}
6526
6527static void nfs4_layoutcommit_release(void *calldata)
6528{
6529 struct nfs4_layoutcommit_data *data = calldata;
6530
Andy Adamsondb29c082011-07-30 20:52:38 -04006531 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006532 put_rpccred(data->cred);
6533 kfree(data);
6534}
6535
6536static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6537 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6538 .rpc_call_done = nfs4_layoutcommit_done,
6539 .rpc_release = nfs4_layoutcommit_release,
6540};
6541
6542int
Andy Adamsonef311532011-03-12 02:58:10 -05006543nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006544{
6545 struct rpc_message msg = {
6546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6547 .rpc_argp = &data->args,
6548 .rpc_resp = &data->res,
6549 .rpc_cred = data->cred,
6550 };
6551 struct rpc_task_setup task_setup_data = {
6552 .task = &data->task,
6553 .rpc_client = NFS_CLIENT(data->args.inode),
6554 .rpc_message = &msg,
6555 .callback_ops = &nfs4_layoutcommit_ops,
6556 .callback_data = data,
6557 .flags = RPC_TASK_ASYNC,
6558 };
6559 struct rpc_task *task;
6560 int status = 0;
6561
6562 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6563 "lbw: %llu inode %lu\n",
6564 data->task.tk_pid, sync,
6565 data->args.lastbytewritten,
6566 data->args.inode->i_ino);
6567
Trond Myklebust9d12b212012-01-17 22:04:25 -05006568 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006569 task = rpc_run_task(&task_setup_data);
6570 if (IS_ERR(task))
6571 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006572 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006573 goto out;
6574 status = nfs4_wait_for_completion_rpc_task(task);
6575 if (status != 0)
6576 goto out;
6577 status = task->tk_status;
6578out:
6579 dprintk("%s: status %d\n", __func__, status);
6580 rpc_put_task(task);
6581 return status;
6582}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006583
6584static int
6585_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6586 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6587{
6588 struct nfs41_secinfo_no_name_args args = {
6589 .style = SECINFO_STYLE_CURRENT_FH,
6590 };
6591 struct nfs4_secinfo_res res = {
6592 .flavors = flavors,
6593 };
6594 struct rpc_message msg = {
6595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6596 .rpc_argp = &args,
6597 .rpc_resp = &res,
6598 };
6599 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6600}
6601
6602static int
6603nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6604 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6605{
6606 struct nfs4_exception exception = { };
6607 int err;
6608 do {
6609 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6610 switch (err) {
6611 case 0:
6612 case -NFS4ERR_WRONGSEC:
6613 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006614 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006615 default:
6616 err = nfs4_handle_exception(server, err, &exception);
6617 }
6618 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006619out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006620 return err;
6621}
6622
6623static int
6624nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6625 struct nfs_fsinfo *info)
6626{
6627 int err;
6628 struct page *page;
6629 rpc_authflavor_t flavor;
6630 struct nfs4_secinfo_flavors *flavors;
6631
6632 page = alloc_page(GFP_KERNEL);
6633 if (!page) {
6634 err = -ENOMEM;
6635 goto out;
6636 }
6637
6638 flavors = page_address(page);
6639 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6640
6641 /*
6642 * Fall back on "guess and check" method if
6643 * the server doesn't support SECINFO_NO_NAME
6644 */
6645 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6646 err = nfs4_find_root_sec(server, fhandle, info);
6647 goto out_freepage;
6648 }
6649 if (err)
6650 goto out_freepage;
6651
6652 flavor = nfs_find_best_sec(flavors);
6653 if (err == 0)
6654 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6655
6656out_freepage:
6657 put_page(page);
6658 if (err == -EACCES)
6659 return -EPERM;
6660out:
6661 return err;
6662}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006663
6664static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006665{
6666 int status;
6667 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006668 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006669 };
6670 struct nfs41_test_stateid_res res;
6671 struct rpc_message msg = {
6672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6673 .rpc_argp = &args,
6674 .rpc_resp = &res,
6675 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006676
Chuck Lever38527b12012-07-11 16:30:23 -04006677 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006678 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006679 nfs4_set_sequence_privileged(&args.seq_args);
6680 status = nfs4_call_sync_sequence(server->client, server, &msg,
6681 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006682 if (status != NFS_OK) {
6683 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006684 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006685 }
6686 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006687 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006688}
6689
Chuck Lever38527b12012-07-11 16:30:23 -04006690/**
6691 * nfs41_test_stateid - perform a TEST_STATEID operation
6692 *
6693 * @server: server / transport on which to perform the operation
6694 * @stateid: state ID to test
6695 *
6696 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6697 * Otherwise a negative NFS4ERR value is returned if the operation
6698 * failed or the state ID is not currently valid.
6699 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006700static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006701{
6702 struct nfs4_exception exception = { };
6703 int err;
6704 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006705 err = _nfs41_test_stateid(server, stateid);
6706 if (err != -NFS4ERR_DELAY)
6707 break;
6708 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006709 } while (exception.retry);
6710 return err;
6711}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006712
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006713static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006714{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006715 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006716 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006717 };
6718 struct nfs41_free_stateid_res res;
6719 struct rpc_message msg = {
6720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6721 .rpc_argp = &args,
6722 .rpc_resp = &res,
6723 };
Chuck Lever38527b12012-07-11 16:30:23 -04006724 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006725
Chuck Lever38527b12012-07-11 16:30:23 -04006726 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006727 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006728 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006729 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006730 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006731 dprintk("NFS reply free_stateid: %d\n", status);
6732 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006733}
6734
Chuck Lever38527b12012-07-11 16:30:23 -04006735/**
6736 * nfs41_free_stateid - perform a FREE_STATEID operation
6737 *
6738 * @server: server / transport on which to perform the operation
6739 * @stateid: state ID to release
6740 *
6741 * Returns NFS_OK if the server freed "stateid". Otherwise a
6742 * negative NFS4ERR value is returned.
6743 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006744static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006745{
6746 struct nfs4_exception exception = { };
6747 int err;
6748 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006749 err = _nfs4_free_stateid(server, stateid);
6750 if (err != -NFS4ERR_DELAY)
6751 break;
6752 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006753 } while (exception.retry);
6754 return err;
6755}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006756
6757static bool nfs41_match_stateid(const nfs4_stateid *s1,
6758 const nfs4_stateid *s2)
6759{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006760 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006761 return false;
6762
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006763 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006764 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006765 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006766 return true;
6767
6768 return false;
6769}
6770
Andy Adamson557134a2009-04-01 09:21:53 -04006771#endif /* CONFIG_NFS_V4_1 */
6772
Trond Myklebust36281ca2012-03-04 18:13:56 -05006773static bool nfs4_match_stateid(const nfs4_stateid *s1,
6774 const nfs4_stateid *s2)
6775{
Trond Myklebustf597c532012-03-04 18:13:56 -05006776 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006777}
6778
6779
Trond Myklebust17280172012-03-11 13:11:00 -04006780static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006781 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006782 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 .recover_open = nfs4_open_reclaim,
6784 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006785 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006786 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006787 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788};
6789
Andy Adamson591d71c2009-04-01 09:22:47 -04006790#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006791static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006792 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6793 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6794 .recover_open = nfs4_open_reclaim,
6795 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006796 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006797 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006798 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006799 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006800};
6801#endif /* CONFIG_NFS_V4_1 */
6802
Trond Myklebust17280172012-03-11 13:11:00 -04006803static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006804 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006805 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 .recover_open = nfs4_open_expired,
6807 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006808 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006809 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810};
6811
Andy Adamson591d71c2009-04-01 09:22:47 -04006812#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006813static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006814 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6815 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006816 .recover_open = nfs41_open_expired,
6817 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006818 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006819 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006820};
6821#endif /* CONFIG_NFS_V4_1 */
6822
Trond Myklebust17280172012-03-11 13:11:00 -04006823static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006824 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006825 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006826 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006827};
6828
6829#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006830static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006831 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006832 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006833 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006834};
6835#endif
6836
Trond Myklebust97dc1352010-06-16 09:52:26 -04006837static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6838 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006839 .init_caps = NFS_CAP_READDIRPLUS
6840 | NFS_CAP_ATOMIC_OPEN
6841 | NFS_CAP_CHANGE_ATTR
6842 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006843 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006844 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006845 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006846 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6847 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6848 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006849};
6850
6851#if defined(CONFIG_NFS_V4_1)
6852static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6853 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006854 .init_caps = NFS_CAP_READDIRPLUS
6855 | NFS_CAP_ATOMIC_OPEN
6856 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006857 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006858 | NFS_CAP_STATEID_NFSV41
6859 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006860 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006861 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006862 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006863 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6864 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6865 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006866};
6867#endif
6868
Trond Myklebust97dc1352010-06-16 09:52:26 -04006869const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6870 [0] = &nfs_v4_0_minor_ops,
6871#if defined(CONFIG_NFS_V4_1)
6872 [1] = &nfs_v4_1_minor_ops,
6873#endif
6874};
6875
Bryan Schumaker73a79702012-07-16 16:39:12 -04006876const struct inode_operations nfs4_dir_inode_operations = {
6877 .create = nfs_create,
6878 .lookup = nfs_lookup,
6879 .atomic_open = nfs_atomic_open,
6880 .link = nfs_link,
6881 .unlink = nfs_unlink,
6882 .symlink = nfs_symlink,
6883 .mkdir = nfs_mkdir,
6884 .rmdir = nfs_rmdir,
6885 .mknod = nfs_mknod,
6886 .rename = nfs_rename,
6887 .permission = nfs_permission,
6888 .getattr = nfs_getattr,
6889 .setattr = nfs_setattr,
6890 .getxattr = generic_getxattr,
6891 .setxattr = generic_setxattr,
6892 .listxattr = generic_listxattr,
6893 .removexattr = generic_removexattr,
6894};
6895
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006896static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006897 .permission = nfs_permission,
6898 .getattr = nfs_getattr,
6899 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006900 .getxattr = generic_getxattr,
6901 .setxattr = generic_setxattr,
6902 .listxattr = generic_listxattr,
6903 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006904};
6905
David Howells509de812006-08-22 20:06:11 -04006906const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 .version = 4, /* protocol version */
6908 .dentry_ops = &nfs4_dentry_operations,
6909 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006910 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006911 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006913 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006914 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 .getattr = nfs4_proc_getattr,
6916 .setattr = nfs4_proc_setattr,
6917 .lookup = nfs4_proc_lookup,
6918 .access = nfs4_proc_access,
6919 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 .create = nfs4_proc_create,
6921 .remove = nfs4_proc_remove,
6922 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006923 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 .unlink_done = nfs4_proc_unlink_done,
6925 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006926 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006927 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006928 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 .link = nfs4_proc_link,
6930 .symlink = nfs4_proc_symlink,
6931 .mkdir = nfs4_proc_mkdir,
6932 .rmdir = nfs4_proc_remove,
6933 .readdir = nfs4_proc_readdir,
6934 .mknod = nfs4_proc_mknod,
6935 .statfs = nfs4_proc_statfs,
6936 .fsinfo = nfs4_proc_fsinfo,
6937 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006938 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 .decode_dirent = nfs4_decode_dirent,
6940 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006941 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006942 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006943 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006945 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006946 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006947 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006949 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006950 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006952 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006953 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006954 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006955 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006956 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006957 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006958 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006959 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006960 .create_server = nfs4_create_server,
6961 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962};
6963
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006964static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6965 .prefix = XATTR_NAME_NFSV4_ACL,
6966 .list = nfs4_xattr_list_nfs4_acl,
6967 .get = nfs4_xattr_get_nfs4_acl,
6968 .set = nfs4_xattr_set_nfs4_acl,
6969};
6970
6971const struct xattr_handler *nfs4_xattr_handlers[] = {
6972 &nfs4_xattr_nfs4_acl_handler,
6973 NULL
6974};
6975
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976/*
6977 * Local variables:
6978 * c-basic-offset: 8
6979 * End:
6980 */