blob: e18b3b46c001a950c4b80c3f0038df58c02168a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040081static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040085#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050086static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050090static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Trond Myklebust52567b02009-10-23 14:46:42 -040092 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -050096 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400108 case -NFS4ERR_ACCESS:
109 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400112 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700114 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400117 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * This is our standard bitmap for GETATTR requests.
122 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400123const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138};
139
Trond Myklebust1549210f2012-06-05 09:16:47 -0400140static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
156};
157
Andy Adamsone23008e2012-10-02 21:07:32 -0400158static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
162};
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
171};
172
Trond Myklebust4ce79712005-06-22 17:16:21 +0000173const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
176 0
177};
178
Fred Isamandae100c2011-07-30 20:52:37 -0400179const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700183 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Manoj Naik830b8e32006-06-09 09:34:25 -0400188const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
205};
206
Al Virobc4785c2006-10-19 23:28:51 -0700207static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct nfs4_readdir_arg *readdir)
209{
Al Viro0dbb4c62006-10-19 23:28:49 -0700210 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000213 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
216 }
217
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
222
223 /*
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
229 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800230 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800258 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Trond Myklebust65de8722008-12-23 15:21:44 -0500261static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
262{
263 int res = 0;
264
265 might_sleep();
266
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100271 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
276}
277
278/* This is the error handling routine for processes that are allowed
279 * to sleep.
280 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800281static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500282{
283 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500284 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500285 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 int ret = errorcode;
287
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500292 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400294 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500295 exception->retry = 1;
296 return 0;
297 }
298 if (state == NULL)
299 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500303 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500304 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400307 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
308 nfs_remove_bad_delegation(inode);
309 exception->retry = 1;
310 break;
311 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500312 if (state == NULL)
313 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400314 ret = nfs4_schedule_stateid_recovery(server, state);
315 if (ret < 0)
316 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500317 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400318 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400319 if (state != NULL) {
320 ret = nfs4_schedule_stateid_recovery(server, state);
321 if (ret < 0)
322 break;
323 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500325 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500326 nfs4_schedule_lease_recovery(clp);
327 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500328#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400329 case -NFS4ERR_BADSESSION:
330 case -NFS4ERR_BADSLOT:
331 case -NFS4ERR_BAD_HIGH_SLOT:
332 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
333 case -NFS4ERR_DEADSESSION:
334 case -NFS4ERR_SEQ_FALSE_RETRY:
335 case -NFS4ERR_SEQ_MISORDERED:
336 dprintk("%s ERROR: %d Reset session\n", __func__,
337 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400338 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400339 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500340#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500342 if (exception->timeout > HZ) {
343 /* We have retried a decent amount, time to
344 * fail
345 */
346 ret = -EBUSY;
347 break;
348 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500349 case -NFS4ERR_GRACE:
350 case -NFS4ERR_DELAY:
351 ret = nfs4_delay(server->client, &exception->timeout);
352 if (ret != 0)
353 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400354 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 case -NFS4ERR_OLD_STATEID:
356 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800357 break;
358 case -NFS4ERR_BADOWNER:
359 /* The following works around a Linux server bug! */
360 case -NFS4ERR_BADNAME:
361 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
362 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
363 exception->retry = 1;
364 printk(KERN_WARNING "NFS: v4 server %s "
365 "does not accept raw "
366 "uid/gids. "
367 "Reenabling the idmapper.\n",
368 server->nfs_client->cl_hostname);
369 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500370 }
371 /* We failed to handle the error */
372 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500373wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500374 ret = nfs4_wait_clnt_recover(clp);
375 if (ret == 0)
376 exception->retry = 1;
377 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378}
379
380
Trond Myklebust452e9352010-07-31 14:29:06 -0400381static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_lock(&clp->cl_lock);
384 if (time_before(clp->cl_last_renewal,timestamp))
385 clp->cl_last_renewal = timestamp;
386 spin_unlock(&clp->cl_lock);
387}
388
Trond Myklebust452e9352010-07-31 14:29:06 -0400389static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
390{
391 do_renew_lease(server->nfs_client, timestamp);
392}
393
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400394#if defined(CONFIG_NFS_V4_1)
395
Trond Myklebustd185a332010-06-16 09:52:25 -0400396static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400397{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500398 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400399 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500400 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400401
Benny Halevydfb4f3092010-09-24 09:17:01 -0400402 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400403 /* just wake up the next guy waiting since
404 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500405 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500406 return;
Andy Adamson13615872009-04-01 09:22:17 -0400407 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500408 tbl = res->sr_slot->table;
409 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500410
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500411 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500412 /* Be nice to the server: try to ensure that the last transmitted
413 * value for highest_user_slotid <= target_highest_slotid
414 */
415 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
416 send_new_highest_used_slotid = true;
417
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500418 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
419 send_new_highest_used_slotid = false;
420 goto out_unlock;
421 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500422 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500423
424 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
425 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500426out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400428 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500429 if (send_new_highest_used_slotid)
430 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400431}
432
Trond Myklebust14516c32010-07-31 14:29:06 -0400433static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500435 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500436 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500438 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500439 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440
Bryan Schumaker468f8612011-04-18 15:57:32 -0400441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Trond Myklebust933602e2012-11-16 12:12:38 -0500445 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500446 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500447
Trond Myklebustac20d162012-12-15 15:36:07 -0500448 if (slot->interrupted) {
449 slot->interrupted = 0;
450 interrupted = true;
451 }
452
Andy Adamson691daf32009-12-04 15:55:39 -0500453 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 switch (res->sr_status) {
455 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500457 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500458 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500459 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500460 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500461 if (res->sr_status_flags != 0)
462 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500463 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500465 case 1:
466 /*
467 * sr_status remains 1 if an RPC level error occurred.
468 * The server may or may not have processed the sequence
469 * operation..
470 * Mark the slot as having hosted an interrupted RPC call.
471 */
472 slot->interrupted = 1;
473 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400474 case -NFS4ERR_DELAY:
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
477 * of RFC5661.
478 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500479 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500481 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500482 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500484 case -NFS4ERR_BADSLOT:
485 /*
486 * The slot id we used was probably retired. Try again
487 * using a different slot id.
488 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500489 goto retry_nowait;
490 case -NFS4ERR_SEQ_MISORDERED:
491 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500492 * Was the last operation on this sequence interrupted?
493 * If so, retry after bumping the sequence number.
494 */
495 if (interrupted) {
496 ++slot->seq_nr;
497 goto retry_nowait;
498 }
499 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500500 * Could this slot have been previously retired?
501 * If so, then the server may be expecting seq_nr = 1!
502 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500503 if (slot->seq_nr != 1) {
504 slot->seq_nr = 1;
505 goto retry_nowait;
506 }
507 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500508 case -NFS4ERR_SEQ_FALSE_RETRY:
509 ++slot->seq_nr;
510 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400511 default:
512 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500513 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514 }
515out:
516 /* The session may be reset by one of the error handlers. */
517 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400518 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500519 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500520retry_nowait:
521 if (rpc_restart_call_prepare(task)) {
522 task->tk_status = 0;
523 ret = 0;
524 }
525 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400526out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400527 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 goto out;
529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
530 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400531}
532
Trond Myklebust14516c32010-07-31 14:29:06 -0400533static int nfs4_sequence_done(struct rpc_task *task,
534 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400535{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500536 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 return 1;
538 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400539}
540
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
543{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500544 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500545 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400546 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500547 if (cache_reply)
548 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500549 res->sr_slot = NULL;
550}
551
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400552static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
553{
554 args->sa_privileged = 1;
555}
556
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000557int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct rpc_task *task)
561{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400562 struct nfs4_slot *slot;
563 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564
565 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400567 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400568 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 tbl = &session->fc_slot_table;
571
Trond Myklebust69d206b2012-11-22 13:21:02 -0500572 task->tk_timeout = 0;
573
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400575 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400576 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500577 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500578 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400579 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400580 }
581
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500582 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500583 if (IS_ERR(slot)) {
584 /* If out of memory, try again in 1/4 second */
585 if (slot == ERR_PTR(-ENOMEM))
586 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400588 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500592 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500594 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
595 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596
Benny Halevydfb4f3092010-09-24 09:17:01 -0400597 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500598 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400599 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 /*
601 * sr_status is only set in decode_sequence, and so will remain
602 * set to 1 if an rpc level failure occurs.
603 */
604 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400605out_success:
606 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400608out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400609 /* Privileged tasks are queued with top priority */
610 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400611 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
612 NULL, RPC_PRIORITY_PRIVILEGED);
613 else
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400615 spin_unlock(&tbl->slot_tbl_lock);
616 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000618EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619
Trond Myklebust035168a2010-06-16 09:52:26 -0400620int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 struct rpc_task *task)
624{
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 int ret = 0;
627
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400628 if (session == NULL) {
629 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400631 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400632
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500633 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400634 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638out:
639 dprintk("<-- %s status=%d\n", __func__, ret);
640 return ret;
641}
642
643struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647};
648
649static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400652 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
655
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400656 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Trond Myklebust17280172012-03-11 13:11:00 -0400666static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400675 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
Trond Myklebustdf896452010-06-16 09:52:26 -0400701#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400702static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500703void nfs41_init_sequence(struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res, int cache_reply)
705{
706}
707
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400708static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709{
710}
711
712
Trond Myklebust14516c32010-07-31 14:29:06 -0400713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400715{
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400717}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718#endif /* CONFIG_NFS_V4_1 */
719
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400720static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400725 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728}
729
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400730static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400740 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700749 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400750 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000752 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500762 struct nfs4_string owner_name;
763 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400772 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100773 int rpc_status;
774 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775};
776
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400777static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
778 int err, struct nfs4_exception *exception)
779{
780 if (err != -EINVAL)
781 return false;
782 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
783 return false;
784 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
785 exception->retry = 1;
786 return true;
787}
788
789static enum open_claim_type4
790nfs4_map_atomic_open_claim(struct nfs_server *server,
791 enum open_claim_type4 claim)
792{
793 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
794 return claim;
795 switch (claim) {
796 default:
797 return claim;
798 case NFS4_OPEN_CLAIM_FH:
799 return NFS4_OPEN_CLAIM_NULL;
800 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
801 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
802 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
803 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
804 }
805}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400806
807static void nfs4_init_opendata_res(struct nfs4_opendata *p)
808{
809 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400810 p->o_res.seqid = p->o_arg.seqid;
811 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400813 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500815 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816}
817
Al Viro82a2c1b2011-06-22 18:30:55 -0400818static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400820 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400821 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400822 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823{
Al Viro82a2c1b2011-06-22 18:30:55 -0400824 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 struct inode *dir = parent->d_inode;
826 struct nfs_server *server = NFS_SERVER(dir);
827 struct nfs4_opendata *p;
828
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400829 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (p == NULL)
831 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400832 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 if (p->o_arg.seqid == NULL)
834 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400835 nfs_sb_active(dentry->d_sb);
836 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->dir = parent;
838 p->owner = sp;
839 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 p->o_arg.open_flags = flags;
841 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700842 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
843 * will return permission denied for all bits until close */
844 if (!(flags & O_EXCL)) {
845 /* ask server to check for all possible rights as results
846 * are cached */
847 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
848 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700849 }
David Howells7539bba2006-08-22 20:06:09 -0400850 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400851 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
852 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 p->o_arg.server = server;
855 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400856 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400857 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
858 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400859 case NFS4_OPEN_CLAIM_NULL:
860 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
861 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
862 p->o_arg.fh = NFS_FH(dir);
863 break;
864 case NFS4_OPEN_CLAIM_PREVIOUS:
865 case NFS4_OPEN_CLAIM_FH:
866 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
867 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
868 p->o_arg.fh = NFS_FH(dentry->d_inode);
869 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500870 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500871 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400872
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873 p->o_arg.u.attrs = &p->attrs;
874 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500875
876 verf[0] = jiffies;
877 verf[1] = current->pid;
878 memcpy(p->o_arg.u.verifier.data, verf,
879 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100881 p->c_arg.fh = &p->o_res.fh;
882 p->c_arg.stateid = &p->o_res.stateid;
883 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400884 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400885 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 return p;
887err_free:
888 kfree(p);
889err:
890 dput(parent);
891 return NULL;
892}
893
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400894static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400896 struct nfs4_opendata *p = container_of(kref,
897 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400898 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899
900 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 if (p->state != NULL)
902 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400903 nfs4_put_state_owner(p->owner);
904 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400905 dput(p->dentry);
906 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500907 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400908 kfree(p);
909}
910
911static void nfs4_opendata_put(struct nfs4_opendata *p)
912{
913 if (p != NULL)
914 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915}
916
Trond Myklebust06f814a2006-01-03 09:55:07 +0100917static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
918{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100919 int ret;
920
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100922 return ret;
923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400926{
927 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928
Trond Myklebust536e43d2012-01-17 22:04:26 -0500929 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 goto out;
931 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500933 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
934 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 break;
936 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500937 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
938 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939 break;
940 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500941 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
942 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 return ret;
946}
947
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500950 if (delegation == NULL)
951 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500954 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500956 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
957 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500958 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 return 1;
960}
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100963{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100965 case FMODE_WRITE:
966 state->n_wronly++;
967 break;
968 case FMODE_READ:
969 state->n_rdonly++;
970 break;
971 case FMODE_READ|FMODE_WRITE:
972 state->n_rdwr++;
973 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400978{
979 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500980 nfs4_stateid_copy(&state->stateid, stateid);
981 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400982 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 case FMODE_READ:
985 set_bit(NFS_O_RDONLY_STATE, &state->flags);
986 break;
987 case FMODE_WRITE:
988 set_bit(NFS_O_WRONLY_STATE, &state->flags);
989 break;
990 case FMODE_READ|FMODE_WRITE:
991 set_bit(NFS_O_RDWR_STATE, &state->flags);
992 }
993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400996{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400997 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400999 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001000}
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static 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 -07001003{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001004 /*
1005 * Protect the call to nfs4_state_set_mode_locked and
1006 * serialise the stateid update
1007 */
1008 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001009 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001010 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 set_bit(NFS_DELEGATED_STATE, &state->flags);
1012 }
1013 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001015 write_sequnlock(&state->seqlock);
1016 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001018 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static 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 -05001022{
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *deleg_cur;
1025 int ret = 0;
1026
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 rcu_read_lock();
1030 deleg_cur = rcu_dereference(nfsi->delegation);
1031 if (deleg_cur == NULL)
1032 goto no_delegation;
1033
1034 spin_lock(&deleg_cur->lock);
1035 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001036 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001038 goto no_delegation_unlock;
1039
1040 if (delegation == NULL)
1041 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001042 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001043 goto no_delegation_unlock;
1044
Trond Myklebustb7391f42008-12-23 15:21:52 -05001045 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001047 ret = 1;
1048no_delegation_unlock:
1049 spin_unlock(&deleg_cur->lock);
1050no_delegation:
1051 rcu_read_unlock();
1052
1053 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001055 ret = 1;
1056 }
1057
1058 return ret;
1059}
1060
1061
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063{
1064 struct nfs_delegation *delegation;
1065
1066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 rcu_read_unlock();
1070 return;
1071 }
1072 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001073 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074}
1075
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077{
1078 struct nfs4_state *state = opendata->state;
1079 struct nfs_inode *nfsi = NFS_I(state->inode);
1080 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001081 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 nfs4_stateid stateid;
1084 int ret = -EAGAIN;
1085
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 if (can_open_cached(state, fmode, open_mode)) {
1090 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092 goto out_return_state;
1093 }
1094 spin_unlock(&state->owner->so_lock);
1095 }
Trond Myklebust34310432008-12-23 15:21:38 -05001096 rcu_read_lock();
1097 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001098 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001099 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001101 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001103 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 rcu_read_unlock();
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001105 if (!opendata->is_recover) {
1106 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1107 if (ret != 0)
1108 goto out;
1109 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001111
1112 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001114 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116out:
1117 return ERR_PTR(ret);
1118out_return_state:
1119 atomic_inc(&state->count);
1120 return state;
1121}
1122
Andy Adamsone23008e2012-10-02 21:07:32 -04001123static void
1124nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1125{
1126 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1127 struct nfs_delegation *delegation;
1128 int delegation_flags = 0;
1129
1130 rcu_read_lock();
1131 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1132 if (delegation)
1133 delegation_flags = delegation->flags;
1134 rcu_read_unlock();
1135 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1136 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1137 "returning a delegation for "
1138 "OPEN(CLAIM_DELEGATE_CUR)\n",
1139 clp->cl_hostname);
1140 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1141 nfs_inode_set_delegation(state->inode,
1142 data->owner->so_cred,
1143 &data->o_res);
1144 else
1145 nfs_inode_reclaim_delegation(state->inode,
1146 data->owner->so_cred,
1147 &data->o_res);
1148}
1149
1150/*
1151 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1152 * and update the nfs4_state.
1153 */
1154static struct nfs4_state *
1155_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1156{
1157 struct inode *inode = data->state->inode;
1158 struct nfs4_state *state = data->state;
1159 int ret;
1160
1161 if (!data->rpc_done) {
1162 ret = data->rpc_status;
1163 goto err;
1164 }
1165
1166 ret = -ESTALE;
1167 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1168 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1169 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1170 goto err;
1171
1172 ret = -ENOMEM;
1173 state = nfs4_get_open_state(inode, data->owner);
1174 if (state == NULL)
1175 goto err;
1176
1177 ret = nfs_refresh_inode(inode, &data->f_attr);
1178 if (ret)
1179 goto err;
1180
1181 if (data->o_res.delegation_type != 0)
1182 nfs4_opendata_check_deleg(data, state);
1183 update_open_stateid(state, &data->o_res.stateid, NULL,
1184 data->o_arg.fmode);
1185
1186 return state;
1187err:
1188 return ERR_PTR(ret);
1189
1190}
1191
1192static struct nfs4_state *
1193_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001194{
1195 struct inode *inode;
1196 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001197 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001198
Trond Myklebustaac00a82007-07-05 19:02:21 -04001199 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001200 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001201 goto out;
1202 }
1203
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001204 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001205 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001206 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001207 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001208 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001209 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001210 goto err;
1211 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001212 state = nfs4_get_open_state(inode, data->owner);
1213 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001214 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001215 if (data->o_res.delegation_type != 0)
1216 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001217 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001220out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001221 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001222 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001223err_put_inode:
1224 iput(inode);
1225err:
1226 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001227}
1228
Andy Adamsone23008e2012-10-02 21:07:32 -04001229static struct nfs4_state *
1230nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1231{
1232 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1233 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1234 return _nfs4_opendata_to_nfs4_state(data);
1235}
1236
Trond Myklebust864472e2006-01-03 09:55:15 +01001237static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1238{
1239 struct nfs_inode *nfsi = NFS_I(state->inode);
1240 struct nfs_open_context *ctx;
1241
1242 spin_lock(&state->inode->i_lock);
1243 list_for_each_entry(ctx, &nfsi->open_files, list) {
1244 if (ctx->state != state)
1245 continue;
1246 get_nfs_open_context(ctx);
1247 spin_unlock(&state->inode->i_lock);
1248 return ctx;
1249 }
1250 spin_unlock(&state->inode->i_lock);
1251 return ERR_PTR(-ENOENT);
1252}
1253
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001254static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1255 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001256{
1257 struct nfs4_opendata *opendata;
1258
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001259 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1260 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001261 if (opendata == NULL)
1262 return ERR_PTR(-ENOMEM);
1263 opendata->state = state;
1264 atomic_inc(&state->count);
1265 return opendata;
1266}
1267
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001269{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001270 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 int ret;
1272
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001273 opendata->o_arg.open_flags = 0;
1274 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001275 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1276 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1277 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001278 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 if (ret != 0)
1280 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 if (IS_ERR(newstate))
1283 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001284 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001285 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 return 0;
1287}
1288
1289static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1290{
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001292 int ret;
1293
1294 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001295 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001296 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 smp_rmb();
1298 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001299 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001300 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 if (ret != 0)
1302 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001303 if (newstate != state)
1304 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 }
1306 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001307 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001308 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 if (ret != 0)
1310 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001311 if (newstate != state)
1312 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 }
1314 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001315 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001316 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 if (ret != 0)
1318 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001319 if (newstate != state)
1320 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001321 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001322 /*
1323 * We may have performed cached opens for all three recoveries.
1324 * Check if we need to update the current stateid.
1325 */
1326 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001327 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001328 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001329 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001330 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001331 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001332 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 return 0;
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/*
1337 * OPEN_RECLAIM:
1338 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001340static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001342 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001343 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int status;
1346
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001347 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1348 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001349 if (IS_ERR(opendata))
1350 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001351 rcu_read_lock();
1352 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001353 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001354 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001355 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001356 opendata->o_arg.u.delegation_type = delegation_type;
1357 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001358 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return status;
1360}
1361
Trond Myklebust539cd032007-06-05 11:46:42 -04001362static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct nfs_server *server = NFS_SERVER(state->inode);
1365 struct nfs4_exception exception = { };
1366 int err;
1367 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001368 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001369 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1370 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001371 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001372 break;
1373 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } while (exception.retry);
1375 return err;
1376}
1377
Trond Myklebust864472e2006-01-03 09:55:15 +01001378static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1379{
1380 struct nfs_open_context *ctx;
1381 int ret;
1382
1383 ctx = nfs4_state_find_open_context(state);
1384 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001385 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001386 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 put_nfs_open_context(ctx);
1388 return ret;
1389}
1390
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001391static 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 -07001392{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001393 switch (err) {
1394 default:
1395 printk(KERN_ERR "NFS: %s: unhandled error "
1396 "%d.\n", __func__, err);
1397 case 0:
1398 case -ENOENT:
1399 case -ESTALE:
1400 break;
1401 case -NFS4ERR_BADSESSION:
1402 case -NFS4ERR_BADSLOT:
1403 case -NFS4ERR_BAD_HIGH_SLOT:
1404 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1405 case -NFS4ERR_DEADSESSION:
1406 set_bit(NFS_DELEGATED_STATE, &state->flags);
1407 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1408 return -EAGAIN;
1409 case -NFS4ERR_STALE_CLIENTID:
1410 case -NFS4ERR_STALE_STATEID:
1411 set_bit(NFS_DELEGATED_STATE, &state->flags);
1412 case -NFS4ERR_EXPIRED:
1413 /* Don't recall a delegation if it was lost */
1414 nfs4_schedule_lease_recovery(server->nfs_client);
1415 return -EAGAIN;
1416 case -NFS4ERR_DELEG_REVOKED:
1417 case -NFS4ERR_ADMIN_REVOKED:
1418 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001419 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001420 nfs_inode_find_state_and_recover(state->inode,
1421 stateid);
1422 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001423 return 0;
1424 case -NFS4ERR_DELAY:
1425 case -NFS4ERR_GRACE:
1426 set_bit(NFS_DELEGATED_STATE, &state->flags);
1427 ssleep(1);
1428 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001429 case -ENOMEM:
1430 case -NFS4ERR_DENIED:
1431 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1432 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return err;
1435}
1436
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001437int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1438{
1439 struct nfs_server *server = NFS_SERVER(state->inode);
1440 struct nfs4_opendata *opendata;
1441 int err;
1442
1443 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1444 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1445 if (IS_ERR(opendata))
1446 return PTR_ERR(opendata);
1447 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1448 err = nfs4_open_recover(opendata, state);
1449 nfs4_opendata_put(opendata);
1450 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1451}
1452
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001453static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1454{
1455 struct nfs4_opendata *data = calldata;
1456
1457 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001459 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001460 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001461 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001462 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001463 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001464}
1465
1466static void nfs4_open_confirm_release(void *calldata)
1467{
1468 struct nfs4_opendata *data = calldata;
1469 struct nfs4_state *state = NULL;
1470
1471 /* If this request hasn't been cancelled, do nothing */
1472 if (data->cancelled == 0)
1473 goto out_free;
1474 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001475 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001477 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001478 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001479 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001480out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001481 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001482}
1483
1484static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001485 .rpc_call_done = nfs4_open_confirm_done,
1486 .rpc_release = nfs4_open_confirm_release,
1487};
1488
1489/*
1490 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1491 */
1492static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1493{
1494 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1495 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 struct rpc_message msg = {
1497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1498 .rpc_argp = &data->c_arg,
1499 .rpc_resp = &data->c_res,
1500 .rpc_cred = data->owner->so_cred,
1501 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001502 struct rpc_task_setup task_setup_data = {
1503 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001505 .callback_ops = &nfs4_open_confirm_ops,
1506 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001507 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001508 .flags = RPC_TASK_ASYNC,
1509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int status;
1511
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001512 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001513 data->rpc_done = 0;
1514 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001515 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001516 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001517 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001518 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001519 status = nfs4_wait_for_completion_rpc_task(task);
1520 if (status != 0) {
1521 data->cancelled = 1;
1522 smp_wmb();
1523 } else
1524 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001525 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return status;
1527}
1528
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 struct nfs4_opendata *data = calldata;
1532 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001533 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001534
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001536 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537 /*
1538 * Check if we still need to send an OPEN call, or if we can use
1539 * a delegation instead.
1540 */
1541 if (data->state != NULL) {
1542 struct nfs_delegation *delegation;
1543
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001544 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001545 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001546 rcu_read_lock();
1547 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001548 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001549 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001550 can_open_delegated(delegation, data->o_arg.fmode))
1551 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552 rcu_read_unlock();
1553 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001554 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001555 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001556 switch (data->o_arg.claim) {
1557 case NFS4_OPEN_CLAIM_PREVIOUS:
1558 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1559 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001560 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001561 case NFS4_OPEN_CLAIM_FH:
1562 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001563 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1564 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001565 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001566 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001567 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001568 &data->o_res.seq_res,
1569 task) != 0)
1570 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001571
1572 /* Set the create mode (note dependency on the session type) */
1573 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1574 if (data->o_arg.open_flags & O_EXCL) {
1575 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1576 if (nfs4_has_persistent_session(clp))
1577 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1578 else if (clp->cl_mvops->minor_version > 0)
1579 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1580 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001581 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001582unlock_no_action:
1583 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001584out_no_action:
1585 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001586out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001587 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001588}
1589
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590static void nfs4_open_done(struct rpc_task *task, void *calldata)
1591{
1592 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001595
Trond Myklebust14516c32010-07-31 14:29:06 -04001596 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1597 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001598
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001600 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1601 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001602 case S_IFREG:
1603 break;
1604 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001606 break;
1607 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001609 break;
1610 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001612 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001613 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001614 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001615 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1616 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001617 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001618 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001620
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621static void nfs4_open_release(void *calldata)
1622{
1623 struct nfs4_opendata *data = calldata;
1624 struct nfs4_state *state = NULL;
1625
1626 /* If this request hasn't been cancelled, do nothing */
1627 if (data->cancelled == 0)
1628 goto out_free;
1629 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001630 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001631 goto out_free;
1632 /* In case we need an open_confirm, no cleanup! */
1633 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1634 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001635 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001636 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001637 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001639 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640}
1641
1642static const struct rpc_call_ops nfs4_open_ops = {
1643 .rpc_call_prepare = nfs4_open_prepare,
1644 .rpc_call_done = nfs4_open_done,
1645 .rpc_release = nfs4_open_release,
1646};
1647
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001648static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649{
1650 struct inode *dir = data->dir->d_inode;
1651 struct nfs_server *server = NFS_SERVER(dir);
1652 struct nfs_openargs *o_arg = &data->o_arg;
1653 struct nfs_openres *o_res = &data->o_res;
1654 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001655 struct rpc_message msg = {
1656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1657 .rpc_argp = o_arg,
1658 .rpc_resp = o_res,
1659 .rpc_cred = data->owner->so_cred,
1660 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001661 struct rpc_task_setup task_setup_data = {
1662 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001663 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001664 .callback_ops = &nfs4_open_ops,
1665 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001666 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001667 .flags = RPC_TASK_ASYNC,
1668 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669 int status;
1670
Trond Myklebust9d12b212012-01-17 22:04:25 -05001671 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001672 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001673 data->rpc_done = 0;
1674 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001675 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001676 data->is_recover = 0;
1677 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001678 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001679 data->is_recover = 1;
1680 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001681 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001682 if (IS_ERR(task))
1683 return PTR_ERR(task);
1684 status = nfs4_wait_for_completion_rpc_task(task);
1685 if (status != 0) {
1686 data->cancelled = 1;
1687 smp_wmb();
1688 } else
1689 status = data->rpc_status;
1690 rpc_put_task(task);
1691
1692 return status;
1693}
1694
1695static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1696{
1697 struct inode *dir = data->dir->d_inode;
1698 struct nfs_openres *o_res = &data->o_res;
1699 int status;
1700
1701 status = nfs4_run_open_task(data, 1);
1702 if (status != 0 || !data->rpc_done)
1703 return status;
1704
Trond Myklebust6926afd2012-01-07 13:22:46 -05001705 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1706
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001707 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1708 status = _nfs4_proc_open_confirm(data);
1709 if (status != 0)
1710 return status;
1711 }
1712
1713 return status;
1714}
1715
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001716static int nfs4_opendata_access(struct rpc_cred *cred,
1717 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001718 struct nfs4_state *state, fmode_t fmode,
1719 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001720{
1721 struct nfs_access_entry cache;
1722 u32 mask;
1723
1724 /* access call failed or for some reason the server doesn't
1725 * support any access modes -- defer access call until later */
1726 if (opendata->o_res.access_supported == 0)
1727 return 0;
1728
1729 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001730 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001731 * write mode bits, but POSIX allows the creating process to write.
1732 * use openflags to check for exec, because fmode won't
1733 * always have FMODE_EXEC set when file open for exec. */
1734 if (openflags & __FMODE_EXEC) {
1735 /* ONLY check for exec rights */
1736 mask = MAY_EXEC;
1737 } else if (fmode & FMODE_READ)
1738 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001739
1740 cache.cred = cred;
1741 cache.jiffies = jiffies;
1742 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1743 nfs_access_add_cache(state->inode, &cache);
1744
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001745 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001746 return 0;
1747
1748 /* even though OPEN succeeded, access is denied. Close the file */
1749 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001750 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001751}
1752
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001753/*
1754 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1755 */
1756static int _nfs4_proc_open(struct nfs4_opendata *data)
1757{
1758 struct inode *dir = data->dir->d_inode;
1759 struct nfs_server *server = NFS_SERVER(dir);
1760 struct nfs_openargs *o_arg = &data->o_arg;
1761 struct nfs_openres *o_res = &data->o_res;
1762 int status;
1763
1764 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001765 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001767 if (status != 0) {
1768 if (status == -NFS4ERR_BADNAME &&
1769 !(o_arg->open_flags & O_CREAT))
1770 return -ENOENT;
1771 return status;
1772 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773
Trond Myklebust6926afd2012-01-07 13:22:46 -05001774 nfs_fattr_map_and_free_names(server, &data->f_attr);
1775
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001776 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001777 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001778 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1779 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001781 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001786 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Andy Adamsond83217c2011-03-01 01:34:17 +00001790static int nfs4_recover_expired_lease(struct nfs_server *server)
1791{
1792 return nfs4_client_recover_expired_lease(server->nfs_client);
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
1796 * OPEN_EXPIRED:
1797 * reclaim state on the server after a network partition.
1798 * Assumes caller holds the appropriate lock
1799 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001800static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001802 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001803 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001805 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001806 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001807 if (IS_ERR(opendata))
1808 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001809 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001810 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001811 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001813 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001816static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001817{
Trond Myklebust539cd032007-06-05 11:46:42 -04001818 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001819 struct nfs4_exception exception = { };
1820 int err;
1821
1822 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001823 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001824 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1825 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001826 switch (err) {
1827 default:
1828 goto out;
1829 case -NFS4ERR_GRACE:
1830 case -NFS4ERR_DELAY:
1831 nfs4_handle_exception(server, err, &exception);
1832 err = 0;
1833 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001834 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001835out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001836 return err;
1837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001842 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 ctx = nfs4_state_find_open_context(state);
1845 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001846 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001847 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001848 put_nfs_open_context(ctx);
1849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001852#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001853static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001854{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001855 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001856 nfs4_stateid *stateid = &state->stateid;
1857 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001858
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001859 /* If a state reset has been done, test_stateid is unneeded */
1860 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1861 return;
1862
1863 status = nfs41_test_stateid(server, stateid);
1864 if (status != NFS_OK) {
1865 /* Free the stateid unless the server explicitly
1866 * informs us the stateid is unrecognized. */
1867 if (status != -NFS4ERR_BAD_STATEID)
1868 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001869 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001870
Bryan Schumaker69388672012-09-26 15:25:52 -04001871 write_seqlock(&state->seqlock);
1872 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1873 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001874 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1875 }
1876}
1877
1878/**
1879 * nfs41_check_open_stateid - possibly free an open stateid
1880 *
1881 * @state: NFSv4 state for an inode
1882 *
1883 * Returns NFS_OK if recovery for this stateid is now finished.
1884 * Otherwise a negative NFS4ERR value is returned.
1885 */
1886static int nfs41_check_open_stateid(struct nfs4_state *state)
1887{
1888 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001889 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001890 int status;
1891
1892 /* If a state reset has been done, test_stateid is unneeded */
1893 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1894 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1895 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1896 return -NFS4ERR_BAD_STATEID;
1897
1898 status = nfs41_test_stateid(server, stateid);
1899 if (status != NFS_OK) {
1900 /* Free the stateid unless the server explicitly
1901 * informs us the stateid is unrecognized. */
1902 if (status != -NFS4ERR_BAD_STATEID)
1903 nfs41_free_stateid(server, stateid);
1904
1905 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1906 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1907 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001908 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001909 }
1910 return status;
1911}
1912
1913static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1914{
Chuck Levereb64cf92012-07-11 16:30:05 -04001915 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001916
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001917 nfs41_clear_delegation_stateid(state);
1918 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001919 if (status != NFS_OK)
1920 status = nfs4_open_expired(sp, state);
1921 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001922}
1923#endif
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001926 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1927 * fields corresponding to attributes that were used to store the verifier.
1928 * Make sure we clobber those fields in the later setattr call
1929 */
1930static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1931{
1932 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1933 !(sattr->ia_valid & ATTR_ATIME_SET))
1934 sattr->ia_valid |= ATTR_ATIME;
1935
1936 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1937 !(sattr->ia_valid & ATTR_MTIME_SET))
1938 sattr->ia_valid |= ATTR_MTIME;
1939}
1940
Trond Myklebustc21443c2013-02-07 14:26:21 -05001941static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1942 fmode_t fmode,
1943 int flags,
1944 struct nfs4_state **res)
1945{
1946 struct nfs4_state_owner *sp = opendata->owner;
1947 struct nfs_server *server = sp->so_server;
1948 struct nfs4_state *state;
1949 unsigned int seq;
1950 int ret;
1951
1952 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1953
1954 ret = _nfs4_proc_open(opendata);
1955 if (ret != 0)
1956 goto out;
1957
1958 state = nfs4_opendata_to_nfs4_state(opendata);
1959 ret = PTR_ERR(state);
1960 if (IS_ERR(state))
1961 goto out;
1962 if (server->caps & NFS_CAP_POSIX_LOCK)
1963 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1964
1965 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1966 if (ret != 0)
1967 goto out;
1968
Trond Myklebust91876b12013-03-28 14:01:33 -04001969 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001970 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001971 *res = state;
1972out:
1973 return ret;
1974}
1975
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001976/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001977 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Andy Adamson82be4172012-05-23 05:02:35 -04001979static int _nfs4_do_open(struct inode *dir,
1980 struct dentry *dentry,
1981 fmode_t fmode,
1982 int flags,
1983 struct iattr *sattr,
1984 struct rpc_cred *cred,
1985 struct nfs4_state **res,
1986 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct nfs4_state_owner *sp;
1989 struct nfs4_state *state = NULL;
1990 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001991 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001992 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001993 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001997 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1998 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2000 goto out_err;
2001 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002002 status = nfs4_recover_expired_lease(server);
2003 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002004 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002005 if (dentry->d_inode != NULL)
2006 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002007 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002008 if (dentry->d_inode)
2009 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002010 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002011 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002012 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002013 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Andy Adamson82be4172012-05-23 05:02:35 -04002015 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2016 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2017 if (!opendata->f_attr.mdsthreshold)
2018 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002019 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002020 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002021 if (dentry->d_inode != NULL)
2022 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002023
Trond Myklebustc21443c2013-02-07 14:26:21 -05002024 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002025 if (status != 0)
2026 goto err_opendata_put;
2027
Trond Myklebust549b19c2013-04-16 18:42:34 -04002028 if ((opendata->o_arg.open_flags & O_EXCL) &&
2029 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002030 nfs4_exclusive_attrset(opendata, sattr);
2031
2032 nfs_fattr_init(opendata->o_res.f_attr);
2033 status = nfs4_do_setattr(state->inode, cred,
2034 opendata->o_res.f_attr, sattr,
2035 state);
2036 if (status == 0)
2037 nfs_setattr_update_inode(state->inode, sattr);
2038 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2039 }
Andy Adamson82be4172012-05-23 05:02:35 -04002040
2041 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2042 *ctx_th = opendata->f_attr.mdsthreshold;
2043 else
2044 kfree(opendata->f_attr.mdsthreshold);
2045 opendata->f_attr.mdsthreshold = NULL;
2046
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002047 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 *res = state;
2050 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002051err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002052 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002053 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002054err_put_state_owner:
2055 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 *res = NULL;
2058 return status;
2059}
2060
2061
Andy Adamson82be4172012-05-23 05:02:35 -04002062static struct nfs4_state *nfs4_do_open(struct inode *dir,
2063 struct dentry *dentry,
2064 fmode_t fmode,
2065 int flags,
2066 struct iattr *sattr,
2067 struct rpc_cred *cred,
2068 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002070 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 struct nfs4_exception exception = { };
2072 struct nfs4_state *res;
2073 int status;
2074
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002075 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002077 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2078 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (status == 0)
2080 break;
2081 /* NOTE: BAD_SEQID means the server and client disagree about the
2082 * book-keeping w.r.t. state-changing operations
2083 * (OPEN/CLOSE/LOCK/LOCKU...)
2084 * It is actually a sign of a bug on the client or on the server.
2085 *
2086 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002087 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * have unhashed the old state_owner for us, and that we can
2089 * therefore safely retry using a new one. We should still warn
2090 * the user though...
2091 */
2092 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002093 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002094 " returned a bad sequence-id error!\n",
2095 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 exception.retry = 1;
2097 continue;
2098 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002099 /*
2100 * BAD_STATEID on OPEN means that the server cancelled our
2101 * state before it received the OPEN_CONFIRM.
2102 * Recover by retrying the request as per the discussion
2103 * on Page 181 of RFC3530.
2104 */
2105 if (status == -NFS4ERR_BAD_STATEID) {
2106 exception.retry = 1;
2107 continue;
2108 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002109 if (status == -EAGAIN) {
2110 /* We must have found a delegation */
2111 exception.retry = 1;
2112 continue;
2113 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002114 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2115 continue;
2116 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 status, &exception));
2118 } while (exception.retry);
2119 return res;
2120}
2121
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002122static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2123 struct nfs_fattr *fattr, struct iattr *sattr,
2124 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002126 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002128 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 .iap = sattr,
2130 .server = server,
2131 .bitmask = server->attr_bitmask,
2132 };
2133 struct nfs_setattrres res = {
2134 .fattr = fattr,
2135 .server = server,
2136 };
2137 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2139 .rpc_argp = &arg,
2140 .rpc_resp = &res,
2141 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002143 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002144 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Trond Myklebust0e574af2005-10-27 22:12:38 -04002146 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Trond Myklebust5d422302013-03-14 16:57:48 -04002148 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002149 struct nfs_lockowner lockowner = {
2150 .l_owner = current->files,
2151 .l_pid = current->tgid,
2152 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002153 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002154 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002155 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2156 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002157 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002158 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002159 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Bryan Schumaker7c513052011-03-24 17:12:24 +00002161 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002162 if (status == 0 && state != NULL)
2163 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002164 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002167static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2168 struct nfs_fattr *fattr, struct iattr *sattr,
2169 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002171 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002172 struct nfs4_exception exception = {
2173 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002174 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int err;
2177 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002178 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2179 switch (err) {
2180 case -NFS4ERR_OPENMODE:
2181 if (state && !(state->state & FMODE_WRITE)) {
2182 err = -EBADF;
2183 if (sattr->ia_valid & ATTR_OPEN)
2184 err = -EACCES;
2185 goto out;
2186 }
2187 }
2188 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002190out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return err;
2192}
2193
2194struct nfs4_closedata {
2195 struct inode *inode;
2196 struct nfs4_state *state;
2197 struct nfs_closeargs arg;
2198 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002199 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002200 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002201 bool roc;
2202 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203};
2204
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002205static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002206{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002207 struct nfs4_closedata *calldata = data;
2208 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002209 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002210
Fred Isamanf7e89172011-01-06 11:36:32 +00002211 if (calldata->roc)
2212 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002213 nfs4_put_open_state(calldata->state);
2214 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002215 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002216 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002217 kfree(calldata);
2218}
2219
Trond Myklebust88069f72009-12-08 08:33:16 -05002220static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2221 fmode_t fmode)
2222{
2223 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002224 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002225 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2226 case FMODE_WRITE:
2227 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2228 break;
2229 case FMODE_READ:
2230 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2231 break;
2232 case 0:
2233 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2234 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2235 clear_bit(NFS_OPEN_STATE, &state->flags);
2236 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002237 spin_unlock(&state->owner->so_lock);
2238}
2239
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002240static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002242 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 struct nfs_server *server = NFS_SERVER(calldata->inode);
2245
Chuck Levera3ca5652012-03-01 17:00:40 -05002246 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002247 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2248 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /* hmm. we are done with the inode, and in the process of freeing
2250 * the state_owner. we keep this around to process errors
2251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 switch (task->tk_status) {
2253 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002254 if (calldata->roc)
2255 pnfs_roc_set_barrier(state->inode,
2256 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002257 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002258 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002259 nfs4_close_clear_stateid_flags(state,
2260 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 break;
2262 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002263 case -NFS4ERR_OLD_STATEID:
2264 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002266 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002269 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2270 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002272 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002273 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002274 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002277static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002279 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002280 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002281 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002282 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002283
Chuck Levera3ca5652012-03-01 17:00:40 -05002284 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002285 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002286 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002287
Trond Myklebust88069f72009-12-08 08:33:16 -05002288 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2289 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002290 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002291 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002292 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002293 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002294 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2295 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2296 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002297 }
2298 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002299 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2300 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2301 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002302 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002303 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002304 if (!nfs4_valid_open_stateid(state))
2305 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002306 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002307
2308 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002309 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002310 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002311 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002312
Fred Isamanf7e89172011-01-06 11:36:32 +00002313 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002314 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002315 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002316 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2317 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002318 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002319 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002320 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002321
Trond Myklebust516a6af2005-10-27 22:12:41 -04002322 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002323 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002324 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002325 &calldata->arg.seq_args,
2326 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002327 task) != 0)
2328 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002329 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002330 return;
2331out_no_action:
2332 task->tk_action = NULL;
2333out_wait:
2334 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002337static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002338 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002339 .rpc_call_done = nfs4_close_done,
2340 .rpc_release = nfs4_free_closedata,
2341};
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343/*
2344 * It is possible for data to be read/written from a mem-mapped file
2345 * after the sys_close call (which hits the vfs layer as a flush).
2346 * This means that we can't safely call nfsv4 close on a file until
2347 * the inode is cleared. This in turn means that we are not good
2348 * NFSv4 citizens - we do not indicate to the server to update the file's
2349 * share state even when we are done with one of the three share
2350 * stateid's in the inode.
2351 *
2352 * NOTE: Caller must be holding the sp->so_owner semaphore!
2353 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002354int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002356 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002358 struct nfs4_state_owner *sp = state->owner;
2359 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002360 struct rpc_message msg = {
2361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2362 .rpc_cred = state->owner->so_cred,
2363 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002364 struct rpc_task_setup task_setup_data = {
2365 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002366 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002367 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002368 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002369 .flags = RPC_TASK_ASYNC,
2370 };
Trond Myklebust95121352005-10-18 14:20:12 -07002371 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002373 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002375 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002376 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002377 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002379 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002380 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002382 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002383 if (calldata->arg.seqid == NULL)
2384 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002385 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002386 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002387 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002388 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002389 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002390 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002391 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002392
Trond Myklebust1174dd12010-12-21 10:52:24 -05002393 msg.rpc_argp = &calldata->arg;
2394 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002395 task_setup_data.callback_data = calldata;
2396 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002397 if (IS_ERR(task))
2398 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002399 status = 0;
2400 if (wait)
2401 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002402 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002403 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002404out_free_calldata:
2405 kfree(calldata);
2406out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002407 nfs4_put_open_state(state);
2408 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002409 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebust2b484292010-09-17 10:56:51 -04002412static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002413nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 struct nfs4_state *state;
2416
Trond Myklebust565277f2007-10-15 18:17:53 -04002417 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002418 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2419 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002420 if (IS_ERR(state))
2421 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002422 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002423 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Trond Myklebust1185a552009-12-03 15:54:02 -05002426static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002427{
2428 if (ctx->state == NULL)
2429 return;
2430 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002431 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002432 else
Al Viro643168c2011-06-22 18:20:23 -04002433 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2437{
Benny Halevy43652ad2009-04-01 09:21:54 -04002438 struct nfs4_server_caps_arg args = {
2439 .fhandle = fhandle,
2440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct nfs4_server_caps_res res = {};
2442 struct rpc_message msg = {
2443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002444 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 .rpc_resp = &res,
2446 };
2447 int status;
2448
Bryan Schumaker7c513052011-03-24 17:12:24 +00002449 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (status == 0) {
2451 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002452 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2453 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2454 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2455 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2456 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2458 server->caps |= NFS_CAP_ACLS;
2459 if (res.has_links != 0)
2460 server->caps |= NFS_CAP_HARDLINKS;
2461 if (res.has_symlinks != 0)
2462 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002463 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2464 server->caps |= NFS_CAP_FILEID;
2465 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2466 server->caps |= NFS_CAP_MODE;
2467 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2468 server->caps |= NFS_CAP_NLINK;
2469 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2470 server->caps |= NFS_CAP_OWNER;
2471 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2472 server->caps |= NFS_CAP_OWNER_GROUP;
2473 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2474 server->caps |= NFS_CAP_ATIME;
2475 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2476 server->caps |= NFS_CAP_CTIME;
2477 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2478 server->caps |= NFS_CAP_MTIME;
2479
Trond Myklebusta65318b2009-03-11 14:10:28 -04002480 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2481 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2482 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002484 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return status;
2488}
2489
Trond Myklebust55a97592006-06-09 09:34:19 -04002490int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 struct nfs4_exception exception = { };
2493 int err;
2494 do {
2495 err = nfs4_handle_exception(server,
2496 _nfs4_server_capabilities(server, fhandle),
2497 &exception);
2498 } while (exception.retry);
2499 return err;
2500}
2501
2502static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2503 struct nfs_fsinfo *info)
2504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct nfs4_lookup_root_arg args = {
2506 .bitmask = nfs4_fattr_bitmap,
2507 };
2508 struct nfs4_lookup_res res = {
2509 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002510 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 .fh = fhandle,
2512 };
2513 struct rpc_message msg = {
2514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2515 .rpc_argp = &args,
2516 .rpc_resp = &res,
2517 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002518
Trond Myklebust0e574af2005-10-27 22:12:38 -04002519 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002520 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2524 struct nfs_fsinfo *info)
2525{
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002529 err = _nfs4_lookup_root(server, fhandle, info);
2530 switch (err) {
2531 case 0:
2532 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002533 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002534 default:
2535 err = nfs4_handle_exception(server, err, &exception);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002538out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return err;
2540}
2541
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002542static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2543 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2544{
2545 struct rpc_auth *auth;
2546 int ret;
2547
2548 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002549 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002550 ret = -EIO;
2551 goto out;
2552 }
2553 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002554out:
2555 return ret;
2556}
2557
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002558static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002559 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002561 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002562 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002564 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002565 if (len < 0)
2566 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002567
2568 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002569 /* AUTH_UNIX is the default flavor if none was specified,
2570 * thus has already been tried. */
2571 if (flav_array[i] == RPC_AUTH_UNIX)
2572 continue;
2573
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002574 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002575 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002576 continue;
2577 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002578 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002579 /*
2580 * -EACCESS could mean that the user doesn't have correct permissions
2581 * to access the mount. It could also mean that we tried to mount
2582 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2583 * existing mount programs don't handle -EACCES very well so it should
2584 * be mapped to -EPERM instead.
2585 */
2586 if (status == -EACCES)
2587 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002588 return status;
2589}
2590
2591/*
2592 * get the file handle for the "/" directory on the server
2593 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002594int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2595 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002596{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002597 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002598 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002599 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2600 /*
2601 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2602 * by nfs4_map_errors() as this function exits.
2603 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002604 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (status == 0)
2606 status = nfs4_server_capabilities(server, fhandle);
2607 if (status == 0)
2608 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002609 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Bryan Schumakerbae36242012-05-10 15:07:31 -04002612static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2613 struct nfs_fsinfo *info)
2614{
2615 int error;
2616 struct nfs_fattr *fattr = info->fattr;
2617
2618 error = nfs4_server_capabilities(server, mntfh);
2619 if (error < 0) {
2620 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2621 return error;
2622 }
2623
2624 error = nfs4_proc_getattr(server, mntfh, fattr);
2625 if (error < 0) {
2626 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2627 return error;
2628 }
2629
2630 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2631 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2632 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2633
2634 return error;
2635}
2636
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637/*
2638 * Get locations and (maybe) other attributes of a referral.
2639 * Note that we'll actually follow the referral later when
2640 * we detect fsid mismatch in inode revalidation
2641 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002642static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2643 const struct qstr *name, struct nfs_fattr *fattr,
2644 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002645{
2646 int status = -ENOMEM;
2647 struct page *page = NULL;
2648 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002649
2650 page = alloc_page(GFP_KERNEL);
2651 if (page == NULL)
2652 goto out;
2653 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2654 if (locations == NULL)
2655 goto out;
2656
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002657 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002658 if (status != 0)
2659 goto out;
2660 /* Make sure server returned a different fsid for the referral */
2661 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002662 dprintk("%s: server did not return a different fsid for"
2663 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002664 status = -EIO;
2665 goto out;
2666 }
Andy Adamson533eb462011-06-13 18:25:56 -04002667 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2668 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002669
Andy Adamson533eb462011-06-13 18:25:56 -04002670 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002671 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002672 memset(fhandle, 0, sizeof(struct nfs_fh));
2673out:
2674 if (page)
2675 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002676 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002677 return status;
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2681{
2682 struct nfs4_getattr_arg args = {
2683 .fh = fhandle,
2684 .bitmask = server->attr_bitmask,
2685 };
2686 struct nfs4_getattr_res res = {
2687 .fattr = fattr,
2688 .server = server,
2689 };
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2692 .rpc_argp = &args,
2693 .rpc_resp = &res,
2694 };
2695
Trond Myklebust0e574af2005-10-27 22:12:38 -04002696 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002697 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2701{
2702 struct nfs4_exception exception = { };
2703 int err;
2704 do {
2705 err = nfs4_handle_exception(server,
2706 _nfs4_proc_getattr(server, fhandle, fattr),
2707 &exception);
2708 } while (exception.retry);
2709 return err;
2710}
2711
2712/*
2713 * The file is not closed if it is opened due to the a request to change
2714 * the size of the file. The open call will not be needed once the
2715 * VFS layer lookup-intents are implemented.
2716 *
2717 * Close is called when the inode is destroyed.
2718 * If we haven't opened the file for O_WRONLY, we
2719 * need to in the size_change case to obtain a stateid.
2720 *
2721 * Got race?
2722 * Because OPEN is always done by name in nfsv4, it is
2723 * possible that we opened a different file by the same
2724 * name. We can recognize this race condition, but we
2725 * can't do anything about it besides returning an error.
2726 *
2727 * This will be fixed with VFS changes (lookup-intent).
2728 */
2729static int
2730nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2731 struct iattr *sattr)
2732{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002733 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002734 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002735 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 int status;
2737
Benny Halevy8a1636c2010-07-14 15:43:57 -04002738 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002739 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002740
Trond Myklebust0e574af2005-10-27 22:12:38 -04002741 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Andy Adamson26699402012-05-30 16:12:24 -04002743 /* Deal with open(O_TRUNC) */
2744 if (sattr->ia_valid & ATTR_OPEN)
2745 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2746
2747 /* Optimization: if the end result is no change, don't RPC */
2748 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2749 return 0;
2750
Trond Myklebustd5308382005-11-04 15:33:38 -05002751 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002752 if (sattr->ia_valid & ATTR_FILE) {
2753 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002754
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002755 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002756 if (ctx) {
2757 cred = ctx->cred;
2758 state = ctx->state;
2759 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002760 }
2761
2762 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002763 if (status == 0)
2764 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return status;
2766}
2767
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002768static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2769 const struct qstr *name, struct nfs_fh *fhandle,
2770 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002771{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002772 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002773 int status;
2774 struct nfs4_lookup_arg args = {
2775 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002776 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002777 .name = name,
2778 };
2779 struct nfs4_lookup_res res = {
2780 .server = server,
2781 .fattr = fattr,
2782 .fh = fhandle,
2783 };
2784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2786 .rpc_argp = &args,
2787 .rpc_resp = &res,
2788 };
2789
2790 nfs_fattr_init(fattr);
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002793 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 dprintk("NFS reply lookup: %d\n", status);
2795 return status;
2796}
2797
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002798static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002799{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002800 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002801 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002802 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2803 fattr->nlink = 2;
2804}
2805
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002806static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2807 struct qstr *name, struct nfs_fh *fhandle,
2808 struct nfs_fattr *fattr)
2809{
2810 struct nfs4_exception exception = { };
2811 struct rpc_clnt *client = *clnt;
2812 int err;
2813 do {
2814 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2815 switch (err) {
2816 case -NFS4ERR_BADNAME:
2817 err = -ENOENT;
2818 goto out;
2819 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002820 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002821 goto out;
2822 case -NFS4ERR_WRONGSEC:
2823 err = -EPERM;
2824 if (client != *clnt)
2825 goto out;
2826
2827 client = nfs4_create_sec_client(client, dir, name);
2828 if (IS_ERR(client))
2829 return PTR_ERR(client);
2830
2831 exception.retry = 1;
2832 break;
2833 default:
2834 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2835 }
2836 } while (exception.retry);
2837
2838out:
2839 if (err == 0)
2840 *clnt = client;
2841 else if (client != *clnt)
2842 rpc_shutdown_client(client);
2843
2844 return err;
2845}
2846
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002847static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002848 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002850 int status;
2851 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002852
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002853 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2854 if (client != NFS_CLIENT(dir)) {
2855 rpc_shutdown_client(client);
2856 nfs_fixup_secinfo_attributes(fattr);
2857 }
2858 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002861struct rpc_clnt *
2862nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2863 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2864{
2865 int status;
2866 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2867
2868 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2869 if (status < 0) {
2870 rpc_shutdown_client(client);
2871 return ERR_PTR(status);
2872 }
2873 return client;
2874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2877{
Trond Myklebust76b32992007-08-10 17:45:11 -04002878 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 struct nfs4_accessargs args = {
2880 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002881 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002883 struct nfs4_accessres res = {
2884 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002885 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 struct rpc_message msg = {
2887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2888 .rpc_argp = &args,
2889 .rpc_resp = &res,
2890 .rpc_cred = entry->cred,
2891 };
2892 int mode = entry->mask;
2893 int status;
2894
2895 /*
2896 * Determine which access bits we want to ask for...
2897 */
2898 if (mode & MAY_READ)
2899 args.access |= NFS4_ACCESS_READ;
2900 if (S_ISDIR(inode->i_mode)) {
2901 if (mode & MAY_WRITE)
2902 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2903 if (mode & MAY_EXEC)
2904 args.access |= NFS4_ACCESS_LOOKUP;
2905 } else {
2906 if (mode & MAY_WRITE)
2907 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2908 if (mode & MAY_EXEC)
2909 args.access |= NFS4_ACCESS_EXECUTE;
2910 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002911
2912 res.fattr = nfs_alloc_fattr();
2913 if (res.fattr == NULL)
2914 return -ENOMEM;
2915
Bryan Schumaker7c513052011-03-24 17:12:24 +00002916 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002918 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002919 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002921 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return status;
2923}
2924
2925static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2926{
2927 struct nfs4_exception exception = { };
2928 int err;
2929 do {
2930 err = nfs4_handle_exception(NFS_SERVER(inode),
2931 _nfs4_proc_access(inode, entry),
2932 &exception);
2933 } while (exception.retry);
2934 return err;
2935}
2936
2937/*
2938 * TODO: For the time being, we don't try to get any attributes
2939 * along with any of the zero-copy operations READ, READDIR,
2940 * READLINK, WRITE.
2941 *
2942 * In the case of the first three, we want to put the GETATTR
2943 * after the read-type operation -- this is because it is hard
2944 * to predict the length of a GETATTR response in v4, and thus
2945 * align the READ data correctly. This means that the GETATTR
2946 * may end up partially falling into the page cache, and we should
2947 * shift it into the 'tail' of the xdr_buf before processing.
2948 * To do this efficiently, we need to know the total length
2949 * of data received, which doesn't seem to be available outside
2950 * of the RPC layer.
2951 *
2952 * In the case of WRITE, we also want to put the GETATTR after
2953 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002954 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 *
2956 * Both of these changes to the XDR layer would in fact be quite
2957 * minor, but I decided to leave them for a subsequent patch.
2958 */
2959static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2960 unsigned int pgbase, unsigned int pglen)
2961{
2962 struct nfs4_readlink args = {
2963 .fh = NFS_FH(inode),
2964 .pgbase = pgbase,
2965 .pglen = pglen,
2966 .pages = &page,
2967 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002968 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct rpc_message msg = {
2970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2971 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 };
2974
Bryan Schumaker7c513052011-03-24 17:12:24 +00002975 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 -07002976}
2977
2978static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2979 unsigned int pgbase, unsigned int pglen)
2980{
2981 struct nfs4_exception exception = { };
2982 int err;
2983 do {
2984 err = nfs4_handle_exception(NFS_SERVER(inode),
2985 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2986 &exception);
2987 } while (exception.retry);
2988 return err;
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002992 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994static int
2995nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002996 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002998 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int status = 0;
3001
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003002 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3003 if (IS_ERR(ctx))
3004 return PTR_ERR(ctx);
3005
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003006 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003007 state = nfs4_do_open(dir, dentry, ctx->mode,
3008 flags, sattr, ctx->cred,
3009 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003010 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (IS_ERR(state)) {
3012 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003015 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003016 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003017 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003019 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return status;
3021}
3022
3023static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3024{
Trond Myklebust16e42952005-10-27 22:12:44 -04003025 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003026 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003028 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003030 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003031 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3035 .rpc_argp = &args,
3036 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003038 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003039
Bryan Schumaker7c513052011-03-24 17:12:24 +00003040 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003041 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003042 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return status;
3044}
3045
3046static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3047{
3048 struct nfs4_exception exception = { };
3049 int err;
3050 do {
3051 err = nfs4_handle_exception(NFS_SERVER(dir),
3052 _nfs4_proc_remove(dir, name),
3053 &exception);
3054 } while (exception.retry);
3055 return err;
3056}
3057
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003060 struct nfs_server *server = NFS_SERVER(dir);
3061 struct nfs_removeargs *args = msg->rpc_argp;
3062 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003064 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003066 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003069static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3070{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003071 nfs4_setup_sequence(NFS_SERVER(data->dir),
3072 &data->args.seq_args,
3073 &data->res.seq_res,
3074 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003077static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003079 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3080
Trond Myklebust14516c32010-07-31 14:29:06 -04003081 if (!nfs4_sequence_done(task, &res->seq_res))
3082 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003083 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003084 return 0;
3085 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003086 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Jeff Laytond3d41522010-09-17 17:31:57 -04003089static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3090{
3091 struct nfs_server *server = NFS_SERVER(dir);
3092 struct nfs_renameargs *arg = msg->rpc_argp;
3093 struct nfs_renameres *res = msg->rpc_resp;
3094
3095 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003096 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003097 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003098}
3099
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003100static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3101{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003102 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3103 &data->args.seq_args,
3104 &data->res.seq_res,
3105 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003106}
3107
3108static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3109 struct inode *new_dir)
3110{
3111 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3112
3113 if (!nfs4_sequence_done(task, &res->seq_res))
3114 return 0;
3115 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3116 return 0;
3117
3118 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003119 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003120 return 1;
3121}
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3124 struct inode *new_dir, struct qstr *new_name)
3125{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003126 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003127 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 .old_dir = NFS_FH(old_dir),
3129 .new_dir = NFS_FH(new_dir),
3130 .old_name = old_name,
3131 .new_name = new_name,
3132 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003133 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003134 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct rpc_message msg = {
3137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3138 .rpc_argp = &arg,
3139 .rpc_resp = &res,
3140 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003141 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Bryan Schumaker7c513052011-03-24 17:12:24 +00003143 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (!status) {
3145 update_changeattr(old_dir, &res.old_cinfo);
3146 update_changeattr(new_dir, &res.new_cinfo);
3147 }
3148 return status;
3149}
3150
3151static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3152 struct inode *new_dir, struct qstr *new_name)
3153{
3154 struct nfs4_exception exception = { };
3155 int err;
3156 do {
3157 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3158 _nfs4_proc_rename(old_dir, old_name,
3159 new_dir, new_name),
3160 &exception);
3161 } while (exception.retry);
3162 return err;
3163}
3164
3165static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3166{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003167 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct nfs4_link_arg arg = {
3169 .fh = NFS_FH(inode),
3170 .dir_fh = NFS_FH(dir),
3171 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003172 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003174 struct nfs4_link_res res = {
3175 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 struct rpc_message msg = {
3178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3179 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003180 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003182 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Trond Myklebust136f2622010-04-16 16:22:49 -04003184 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003185 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003186 goto out;
3187
Bryan Schumaker7c513052011-03-24 17:12:24 +00003188 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003189 if (!status) {
3190 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003191 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003192 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003193out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003194 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return status;
3196}
3197
3198static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3199{
3200 struct nfs4_exception exception = { };
3201 int err;
3202 do {
3203 err = nfs4_handle_exception(NFS_SERVER(inode),
3204 _nfs4_proc_link(inode, dir, name),
3205 &exception);
3206 } while (exception.retry);
3207 return err;
3208}
3209
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210struct nfs4_createdata {
3211 struct rpc_message msg;
3212 struct nfs4_create_arg arg;
3213 struct nfs4_create_res res;
3214 struct nfs_fh fh;
3215 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216};
3217
3218static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3219 struct qstr *name, struct iattr *sattr, u32 ftype)
3220{
3221 struct nfs4_createdata *data;
3222
3223 data = kzalloc(sizeof(*data), GFP_KERNEL);
3224 if (data != NULL) {
3225 struct nfs_server *server = NFS_SERVER(dir);
3226
3227 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3228 data->msg.rpc_argp = &data->arg;
3229 data->msg.rpc_resp = &data->res;
3230 data->arg.dir_fh = NFS_FH(dir);
3231 data->arg.server = server;
3232 data->arg.name = name;
3233 data->arg.attrs = sattr;
3234 data->arg.ftype = ftype;
3235 data->arg.bitmask = server->attr_bitmask;
3236 data->res.server = server;
3237 data->res.fh = &data->fh;
3238 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003239 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003240 }
3241 return data;
3242}
3243
3244static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3245{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003246 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003247 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 if (status == 0) {
3249 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003250 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3251 }
3252 return status;
3253}
3254
3255static void nfs4_free_createdata(struct nfs4_createdata *data)
3256{
3257 kfree(data);
3258}
3259
Chuck Lever4f390c12006-08-22 20:06:22 -04003260static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003261 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003263 struct nfs4_createdata *data;
3264 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Chuck Lever94a6d752006-08-22 20:06:23 -04003266 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003267 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003268
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003269 status = -ENOMEM;
3270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3271 if (data == NULL)
3272 goto out;
3273
3274 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3275 data->arg.u.symlink.pages = &page;
3276 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003278 status = nfs4_do_create(dir, dentry, data);
3279
3280 nfs4_free_createdata(data);
3281out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return status;
3283}
3284
Chuck Lever4f390c12006-08-22 20:06:22 -04003285static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003286 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 struct nfs4_exception exception = { };
3289 int err;
3290 do {
3291 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003292 _nfs4_proc_symlink(dir, dentry, page,
3293 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 &exception);
3295 } while (exception.retry);
3296 return err;
3297}
3298
3299static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3300 struct iattr *sattr)
3301{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003302 struct nfs4_createdata *data;
3303 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003305 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3306 if (data == NULL)
3307 goto out;
3308
3309 status = nfs4_do_create(dir, dentry, data);
3310
3311 nfs4_free_createdata(data);
3312out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return status;
3314}
3315
3316static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3317 struct iattr *sattr)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003321
3322 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 do {
3324 err = nfs4_handle_exception(NFS_SERVER(dir),
3325 _nfs4_proc_mkdir(dir, dentry, sattr),
3326 &exception);
3327 } while (exception.retry);
3328 return err;
3329}
3330
3331static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003332 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 struct inode *dir = dentry->d_inode;
3335 struct nfs4_readdir_arg args = {
3336 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003337 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 .pgbase = 0,
3339 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003340 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003341 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 };
3343 struct nfs4_readdir_res res;
3344 struct rpc_message msg = {
3345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3346 .rpc_argp = &args,
3347 .rpc_resp = &res,
3348 .rpc_cred = cred,
3349 };
3350 int status;
3351
Harvey Harrison3110ff82008-05-02 13:42:44 -07003352 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003353 dentry->d_parent->d_name.name,
3354 dentry->d_name.name,
3355 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003356 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003358 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 -05003359 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003360 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003361 status += args.pgbase;
3362 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003363
3364 nfs_invalidate_atime(dir);
3365
Harvey Harrison3110ff82008-05-02 13:42:44 -07003366 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return status;
3368}
3369
3370static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003371 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
3373 struct nfs4_exception exception = { };
3374 int err;
3375 do {
3376 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3377 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003378 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 &exception);
3380 } while (exception.retry);
3381 return err;
3382}
3383
3384static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3385 struct iattr *sattr, dev_t rdev)
3386{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003387 struct nfs4_createdata *data;
3388 int mode = sattr->ia_mode;
3389 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003391 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3392 if (data == NULL)
3393 goto out;
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003396 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003398 data->arg.ftype = NF4BLK;
3399 data->arg.u.device.specdata1 = MAJOR(rdev);
3400 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
3402 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003403 data->arg.ftype = NF4CHR;
3404 data->arg.u.device.specdata1 = MAJOR(rdev);
3405 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003406 } else if (!S_ISSOCK(mode)) {
3407 status = -EINVAL;
3408 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003411 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003412out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003413 nfs4_free_createdata(data);
3414out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return status;
3416}
3417
3418static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3419 struct iattr *sattr, dev_t rdev)
3420{
3421 struct nfs4_exception exception = { };
3422 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003423
3424 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 do {
3426 err = nfs4_handle_exception(NFS_SERVER(dir),
3427 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3428 &exception);
3429 } while (exception.retry);
3430 return err;
3431}
3432
3433static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3434 struct nfs_fsstat *fsstat)
3435{
3436 struct nfs4_statfs_arg args = {
3437 .fh = fhandle,
3438 .bitmask = server->attr_bitmask,
3439 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003440 struct nfs4_statfs_res res = {
3441 .fsstat = fsstat,
3442 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 struct rpc_message msg = {
3444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3445 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003446 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 };
3448
Trond Myklebust0e574af2005-10-27 22:12:38 -04003449 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003450 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
3453static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3454{
3455 struct nfs4_exception exception = { };
3456 int err;
3457 do {
3458 err = nfs4_handle_exception(server,
3459 _nfs4_proc_statfs(server, fhandle, fsstat),
3460 &exception);
3461 } while (exception.retry);
3462 return err;
3463}
3464
3465static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3466 struct nfs_fsinfo *fsinfo)
3467{
3468 struct nfs4_fsinfo_arg args = {
3469 .fh = fhandle,
3470 .bitmask = server->attr_bitmask,
3471 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003472 struct nfs4_fsinfo_res res = {
3473 .fsinfo = fsinfo,
3474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 struct rpc_message msg = {
3476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3477 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003478 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 };
3480
Bryan Schumaker7c513052011-03-24 17:12:24 +00003481 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
3484static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3485{
3486 struct nfs4_exception exception = { };
3487 int err;
3488
3489 do {
3490 err = nfs4_handle_exception(server,
3491 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3492 &exception);
3493 } while (exception.retry);
3494 return err;
3495}
3496
3497static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3498{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003499 int error;
3500
Trond Myklebust0e574af2005-10-27 22:12:38 -04003501 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003502 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003503 if (error == 0) {
3504 /* block layout checks this! */
3505 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003506 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003507 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003508
3509 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
3512static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3513 struct nfs_pathconf *pathconf)
3514{
3515 struct nfs4_pathconf_arg args = {
3516 .fh = fhandle,
3517 .bitmask = server->attr_bitmask,
3518 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003519 struct nfs4_pathconf_res res = {
3520 .pathconf = pathconf,
3521 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 struct rpc_message msg = {
3523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3524 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003525 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 };
3527
3528 /* None of the pathconf attributes are mandatory to implement */
3529 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3530 memset(pathconf, 0, sizeof(*pathconf));
3531 return 0;
3532 }
3533
Trond Myklebust0e574af2005-10-27 22:12:38 -04003534 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003535 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
3537
3538static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3539 struct nfs_pathconf *pathconf)
3540{
3541 struct nfs4_exception exception = { };
3542 int err;
3543
3544 do {
3545 err = nfs4_handle_exception(server,
3546 _nfs4_proc_pathconf(server, fhandle, pathconf),
3547 &exception);
3548 } while (exception.retry);
3549 return err;
3550}
3551
Trond Myklebust5521abf2013-03-16 20:54:34 -04003552int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003553 const struct nfs_open_context *ctx,
3554 const struct nfs_lock_context *l_ctx,
3555 fmode_t fmode)
3556{
3557 const struct nfs_lockowner *lockowner = NULL;
3558
3559 if (l_ctx != NULL)
3560 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003561 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003562}
3563EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3564
Trond Myklebust5521abf2013-03-16 20:54:34 -04003565static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3566 const struct nfs_open_context *ctx,
3567 const struct nfs_lock_context *l_ctx,
3568 fmode_t fmode)
3569{
3570 nfs4_stateid current_stateid;
3571
3572 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3573 return false;
3574 return nfs4_stateid_match(stateid, &current_stateid);
3575}
3576
3577static bool nfs4_error_stateid_expired(int err)
3578{
3579 switch (err) {
3580 case -NFS4ERR_DELEG_REVOKED:
3581 case -NFS4ERR_ADMIN_REVOKED:
3582 case -NFS4ERR_BAD_STATEID:
3583 case -NFS4ERR_STALE_STATEID:
3584 case -NFS4ERR_OLD_STATEID:
3585 case -NFS4ERR_OPENMODE:
3586 case -NFS4ERR_EXPIRED:
3587 return true;
3588 }
3589 return false;
3590}
3591
Benny Halevyd20581a2011-05-22 19:52:03 +03003592void __nfs4_read_done_cb(struct nfs_read_data *data)
3593{
Fred Isamancd841602012-04-20 14:47:44 -04003594 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003595}
3596
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003597static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Fred Isamancd841602012-04-20 14:47:44 -04003599 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003601 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003602 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003603 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003605
Benny Halevyd20581a2011-05-22 19:52:03 +03003606 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003608 renew_lease(server, data->timestamp);
3609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Trond Myklebust5521abf2013-03-16 20:54:34 -04003612static bool nfs4_read_stateid_changed(struct rpc_task *task,
3613 struct nfs_readargs *args)
3614{
3615
3616 if (!nfs4_error_stateid_expired(task->tk_status) ||
3617 nfs4_stateid_is_current(&args->stateid,
3618 args->context,
3619 args->lock_context,
3620 FMODE_READ))
3621 return false;
3622 rpc_restart_call_prepare(task);
3623 return true;
3624}
3625
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003626static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3627{
3628
3629 dprintk("--> %s\n", __func__);
3630
3631 if (!nfs4_sequence_done(task, &data->res.seq_res))
3632 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003633 if (nfs4_read_stateid_changed(task, &data->args))
3634 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003635 return data->read_done_cb ? data->read_done_cb(task, data) :
3636 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003637}
3638
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003639static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003642 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003643 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003644 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003647static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3648{
Trond Myklebust9b206142013-03-17 15:52:00 -04003649 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003650 &data->args.seq_args,
3651 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003652 task))
3653 return;
3654 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3655 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
Fred Isamanb029bc92011-03-03 15:13:42 +00003658static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
Fred Isamancd841602012-04-20 14:47:44 -04003660 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003662 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003663 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003664 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003666 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003668 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003669 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Trond Myklebust5521abf2013-03-16 20:54:34 -04003673static bool nfs4_write_stateid_changed(struct rpc_task *task,
3674 struct nfs_writeargs *args)
3675{
3676
3677 if (!nfs4_error_stateid_expired(task->tk_status) ||
3678 nfs4_stateid_is_current(&args->stateid,
3679 args->context,
3680 args->lock_context,
3681 FMODE_WRITE))
3682 return false;
3683 rpc_restart_call_prepare(task);
3684 return true;
3685}
3686
Fred Isamanb029bc92011-03-03 15:13:42 +00003687static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3688{
3689 if (!nfs4_sequence_done(task, &data->res.seq_res))
3690 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003691 if (nfs4_write_stateid_changed(task, &data->args))
3692 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003693 return data->write_done_cb ? data->write_done_cb(task, data) :
3694 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003695}
3696
Trond Myklebust5a37f852012-04-28 14:55:16 -04003697static
3698bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003699{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003700 const struct nfs_pgio_header *hdr = data->header;
3701
3702 /* Don't request attributes for pNFS or O_DIRECT writes */
3703 if (data->ds_clp != NULL || hdr->dreq != NULL)
3704 return false;
3705 /* Otherwise, request attributes if and only if we don't hold
3706 * a delegation
3707 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003708 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003709}
Fred Isamana69aef12011-03-03 15:13:47 +00003710
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003711static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Fred Isamancd841602012-04-20 14:47:44 -04003713 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003714
Trond Myklebust5a37f852012-04-28 14:55:16 -04003715 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003716 data->args.bitmask = NULL;
3717 data->res.fattr = NULL;
3718 } else
3719 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003720
Fred Isamanb029bc92011-03-03 15:13:42 +00003721 if (!data->write_done_cb)
3722 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003723 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 data->timestamp = jiffies;
3725
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003727 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003730static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3731{
Trond Myklebust9b206142013-03-17 15:52:00 -04003732 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003733 &data->args.seq_args,
3734 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003735 task))
3736 return;
3737 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3738 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
Fred Isaman0b7c0152012-04-20 14:47:39 -04003741static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3742{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003743 nfs4_setup_sequence(NFS_SERVER(data->inode),
3744 &data->args.seq_args,
3745 &data->res.seq_res,
3746 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Fred Isaman0b7c0152012-04-20 14:47:39 -04003749static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003752
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003753 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003754 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003755 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
Fred Isaman0b7c0152012-04-20 14:47:39 -04003760static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003761{
3762 if (!nfs4_sequence_done(task, &data->res.seq_res))
3763 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003764 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003765}
3766
Fred Isaman0b7c0152012-04-20 14:47:39 -04003767static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003769 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003770
Fred Isaman0b7c0152012-04-20 14:47:39 -04003771 if (data->commit_done_cb == NULL)
3772 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003773 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003774 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003775 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003778struct nfs4_renewdata {
3779 struct nfs_client *client;
3780 unsigned long timestamp;
3781};
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783/*
3784 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3785 * standalone procedure for queueing an asynchronous RENEW.
3786 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003787static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003788{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003789 struct nfs4_renewdata *data = calldata;
3790 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003791
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003792 if (atomic_read(&clp->cl_count) > 1)
3793 nfs4_schedule_state_renewal(clp);
3794 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003795 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003796}
3797
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003798static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003800 struct nfs4_renewdata *data = calldata;
3801 struct nfs_client *clp = data->client;
3802 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003805 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003806 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3807 return;
3808 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003809 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003810 return;
3811 }
3812 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003814 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003817static const struct rpc_call_ops nfs4_renew_ops = {
3818 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003819 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003820};
3821
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003822static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
3824 struct rpc_message msg = {
3825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3826 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003827 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003829 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003831 if (renew_flags == 0)
3832 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003833 if (!atomic_inc_not_zero(&clp->cl_count))
3834 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003835 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003836 if (data == NULL)
3837 return -ENOMEM;
3838 data->client = clp;
3839 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003840 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003841 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003844static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
3846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3848 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003849 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 };
3851 unsigned long now = jiffies;
3852 int status;
3853
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003854 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (status < 0)
3856 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003857 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return 0;
3859}
3860
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003861static inline int nfs4_server_supports_acls(struct nfs_server *server)
3862{
3863 return (server->caps & NFS_CAP_ACLS)
3864 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3865 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3866}
3867
Trond Myklebust21f498c2012-08-24 10:59:25 -04003868/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3869 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003870 * the stack.
3871 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003872#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003873
Neil Hormane9e3d722011-03-04 19:26:03 -05003874static int buf_to_pages_noslab(const void *buf, size_t buflen,
3875 struct page **pages, unsigned int *pgbase)
3876{
3877 struct page *newpage, **spages;
3878 int rc = 0;
3879 size_t len;
3880 spages = pages;
3881
3882 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003883 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003884 newpage = alloc_page(GFP_KERNEL);
3885
3886 if (newpage == NULL)
3887 goto unwind;
3888 memcpy(page_address(newpage), buf, len);
3889 buf += len;
3890 buflen -= len;
3891 *pages++ = newpage;
3892 rc++;
3893 } while (buflen != 0);
3894
3895 return rc;
3896
3897unwind:
3898 for(; rc > 0; rc--)
3899 __free_page(spages[rc-1]);
3900 return -ENOMEM;
3901}
3902
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003903struct nfs4_cached_acl {
3904 int cached;
3905 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003906 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003907};
3908
3909static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003910{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003911 struct nfs_inode *nfsi = NFS_I(inode);
3912
3913 spin_lock(&inode->i_lock);
3914 kfree(nfsi->nfs4_acl);
3915 nfsi->nfs4_acl = acl;
3916 spin_unlock(&inode->i_lock);
3917}
3918
3919static void nfs4_zap_acl_attr(struct inode *inode)
3920{
3921 nfs4_set_cached_acl(inode, NULL);
3922}
3923
3924static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3925{
3926 struct nfs_inode *nfsi = NFS_I(inode);
3927 struct nfs4_cached_acl *acl;
3928 int ret = -ENOENT;
3929
3930 spin_lock(&inode->i_lock);
3931 acl = nfsi->nfs4_acl;
3932 if (acl == NULL)
3933 goto out;
3934 if (buf == NULL) /* user is just asking for length */
3935 goto out_len;
3936 if (acl->cached == 0)
3937 goto out;
3938 ret = -ERANGE; /* see getxattr(2) man page */
3939 if (acl->len > buflen)
3940 goto out;
3941 memcpy(buf, acl->data, acl->len);
3942out_len:
3943 ret = acl->len;
3944out:
3945 spin_unlock(&inode->i_lock);
3946 return ret;
3947}
3948
Sachin Prabhu5794d212012-04-17 14:36:40 +01003949static 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 +00003950{
3951 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003952 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003953
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003954 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003955 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003956 if (acl == NULL)
3957 goto out;
3958 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003959 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003960 } else {
3961 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3962 if (acl == NULL)
3963 goto out;
3964 acl->cached = 0;
3965 }
3966 acl->len = acl_len;
3967out:
3968 nfs4_set_cached_acl(inode, acl);
3969}
3970
Andy Adamsonbf118a32011-12-07 11:55:27 -05003971/*
3972 * The getxattr API returns the required buffer length when called with a
3973 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3974 * the required buf. On a NULL buf, we send a page of data to the server
3975 * guessing that the ACL request can be serviced by a page. If so, we cache
3976 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3977 * the cache. If not so, we throw away the page, and cache the required
3978 * length. The next getxattr call will then produce another round trip to
3979 * the server, this time with the input buf of the required size.
3980 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003981static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003982{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003983 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003984 struct nfs_getaclargs args = {
3985 .fh = NFS_FH(inode),
3986 .acl_pages = pages,
3987 .acl_len = buflen,
3988 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003989 struct nfs_getaclres res = {
3990 .acl_len = buflen,
3991 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003992 struct rpc_message msg = {
3993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3994 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003995 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003996 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003997 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3998 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003999
Andy Adamsonbf118a32011-12-07 11:55:27 -05004000 /* As long as we're doing a round trip to the server anyway,
4001 * let's be prepared for a page of acl data. */
4002 if (npages == 0)
4003 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004004 if (npages > ARRAY_SIZE(pages))
4005 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004006
Andy Adamsonbf118a32011-12-07 11:55:27 -05004007 for (i = 0; i < npages; i++) {
4008 pages[i] = alloc_page(GFP_KERNEL);
4009 if (!pages[i])
4010 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004011 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004012
4013 /* for decoding across pages */
4014 res.acl_scratch = alloc_page(GFP_KERNEL);
4015 if (!res.acl_scratch)
4016 goto out_free;
4017
Andy Adamsonbf118a32011-12-07 11:55:27 -05004018 args.acl_len = npages * PAGE_SIZE;
4019 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004020
Peng Taode040be2012-01-10 22:42:47 +08004021 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004022 __func__, buf, buflen, npages, args.acl_len);
4023 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4024 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004025 if (ret)
4026 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004027
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004028 /* Handle the case where the passed-in buffer is too short */
4029 if (res.acl_flags & NFS4_ACL_TRUNC) {
4030 /* Did the user only issue a request for the acl length? */
4031 if (buf == NULL)
4032 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004033 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004034 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004035 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004036 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004037 if (buf) {
4038 if (res.acl_len > buflen) {
4039 ret = -ERANGE;
4040 goto out_free;
4041 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004042 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004043 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004044out_ok:
4045 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004046out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004047 for (i = 0; i < npages; i++)
4048 if (pages[i])
4049 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004050 if (res.acl_scratch)
4051 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004052 return ret;
4053}
4054
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004055static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4056{
4057 struct nfs4_exception exception = { };
4058 ssize_t ret;
4059 do {
4060 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4061 if (ret >= 0)
4062 break;
4063 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4064 } while (exception.retry);
4065 return ret;
4066}
4067
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004068static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4069{
4070 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004071 int ret;
4072
4073 if (!nfs4_server_supports_acls(server))
4074 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004075 ret = nfs_revalidate_inode(server, inode);
4076 if (ret < 0)
4077 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004078 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4079 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004080 ret = nfs4_read_cached_acl(inode, buf, buflen);
4081 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004082 /* -ENOENT is returned if there is no ACL or if there is an ACL
4083 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004084 return ret;
4085 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004086}
4087
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004088static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004089{
4090 struct nfs_server *server = NFS_SERVER(inode);
4091 struct page *pages[NFS4ACL_MAXPAGES];
4092 struct nfs_setaclargs arg = {
4093 .fh = NFS_FH(inode),
4094 .acl_pages = pages,
4095 .acl_len = buflen,
4096 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004097 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4100 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004101 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004102 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004103 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004104 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004105
4106 if (!nfs4_server_supports_acls(server))
4107 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004108 if (npages > ARRAY_SIZE(pages))
4109 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004110 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4111 if (i < 0)
4112 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004113 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004114 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004115
4116 /*
4117 * Free each page after tx, so the only ref left is
4118 * held by the network stack
4119 */
4120 for (; i > 0; i--)
4121 put_page(pages[i-1]);
4122
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004123 /*
4124 * Acl update can result in inode attribute update.
4125 * so mark the attribute cache invalid.
4126 */
4127 spin_lock(&inode->i_lock);
4128 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4129 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004130 nfs_access_zap_cache(inode);
4131 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004132 return ret;
4133}
4134
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004135static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4136{
4137 struct nfs4_exception exception = { };
4138 int err;
4139 do {
4140 err = nfs4_handle_exception(NFS_SERVER(inode),
4141 __nfs4_proc_set_acl(inode, buf, buflen),
4142 &exception);
4143 } while (exception.retry);
4144 return err;
4145}
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004148nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004150 struct nfs_client *clp = server->nfs_client;
4151
4152 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 return 0;
4154 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004155 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004156 case -NFS4ERR_ADMIN_REVOKED:
4157 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004158 if (state == NULL)
4159 break;
4160 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004161 case -NFS4ERR_OPENMODE:
4162 if (state == NULL)
4163 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004164 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4165 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004166 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004167 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004168 if (state != NULL) {
4169 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4170 goto stateid_invalid;
4171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004173 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004174 nfs4_schedule_lease_recovery(clp);
4175 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004176#if defined(CONFIG_NFS_V4_1)
4177 case -NFS4ERR_BADSESSION:
4178 case -NFS4ERR_BADSLOT:
4179 case -NFS4ERR_BAD_HIGH_SLOT:
4180 case -NFS4ERR_DEADSESSION:
4181 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4182 case -NFS4ERR_SEQ_FALSE_RETRY:
4183 case -NFS4ERR_SEQ_MISORDERED:
4184 dprintk("%s ERROR %d, Reset session\n", __func__,
4185 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004186 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004187 task->tk_status = 0;
4188 return -EAGAIN;
4189#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004191 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004192 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4194 task->tk_status = 0;
4195 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004196 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 case -NFS4ERR_OLD_STATEID:
4198 task->tk_status = 0;
4199 return -EAGAIN;
4200 }
4201 task->tk_status = nfs4_map_errors(task->tk_status);
4202 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004203stateid_invalid:
4204 task->tk_status = -EIO;
4205 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004206wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004207 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004208 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4209 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4210 task->tk_status = 0;
4211 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Chuck Leverf0920752012-05-21 22:45:41 -04004214static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4215 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004216{
4217 __be32 verf[2];
4218
Chuck Lever2c820d92012-05-21 22:45:33 -04004219 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4220 /* An impossible timestamp guarantees this value
4221 * will never match a generated boot time. */
4222 verf[0] = 0;
4223 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4224 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004225 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4226 verf[0] = (__be32)nn->boot_time.tv_sec;
4227 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004228 }
Chuck Levercd937102012-03-02 17:14:31 -05004229 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4230}
4231
Chuck Levere984a552012-09-14 17:24:21 -04004232static unsigned int
4233nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4234 char *buf, size_t len)
4235{
4236 unsigned int result;
4237
4238 rcu_read_lock();
4239 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4240 clp->cl_ipaddr,
4241 rpc_peeraddr2str(clp->cl_rpcclient,
4242 RPC_DISPLAY_ADDR),
4243 rpc_peeraddr2str(clp->cl_rpcclient,
4244 RPC_DISPLAY_PROTO));
4245 rcu_read_unlock();
4246 return result;
4247}
4248
4249static unsigned int
4250nfs4_init_uniform_client_string(const struct nfs_client *clp,
4251 char *buf, size_t len)
4252{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004253 char *nodename = clp->cl_rpcclient->cl_nodename;
4254
4255 if (nfs4_client_id_uniquifier[0] != '\0')
4256 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004257 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4258 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004259 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004260}
4261
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004262/**
4263 * nfs4_proc_setclientid - Negotiate client ID
4264 * @clp: state data structure
4265 * @program: RPC program for NFSv4 callback service
4266 * @port: IP port number for NFS4 callback service
4267 * @cred: RPC credential to use for this call
4268 * @res: where to place the result
4269 *
4270 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4271 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004272int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4273 unsigned short port, struct rpc_cred *cred,
4274 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
4276 nfs4_verifier sc_verifier;
4277 struct nfs4_setclientid setclientid = {
4278 .sc_verifier = &sc_verifier,
4279 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004280 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 };
4282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4284 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004285 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004286 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004288 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Chuck Leverde734832012-07-11 16:30:50 -04004290 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004291 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004292 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4293 setclientid.sc_name_len =
4294 nfs4_init_uniform_client_string(clp,
4295 setclientid.sc_name,
4296 sizeof(setclientid.sc_name));
4297 else
4298 setclientid.sc_name_len =
4299 nfs4_init_nonuniform_client_string(clp,
4300 setclientid.sc_name,
4301 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004302 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004303 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004304 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004305 sizeof(setclientid.sc_netid),
4306 rpc_peeraddr2str(clp->cl_rpcclient,
4307 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004308 rcu_read_unlock();
4309 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004310 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 clp->cl_ipaddr, port >> 8, port & 255);
4312
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004313 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4314 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4315 setclientid.sc_name_len, setclientid.sc_name);
4316 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4317 dprintk("NFS reply setclientid: %d\n", status);
4318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004321/**
4322 * nfs4_proc_setclientid_confirm - Confirm client ID
4323 * @clp: state data structure
4324 * @res: result of a previous SETCLIENTID
4325 * @cred: RPC credential to use for this call
4326 *
4327 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4328 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004329int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004330 struct nfs4_setclientid_res *arg,
4331 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 struct nfs_fsinfo fsinfo;
4334 struct rpc_message msg = {
4335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004336 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004338 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 };
4340 unsigned long now;
4341 int status;
4342
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004343 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4344 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4345 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004347 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (status == 0) {
4349 spin_lock(&clp->cl_lock);
4350 clp->cl_lease_time = fsinfo.lease_time * HZ;
4351 clp->cl_last_renewal = now;
4352 spin_unlock(&clp->cl_lock);
4353 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004354 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return status;
4356}
4357
Trond Myklebustfe650402006-01-03 09:55:18 +01004358struct nfs4_delegreturndata {
4359 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004360 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004361 struct nfs_fh fh;
4362 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004363 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004364 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004365 int rpc_status;
4366};
4367
Trond Myklebustfe650402006-01-03 09:55:18 +01004368static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4369{
4370 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004371
Trond Myklebust14516c32010-07-31 14:29:06 -04004372 if (!nfs4_sequence_done(task, &data->res.seq_res))
4373 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004374
Ricardo Labiaga79708862009-12-07 09:23:21 -05004375 switch (task->tk_status) {
4376 case -NFS4ERR_STALE_STATEID:
4377 case -NFS4ERR_EXPIRED:
4378 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004379 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004380 break;
4381 default:
4382 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4383 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004384 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004385 return;
4386 }
4387 }
4388 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004389}
4390
4391static void nfs4_delegreturn_release(void *calldata)
4392{
Trond Myklebustfe650402006-01-03 09:55:18 +01004393 kfree(calldata);
4394}
4395
Andy Adamson938e1012009-04-01 09:22:28 -04004396#if defined(CONFIG_NFS_V4_1)
4397static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4398{
4399 struct nfs4_delegreturndata *d_data;
4400
4401 d_data = (struct nfs4_delegreturndata *)data;
4402
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004403 nfs4_setup_sequence(d_data->res.server,
4404 &d_data->args.seq_args,
4405 &d_data->res.seq_res,
4406 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004407}
4408#endif /* CONFIG_NFS_V4_1 */
4409
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004410static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004411#if defined(CONFIG_NFS_V4_1)
4412 .rpc_call_prepare = nfs4_delegreturn_prepare,
4413#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004414 .rpc_call_done = nfs4_delegreturn_done,
4415 .rpc_release = nfs4_delegreturn_release,
4416};
4417
Trond Myklebuste6f81072008-01-24 18:14:34 -05004418static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004419{
4420 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004421 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004422 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004423 struct rpc_message msg = {
4424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4425 .rpc_cred = cred,
4426 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004427 struct rpc_task_setup task_setup_data = {
4428 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004429 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004430 .callback_ops = &nfs4_delegreturn_ops,
4431 .flags = RPC_TASK_ASYNC,
4432 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004433 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004434
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004435 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004436 if (data == NULL)
4437 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004438 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004439 data->args.fhandle = &data->fh;
4440 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004441 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004442 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004443 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004444 data->res.fattr = &data->fattr;
4445 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004446 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004447 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004448 data->rpc_status = 0;
4449
Trond Myklebustc970aa82007-07-14 15:39:59 -04004450 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004451 msg.rpc_argp = &data->args;
4452 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004453 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004454 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004455 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004456 if (!issync)
4457 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004458 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004459 if (status != 0)
4460 goto out;
4461 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004462 if (status == 0)
4463 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4464 else
4465 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004466out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004467 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004468 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
Trond Myklebuste6f81072008-01-24 18:14:34 -05004471int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
4473 struct nfs_server *server = NFS_SERVER(inode);
4474 struct nfs4_exception exception = { };
4475 int err;
4476 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004477 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 switch (err) {
4479 case -NFS4ERR_STALE_STATEID:
4480 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 case 0:
4482 return 0;
4483 }
4484 err = nfs4_handle_exception(server, err, &exception);
4485 } while (exception.retry);
4486 return err;
4487}
4488
4489#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4490#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4491
4492/*
4493 * sleep, with exponential backoff, and retry the LOCK operation.
4494 */
4495static unsigned long
4496nfs4_set_lock_task_retry(unsigned long timeout)
4497{
Jeff Laytond3103102011-12-01 22:44:39 +01004498 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 timeout <<= 1;
4500 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4501 return NFS4_LOCK_MAXTIMEOUT;
4502 return timeout;
4503}
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4506{
4507 struct inode *inode = state->inode;
4508 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004509 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004510 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004512 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004514 struct nfs_lockt_res res = {
4515 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 };
4517 struct rpc_message msg = {
4518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4519 .rpc_argp = &arg,
4520 .rpc_resp = &res,
4521 .rpc_cred = state->owner->so_cred,
4522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 struct nfs4_lock_state *lsp;
4524 int status;
4525
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004526 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004527 status = nfs4_set_lock_state(state, request);
4528 if (status != 0)
4529 goto out;
4530 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004531 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004532 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004533 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004534 switch (status) {
4535 case 0:
4536 request->fl_type = F_UNLCK;
4537 break;
4538 case -NFS4ERR_DENIED:
4539 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004541 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004542out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 return status;
4544}
4545
4546static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4547{
4548 struct nfs4_exception exception = { };
4549 int err;
4550
4551 do {
4552 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4553 _nfs4_proc_getlk(state, cmd, request),
4554 &exception);
4555 } while (exception.retry);
4556 return err;
4557}
4558
4559static int do_vfs_lock(struct file *file, struct file_lock *fl)
4560{
4561 int res = 0;
4562 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4563 case FL_POSIX:
4564 res = posix_lock_file_wait(file, fl);
4565 break;
4566 case FL_FLOCK:
4567 res = flock_lock_file_wait(file, fl);
4568 break;
4569 default:
4570 BUG();
4571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return res;
4573}
4574
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004575struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004576 struct nfs_locku_args arg;
4577 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004578 struct nfs4_lock_state *lsp;
4579 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004580 struct file_lock fl;
4581 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004582 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004583};
4584
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004585static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4586 struct nfs_open_context *ctx,
4587 struct nfs4_lock_state *lsp,
4588 struct nfs_seqid *seqid)
4589{
4590 struct nfs4_unlockdata *p;
4591 struct inode *inode = lsp->ls_state->inode;
4592
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004593 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004594 if (p == NULL)
4595 return NULL;
4596 p->arg.fh = NFS_FH(inode);
4597 p->arg.fl = &p->fl;
4598 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004599 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004600 p->arg.stateid = &lsp->ls_stateid;
4601 p->lsp = lsp;
4602 atomic_inc(&lsp->ls_count);
4603 /* Ensure we don't close file until we're done freeing locks! */
4604 p->ctx = get_nfs_open_context(ctx);
4605 memcpy(&p->fl, fl, sizeof(p->fl));
4606 p->server = NFS_SERVER(inode);
4607 return p;
4608}
4609
Trond Myklebust06f814a2006-01-03 09:55:07 +01004610static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004611{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004612 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004613 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004614 nfs4_put_lock_state(calldata->lsp);
4615 put_nfs_open_context(calldata->ctx);
4616 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004617}
4618
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004619static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004620{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004621 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004622
Trond Myklebust14516c32010-07-31 14:29:06 -04004623 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4624 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004625 switch (task->tk_status) {
4626 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004627 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4628 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004629 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004630 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004631 case -NFS4ERR_BAD_STATEID:
4632 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004633 case -NFS4ERR_STALE_STATEID:
4634 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004635 break;
4636 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004637 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004638 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004639 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004640 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004641}
4642
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004643static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004644{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004645 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004647 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004648 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004649 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004650 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004651 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004652 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004653 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004654 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004655 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004656 &calldata->res.seq_res,
4657 task) != 0)
4658 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004659 return;
4660out_no_action:
4661 task->tk_action = NULL;
4662out_wait:
4663 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004666static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004667 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004668 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004669 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004670};
4671
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004672static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4673 struct nfs_open_context *ctx,
4674 struct nfs4_lock_state *lsp,
4675 struct nfs_seqid *seqid)
4676{
4677 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4680 .rpc_cred = ctx->cred,
4681 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004682 struct rpc_task_setup task_setup_data = {
4683 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004684 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004685 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004686 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004687 .flags = RPC_TASK_ASYNC,
4688 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004689
Frank Filz137d6ac2007-07-09 15:32:29 -07004690 /* Ensure this is an unlock - when canceling a lock, the
4691 * canceled lock is passed in, and it won't be an unlock.
4692 */
4693 fl->fl_type = F_UNLCK;
4694
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004695 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4696 if (data == NULL) {
4697 nfs_free_seqid(seqid);
4698 return ERR_PTR(-ENOMEM);
4699 }
4700
Trond Myklebust9d12b212012-01-17 22:04:25 -05004701 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004702 msg.rpc_argp = &data->arg;
4703 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004704 task_setup_data.callback_data = data;
4705 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004706}
4707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4709{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004710 struct inode *inode = state->inode;
4711 struct nfs4_state_owner *sp = state->owner;
4712 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004713 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004714 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004715 struct rpc_task *task;
4716 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004717 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Trond Myklebust9b073572006-06-29 16:38:34 -04004719 status = nfs4_set_lock_state(state, request);
4720 /* Unlock _before_ we do the RPC call */
4721 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004722 /* Exclude nfs_delegation_claim_locks() */
4723 mutex_lock(&sp->so_delegreturn_mutex);
4724 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004725 down_read(&nfsi->rwsem);
4726 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4727 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004728 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004729 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004730 }
4731 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004732 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004733 if (status != 0)
4734 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 /* Is this a delegated lock? */
4736 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004737 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004738 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004739 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004740 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004741 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004742 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004743 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004744 status = PTR_ERR(task);
4745 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004746 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004747 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004748 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004749out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004750 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004751 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752}
4753
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754struct nfs4_lockdata {
4755 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004756 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004757 struct nfs4_lock_state *lsp;
4758 struct nfs_open_context *ctx;
4759 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004760 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004761 int rpc_status;
4762 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004763 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004764};
4765
4766static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004767 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4768 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004769{
4770 struct nfs4_lockdata *p;
4771 struct inode *inode = lsp->ls_state->inode;
4772 struct nfs_server *server = NFS_SERVER(inode);
4773
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004774 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004775 if (p == NULL)
4776 return NULL;
4777
4778 p->arg.fh = NFS_FH(inode);
4779 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004780 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004781 if (p->arg.open_seqid == NULL)
4782 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004783 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004784 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004785 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004786 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004787 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004788 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004789 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004790 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004791 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004792 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004793 atomic_inc(&lsp->ls_count);
4794 p->ctx = get_nfs_open_context(ctx);
4795 memcpy(&p->fl, fl, sizeof(p->fl));
4796 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004797out_free_seqid:
4798 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004799out_free:
4800 kfree(p);
4801 return NULL;
4802}
4803
4804static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4805{
4806 struct nfs4_lockdata *data = calldata;
4807 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Harvey Harrison3110ff82008-05-02 13:42:44 -07004809 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004810 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004811 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004812 /* Do we need to do an open_to_lock_owner? */
4813 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004814 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004815 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004816 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04004817 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004818 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004819 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004820 } else
4821 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004822 if (!nfs4_valid_open_stateid(state)) {
4823 data->rpc_status = -EBADF;
4824 task->tk_action = NULL;
4825 goto out_release_open_seqid;
4826 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004827 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004828 if (nfs4_setup_sequence(data->server,
4829 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004830 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004831 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004832 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004833out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004834 nfs_release_seqid(data->arg.open_seqid);
4835out_release_lock_seqid:
4836 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004837out_wait:
4838 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004839 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004840}
4841
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4843{
4844 struct nfs4_lockdata *data = calldata;
4845
Harvey Harrison3110ff82008-05-02 13:42:44 -07004846 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004847
Trond Myklebust14516c32010-07-31 14:29:06 -04004848 if (!nfs4_sequence_done(task, &data->res.seq_res))
4849 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004850
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004851 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004853 if (data->rpc_status == 0)
4854 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4855 else
4856 goto out;
4857 }
4858 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004859 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004860 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004861 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004862 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004863out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004864 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004865}
4866
4867static void nfs4_lock_release(void *calldata)
4868{
4869 struct nfs4_lockdata *data = calldata;
4870
Harvey Harrison3110ff82008-05-02 13:42:44 -07004871 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004872 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004873 if (data->cancelled != 0) {
4874 struct rpc_task *task;
4875 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4876 data->arg.lock_seqid);
4877 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004878 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004879 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004880 } else
4881 nfs_free_seqid(data->arg.lock_seqid);
4882 nfs4_put_lock_state(data->lsp);
4883 put_nfs_open_context(data->ctx);
4884 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004885 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004886}
4887
4888static const struct rpc_call_ops nfs4_lock_ops = {
4889 .rpc_call_prepare = nfs4_lock_prepare,
4890 .rpc_call_done = nfs4_lock_done,
4891 .rpc_release = nfs4_lock_release,
4892};
4893
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004894static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4895{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004896 switch (error) {
4897 case -NFS4ERR_ADMIN_REVOKED:
4898 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004899 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004900 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004901 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004902 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004903 break;
4904 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004905 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004906 case -NFS4ERR_EXPIRED:
4907 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004908 };
4909}
4910
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004911static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004912{
4913 struct nfs4_lockdata *data;
4914 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004915 struct rpc_message msg = {
4916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4917 .rpc_cred = state->owner->so_cred,
4918 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004919 struct rpc_task_setup task_setup_data = {
4920 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004921 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004922 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004923 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004924 .flags = RPC_TASK_ASYNC,
4925 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926 int ret;
4927
Harvey Harrison3110ff82008-05-02 13:42:44 -07004928 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004929 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004930 fl->fl_u.nfs4_fl.owner,
4931 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004932 if (data == NULL)
4933 return -ENOMEM;
4934 if (IS_SETLKW(cmd))
4935 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004936 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004937 msg.rpc_argp = &data->arg;
4938 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004939 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004940 if (recovery_type > NFS_LOCK_NEW) {
4941 if (recovery_type == NFS_LOCK_RECLAIM)
4942 data->arg.reclaim = NFS_LOCK_RECLAIM;
4943 nfs4_set_sequence_privileged(&data->arg.seq_args);
4944 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004945 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004946 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004947 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004948 ret = nfs4_wait_for_completion_rpc_task(task);
4949 if (ret == 0) {
4950 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004951 if (ret)
4952 nfs4_handle_setlk_error(data->server, data->lsp,
4953 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004954 } else
4955 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004956 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004957 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
4961static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4962{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004963 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004964 struct nfs4_exception exception = {
4965 .inode = state->inode,
4966 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004967 int err;
4968
4969 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004970 /* Cache the lock if possible... */
4971 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4972 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004973 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004974 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004975 break;
4976 nfs4_handle_exception(server, err, &exception);
4977 } while (exception.retry);
4978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
4981static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4982{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004983 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004984 struct nfs4_exception exception = {
4985 .inode = state->inode,
4986 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004987 int err;
4988
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004989 err = nfs4_set_lock_state(state, request);
4990 if (err != 0)
4991 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004992 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004993 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4994 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004995 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004996 switch (err) {
4997 default:
4998 goto out;
4999 case -NFS4ERR_GRACE:
5000 case -NFS4ERR_DELAY:
5001 nfs4_handle_exception(server, err, &exception);
5002 err = 0;
5003 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005004 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005005out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007}
5008
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005009#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005010/**
5011 * nfs41_check_expired_locks - possibly free a lock stateid
5012 *
5013 * @state: NFSv4 state for an inode
5014 *
5015 * Returns NFS_OK if recovery for this stateid is now finished.
5016 * Otherwise a negative NFS4ERR value is returned.
5017 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005018static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005019{
Chuck Levereb64cf92012-07-11 16:30:05 -04005020 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005021 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005022 struct nfs_server *server = NFS_SERVER(state->inode);
5023
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005024 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005025 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005026 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5027 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005028 /* Free the stateid unless the server
5029 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005030 if (status != -NFS4ERR_BAD_STATEID)
5031 nfs41_free_stateid(server,
5032 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005033 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005034 ret = status;
5035 }
5036 }
5037 };
5038
5039 return ret;
5040}
5041
5042static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5043{
5044 int status = NFS_OK;
5045
5046 if (test_bit(LK_STATE_IN_USE, &state->flags))
5047 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005048 if (status != NFS_OK)
5049 status = nfs4_lock_expired(state, request);
5050 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005051}
5052#endif
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5055{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005056 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005057 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005058 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005059 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005060 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005062 if ((fl_flags & FL_POSIX) &&
5063 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5064 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005065 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005066 status = nfs4_set_lock_state(state, request);
5067 if (status != 0)
5068 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005069 request->fl_flags |= FL_ACCESS;
5070 status = do_vfs_lock(request->fl_file, request);
5071 if (status < 0)
5072 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005073 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005074 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005075 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005076 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005077 request->fl_flags = fl_flags & ~FL_SLEEP;
5078 status = do_vfs_lock(request->fl_file, request);
5079 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005080 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005081 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5082 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005083 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005084 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005085 goto out;
5086 down_read(&nfsi->rwsem);
5087 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5088 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005089 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005090 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005091 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005092 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005093 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005094 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5095 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005096out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005097 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005098out:
5099 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 return status;
5101}
5102
5103static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5104{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005105 struct nfs4_exception exception = {
5106 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005107 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 int err;
5110
5111 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005112 err = _nfs4_proc_setlk(state, cmd, request);
5113 if (err == -NFS4ERR_DENIED)
5114 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005116 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 } while (exception.retry);
5118 return err;
5119}
5120
5121static int
5122nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5123{
5124 struct nfs_open_context *ctx;
5125 struct nfs4_state *state;
5126 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5127 int status;
5128
5129 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005130 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 state = ctx->state;
5132
5133 if (request->fl_start < 0 || request->fl_end < 0)
5134 return -EINVAL;
5135
Trond Myklebustd9531262009-07-21 19:22:38 -04005136 if (IS_GETLK(cmd)) {
5137 if (state != NULL)
5138 return nfs4_proc_getlk(state, F_GETLK, request);
5139 return 0;
5140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5143 return -EINVAL;
5144
Trond Myklebustd9531262009-07-21 19:22:38 -04005145 if (request->fl_type == F_UNLCK) {
5146 if (state != NULL)
5147 return nfs4_proc_unlck(state, cmd, request);
5148 return 0;
5149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
Trond Myklebustd9531262009-07-21 19:22:38 -04005151 if (state == NULL)
5152 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005153 /*
5154 * Don't rely on the VFS having checked the file open mode,
5155 * since it won't do this for flock() locks.
5156 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005157 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005158 case F_RDLCK:
5159 if (!(filp->f_mode & FMODE_READ))
5160 return -EBADF;
5161 break;
5162 case F_WRLCK:
5163 if (!(filp->f_mode & FMODE_WRITE))
5164 return -EBADF;
5165 }
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 do {
5168 status = nfs4_proc_setlk(state, cmd, request);
5169 if ((status != -EAGAIN) || IS_SETLK(cmd))
5170 break;
5171 timeout = nfs4_set_lock_task_retry(timeout);
5172 status = -ERESTARTSYS;
5173 if (signalled())
5174 break;
5175 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return status;
5177}
5178
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005179int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005180{
5181 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005182 int err;
5183
5184 err = nfs4_set_lock_state(state, fl);
5185 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005186 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005187 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005188 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005189}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005190
Trond Myklebustcf470c32012-03-07 13:49:12 -05005191struct nfs_release_lockowner_data {
5192 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005193 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005194 struct nfs_release_lockowner_args args;
5195};
5196
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005197static void nfs4_release_lockowner_release(void *calldata)
5198{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005199 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005200 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005201 kfree(calldata);
5202}
5203
Trond Myklebust17280172012-03-11 13:11:00 -04005204static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005205 .rpc_release = nfs4_release_lockowner_release,
5206};
5207
Trond Myklebustcf470c32012-03-07 13:49:12 -05005208int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005209{
5210 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005211 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005212 struct rpc_message msg = {
5213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5214 };
5215
5216 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005217 return -EINVAL;
5218 data = kmalloc(sizeof(*data), GFP_NOFS);
5219 if (!data)
5220 return -ENOMEM;
5221 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005222 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005223 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5224 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5225 data->args.lock_owner.s_dev = server->s_dev;
5226 msg.rpc_argp = &data->args;
5227 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5228 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005229}
5230
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005231#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5232
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005233static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5234 const void *buf, size_t buflen,
5235 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005236{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005237 if (strcmp(key, "") != 0)
5238 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005239
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005240 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005241}
5242
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005243static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5244 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005245{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005246 if (strcmp(key, "") != 0)
5247 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005248
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005249 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005250}
5251
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005252static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5253 size_t list_len, const char *name,
5254 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005255{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005256 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005257
J. Bruce Fields096455a2006-03-20 23:23:42 -05005258 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5259 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005260
5261 if (list && len <= list_len)
5262 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005263 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005264}
5265
Andy Adamson533eb462011-06-13 18:25:56 -04005266/*
5267 * nfs_fhget will use either the mounted_on_fileid or the fileid
5268 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005269static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5270{
Andy Adamson533eb462011-06-13 18:25:56 -04005271 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5272 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5273 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005274 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005275 return;
5276
5277 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005278 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005279 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5280 fattr->nlink = 2;
5281}
5282
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005283static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5284 const struct qstr *name,
5285 struct nfs4_fs_locations *fs_locations,
5286 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005287{
5288 struct nfs_server *server = NFS_SERVER(dir);
5289 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005290 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005291 };
5292 struct nfs4_fs_locations_arg args = {
5293 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005294 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005295 .page = page,
5296 .bitmask = bitmask,
5297 };
Benny Halevy22958462009-04-01 09:22:02 -04005298 struct nfs4_fs_locations_res res = {
5299 .fs_locations = fs_locations,
5300 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005301 struct rpc_message msg = {
5302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5303 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005304 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005305 };
5306 int status;
5307
Harvey Harrison3110ff82008-05-02 13:42:44 -07005308 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005309
5310 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5311 * is not supported */
5312 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5313 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5314 else
5315 bitmask[0] |= FATTR4_WORD0_FILEID;
5316
Trond Myklebustc228fd32007-01-13 02:28:11 -05005317 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005318 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005319 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005320 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005321 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005322 return status;
5323}
5324
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005325int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5326 const struct qstr *name,
5327 struct nfs4_fs_locations *fs_locations,
5328 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005329{
5330 struct nfs4_exception exception = { };
5331 int err;
5332 do {
5333 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005334 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005335 &exception);
5336 } while (exception.retry);
5337 return err;
5338}
5339
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005340static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5341{
5342 int status;
5343 struct nfs4_secinfo_arg args = {
5344 .dir_fh = NFS_FH(dir),
5345 .name = name,
5346 };
5347 struct nfs4_secinfo_res res = {
5348 .flavors = flavors,
5349 };
5350 struct rpc_message msg = {
5351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5352 .rpc_argp = &args,
5353 .rpc_resp = &res,
5354 };
5355
5356 dprintk("NFS call secinfo %s\n", name->name);
5357 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5358 dprintk("NFS reply secinfo: %d\n", status);
5359 return status;
5360}
5361
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005362int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5363 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005364{
5365 struct nfs4_exception exception = { };
5366 int err;
5367 do {
5368 err = nfs4_handle_exception(NFS_SERVER(dir),
5369 _nfs4_proc_secinfo(dir, name, flavors),
5370 &exception);
5371 } while (exception.retry);
5372 return err;
5373}
5374
Andy Adamson557134a2009-04-01 09:21:53 -04005375#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005376/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005377 * Check the exchange flags returned by the server for invalid flags, having
5378 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5379 * DS flags set.
5380 */
5381static int nfs4_check_cl_exchange_flags(u32 flags)
5382{
5383 if (flags & ~EXCHGID4_FLAG_MASK_R)
5384 goto out_inval;
5385 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5386 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5387 goto out_inval;
5388 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5389 goto out_inval;
5390 return NFS_OK;
5391out_inval:
5392 return -NFS4ERR_INVAL;
5393}
5394
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005395static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005396nfs41_same_server_scope(struct nfs41_server_scope *a,
5397 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005398{
5399 if (a->server_scope_sz == b->server_scope_sz &&
5400 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5401 return true;
5402
5403 return false;
5404}
5405
Andy Adamson357f54d2010-12-14 10:11:57 -05005406/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005407 * nfs4_proc_bind_conn_to_session()
5408 *
5409 * The 4.1 client currently uses the same TCP connection for the
5410 * fore and backchannel.
5411 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005412int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005413{
5414 int status;
5415 struct nfs41_bind_conn_to_session_res res;
5416 struct rpc_message msg = {
5417 .rpc_proc =
5418 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5419 .rpc_argp = clp,
5420 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005421 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005422 };
5423
5424 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005425
5426 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5427 if (unlikely(res.session == NULL)) {
5428 status = -ENOMEM;
5429 goto out;
5430 }
5431
5432 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5433 if (status == 0) {
5434 if (memcmp(res.session->sess_id.data,
5435 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5436 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5437 status = -EIO;
5438 goto out_session;
5439 }
5440 if (res.dir != NFS4_CDFS4_BOTH) {
5441 dprintk("NFS: %s: Unexpected direction from server\n",
5442 __func__);
5443 status = -EIO;
5444 goto out_session;
5445 }
5446 if (res.use_conn_in_rdma_mode) {
5447 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5448 __func__);
5449 status = -EIO;
5450 goto out_session;
5451 }
5452 }
5453out_session:
5454 kfree(res.session);
5455out:
5456 dprintk("<-- %s status= %d\n", __func__, status);
5457 return status;
5458}
5459
5460/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005461 * nfs4_proc_exchange_id()
5462 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005463 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5464 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005465 * Since the clientid has expired, all compounds using sessions
5466 * associated with the stale clientid will be returning
5467 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5468 * be in some phase of session reset.
5469 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005470int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005471{
5472 nfs4_verifier verifier;
5473 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005474 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005475 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005476 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005477 };
5478 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005479 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005480 };
5481 int status;
5482 struct rpc_message msg = {
5483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5484 .rpc_argp = &args,
5485 .rpc_resp = &res,
5486 .rpc_cred = cred,
5487 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005488
Chuck Leverf0920752012-05-21 22:45:41 -04005489 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005490 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5491 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005492 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5493 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5494 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005495
Chuck Leveracdeb692012-05-21 22:46:16 -04005496 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005497 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005498 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005499 status = -ENOMEM;
5500 goto out;
5501 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005502
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005503 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005504 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005505 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005506 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005507 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005508 }
5509
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005510 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005511 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005512 status = -ENOMEM;
5513 goto out_server_scope;
5514 }
5515
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005517 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005518 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005519
Chuck Lever177313f2012-05-21 22:44:58 -04005520 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005521 clp->cl_clientid = res.clientid;
5522 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5523 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5524 clp->cl_seqid = res.seqid;
5525
Chuck Leveracdeb692012-05-21 22:46:16 -04005526 kfree(clp->cl_serverowner);
5527 clp->cl_serverowner = res.server_owner;
5528 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005529
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005530 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005531 kfree(clp->cl_implid);
5532 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005533
Chuck Lever177313f2012-05-21 22:44:58 -04005534 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005535 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005536 res.server_scope)) {
5537 dprintk("%s: server_scope mismatch detected\n",
5538 __func__);
5539 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005540 kfree(clp->cl_serverscope);
5541 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005542 }
5543
Chuck Lever177313f2012-05-21 22:44:58 -04005544 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005545 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005546 goto out;
5547 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005548 } else
5549 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005550
Chuck Leveracdeb692012-05-21 22:46:16 -04005551out_server_owner:
5552 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005553out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005554 kfree(res.server_scope);
5555out:
Chuck Lever177313f2012-05-21 22:44:58 -04005556 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005557 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005558 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005559 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005560 clp->cl_implid->date.seconds,
5561 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005562 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005563 return status;
5564}
5565
Trond Myklebust66245532012-05-25 17:18:09 -04005566static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5567 struct rpc_cred *cred)
5568{
5569 struct rpc_message msg = {
5570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5571 .rpc_argp = clp,
5572 .rpc_cred = cred,
5573 };
5574 int status;
5575
5576 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5577 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005578 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005579 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5580 return status;
5581}
5582
5583static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5584 struct rpc_cred *cred)
5585{
5586 unsigned int loop;
5587 int ret;
5588
5589 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5590 ret = _nfs4_proc_destroy_clientid(clp, cred);
5591 switch (ret) {
5592 case -NFS4ERR_DELAY:
5593 case -NFS4ERR_CLIENTID_BUSY:
5594 ssleep(1);
5595 break;
5596 default:
5597 return ret;
5598 }
5599 }
5600 return 0;
5601}
5602
5603int nfs4_destroy_clientid(struct nfs_client *clp)
5604{
5605 struct rpc_cred *cred;
5606 int ret = 0;
5607
5608 if (clp->cl_mvops->minor_version < 1)
5609 goto out;
5610 if (clp->cl_exchange_flags == 0)
5611 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005612 if (clp->cl_preserve_clid)
5613 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005614 cred = nfs4_get_exchange_id_cred(clp);
5615 ret = nfs4_proc_destroy_clientid(clp, cred);
5616 if (cred)
5617 put_rpccred(cred);
5618 switch (ret) {
5619 case 0:
5620 case -NFS4ERR_STALE_CLIENTID:
5621 clp->cl_exchange_flags = 0;
5622 }
5623out:
5624 return ret;
5625}
5626
Andy Adamson2050f0c2009-04-01 09:22:30 -04005627struct nfs4_get_lease_time_data {
5628 struct nfs4_get_lease_time_args *args;
5629 struct nfs4_get_lease_time_res *res;
5630 struct nfs_client *clp;
5631};
5632
5633static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5634 void *calldata)
5635{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005636 struct nfs4_get_lease_time_data *data =
5637 (struct nfs4_get_lease_time_data *)calldata;
5638
5639 dprintk("--> %s\n", __func__);
5640 /* just setup sequence, do not trigger session recovery
5641 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005642 nfs41_setup_sequence(data->clp->cl_session,
5643 &data->args->la_seq_args,
5644 &data->res->lr_seq_res,
5645 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005646 dprintk("<-- %s\n", __func__);
5647}
5648
5649/*
5650 * Called from nfs4_state_manager thread for session setup, so don't recover
5651 * from sequence operation or clientid errors.
5652 */
5653static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5654{
5655 struct nfs4_get_lease_time_data *data =
5656 (struct nfs4_get_lease_time_data *)calldata;
5657
5658 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005659 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5660 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005661 switch (task->tk_status) {
5662 case -NFS4ERR_DELAY:
5663 case -NFS4ERR_GRACE:
5664 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5665 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5666 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005667 /* fall through */
5668 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005669 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005670 return;
5671 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005672 dprintk("<-- %s\n", __func__);
5673}
5674
Trond Myklebust17280172012-03-11 13:11:00 -04005675static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005676 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5677 .rpc_call_done = nfs4_get_lease_time_done,
5678};
5679
5680int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5681{
5682 struct rpc_task *task;
5683 struct nfs4_get_lease_time_args args;
5684 struct nfs4_get_lease_time_res res = {
5685 .lr_fsinfo = fsinfo,
5686 };
5687 struct nfs4_get_lease_time_data data = {
5688 .args = &args,
5689 .res = &res,
5690 .clp = clp,
5691 };
5692 struct rpc_message msg = {
5693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5694 .rpc_argp = &args,
5695 .rpc_resp = &res,
5696 };
5697 struct rpc_task_setup task_setup = {
5698 .rpc_client = clp->cl_rpcclient,
5699 .rpc_message = &msg,
5700 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005701 .callback_data = &data,
5702 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005703 };
5704 int status;
5705
Trond Myklebust9d12b212012-01-17 22:04:25 -05005706 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005707 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005708 dprintk("--> %s\n", __func__);
5709 task = rpc_run_task(&task_setup);
5710
5711 if (IS_ERR(task))
5712 status = PTR_ERR(task);
5713 else {
5714 status = task->tk_status;
5715 rpc_put_task(task);
5716 }
5717 dprintk("<-- %s return %d\n", __func__, status);
5718
5719 return status;
5720}
5721
Andy Adamsonfc931582009-04-01 09:22:31 -04005722/*
5723 * Initialize the values to be used by the client in CREATE_SESSION
5724 * If nfs4_init_session set the fore channel request and response sizes,
5725 * use them.
5726 *
5727 * Set the back channel max_resp_sz_cached to zero to force the client to
5728 * always set csa_cachethis to FALSE because the current implementation
5729 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5730 */
5731static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5732{
5733 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005734 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5735 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005736
5737 if (mxrqst_sz == 0)
5738 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5739 if (mxresp_sz == 0)
5740 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5741 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005742 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5743 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005744 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005745 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005746
5747 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005748 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005749 __func__,
5750 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005751 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005752
5753 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005754 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5755 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5756 args->bc_attrs.max_resp_sz_cached = 0;
5757 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5758 args->bc_attrs.max_reqs = 1;
5759
5760 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5761 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5762 __func__,
5763 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5764 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5765 args->bc_attrs.max_reqs);
5766}
5767
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005768static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005769{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005770 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5771 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5772
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005773 if (rcvd->max_resp_sz > sent->max_resp_sz)
5774 return -EINVAL;
5775 /*
5776 * Our requested max_ops is the minimum we need; we're not
5777 * prepared to break up compounds into smaller pieces than that.
5778 * So, no point even trying to continue if the server won't
5779 * cooperate:
5780 */
5781 if (rcvd->max_ops < sent->max_ops)
5782 return -EINVAL;
5783 if (rcvd->max_reqs == 0)
5784 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005785 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5786 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005787 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005788}
5789
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005790static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5791{
5792 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5793 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005794
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005795 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5796 return -EINVAL;
5797 if (rcvd->max_resp_sz < sent->max_resp_sz)
5798 return -EINVAL;
5799 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5800 return -EINVAL;
5801 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005802 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005803 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005804 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005805 return -EINVAL;
5806 return 0;
5807}
Andy Adamson8d353012009-04-01 09:22:32 -04005808
Andy Adamson8d353012009-04-01 09:22:32 -04005809static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5810 struct nfs4_session *session)
5811{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005812 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005813
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005814 ret = nfs4_verify_fore_channel_attrs(args, session);
5815 if (ret)
5816 return ret;
5817 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005818}
5819
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005820static int _nfs4_proc_create_session(struct nfs_client *clp,
5821 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005822{
5823 struct nfs4_session *session = clp->cl_session;
5824 struct nfs41_create_session_args args = {
5825 .client = clp,
5826 .cb_program = NFS4_CALLBACK,
5827 };
5828 struct nfs41_create_session_res res = {
5829 .client = clp,
5830 };
5831 struct rpc_message msg = {
5832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5833 .rpc_argp = &args,
5834 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005835 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005836 };
5837 int status;
5838
5839 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005840 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005841
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005842 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005843
Trond Myklebust43095d32012-11-20 11:13:12 -05005844 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005845 /* Verify the session's negotiated channel_attrs values */
5846 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005847 /* Increment the clientid slot sequence id */
5848 clp->cl_seqid++;
5849 }
5850
5851 return status;
5852}
5853
5854/*
5855 * Issues a CREATE_SESSION operation to the server.
5856 * It is the responsibility of the caller to verify the session is
5857 * expired before calling this routine.
5858 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005859int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005860{
5861 int status;
5862 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005863 struct nfs4_session *session = clp->cl_session;
5864
5865 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5866
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005867 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005868 if (status)
5869 goto out;
5870
Andy Adamsonaacd5532011-11-09 13:58:21 -05005871 /* Init or reset the session slot tables */
5872 status = nfs4_setup_session_slot_tables(session);
5873 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005874 if (status)
5875 goto out;
5876
5877 ptr = (unsigned *)&session->sess_id.data[0];
5878 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5879 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005880out:
5881 dprintk("<-- %s\n", __func__);
5882 return status;
5883}
5884
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005885/*
5886 * Issue the over-the-wire RPC DESTROY_SESSION.
5887 * The caller must serialize access to this routine.
5888 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005889int nfs4_proc_destroy_session(struct nfs4_session *session,
5890 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005891{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5894 .rpc_argp = session,
5895 .rpc_cred = cred,
5896 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005897 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005898
5899 dprintk("--> nfs4_proc_destroy_session\n");
5900
5901 /* session is still being setup */
5902 if (session->clp->cl_cons_state != NFS_CS_READY)
5903 return status;
5904
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005905 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005906
5907 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005908 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005909 "Session has been destroyed regardless...\n", status);
5910
5911 dprintk("<-- nfs4_proc_destroy_session\n");
5912 return status;
5913}
5914
Trond Myklebust7b38c362012-05-23 13:23:31 -04005915/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005916 * Renew the cl_session lease.
5917 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005918struct nfs4_sequence_data {
5919 struct nfs_client *clp;
5920 struct nfs4_sequence_args args;
5921 struct nfs4_sequence_res res;
5922};
5923
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005924static void nfs41_sequence_release(void *data)
5925{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005926 struct nfs4_sequence_data *calldata = data;
5927 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005928
Alexandros Batsakis71358402010-02-05 03:45:05 -08005929 if (atomic_read(&clp->cl_count) > 1)
5930 nfs4_schedule_state_renewal(clp);
5931 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005932 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005933}
5934
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005935static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5936{
5937 switch(task->tk_status) {
5938 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5940 return -EAGAIN;
5941 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005942 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005943 }
5944 return 0;
5945}
5946
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005947static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005948{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005949 struct nfs4_sequence_data *calldata = data;
5950 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005951
Trond Myklebust14516c32010-07-31 14:29:06 -04005952 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5953 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005954
5955 if (task->tk_status < 0) {
5956 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005957 if (atomic_read(&clp->cl_count) == 1)
5958 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005959
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005960 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5961 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005962 return;
5963 }
5964 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005965 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005966out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005967 dprintk("<-- %s\n", __func__);
5968}
5969
5970static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5971{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005972 struct nfs4_sequence_data *calldata = data;
5973 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005974 struct nfs4_sequence_args *args;
5975 struct nfs4_sequence_res *res;
5976
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977 args = task->tk_msg.rpc_argp;
5978 res = task->tk_msg.rpc_resp;
5979
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005980 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005981}
5982
5983static const struct rpc_call_ops nfs41_sequence_ops = {
5984 .rpc_call_done = nfs41_sequence_call_done,
5985 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005986 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005987};
5988
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005989static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5990 struct rpc_cred *cred,
5991 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005992{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005993 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005994 struct rpc_message msg = {
5995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5996 .rpc_cred = cred,
5997 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005998 struct rpc_task_setup task_setup_data = {
5999 .rpc_client = clp->cl_rpcclient,
6000 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006001 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006002 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006003 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006004
Alexandros Batsakis71358402010-02-05 03:45:05 -08006005 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006006 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006007 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006008 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006009 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006010 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006011 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006012 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006013 if (is_privileged)
6014 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006015 msg.rpc_argp = &calldata->args;
6016 msg.rpc_resp = &calldata->res;
6017 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006018 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006019
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006020 return rpc_run_task(&task_setup_data);
6021}
6022
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006023static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024{
6025 struct rpc_task *task;
6026 int ret = 0;
6027
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006028 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6029 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006030 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006031 if (IS_ERR(task))
6032 ret = PTR_ERR(task);
6033 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006034 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006035 dprintk("<-- %s status=%d\n", __func__, ret);
6036 return ret;
6037}
6038
6039static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6040{
6041 struct rpc_task *task;
6042 int ret;
6043
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006044 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006045 if (IS_ERR(task)) {
6046 ret = PTR_ERR(task);
6047 goto out;
6048 }
6049 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006050 if (!ret) {
6051 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6052
6053 if (task->tk_status == 0)
6054 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006055 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006056 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006057 rpc_put_task(task);
6058out:
6059 dprintk("<-- %s status=%d\n", __func__, ret);
6060 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006061}
6062
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006063struct nfs4_reclaim_complete_data {
6064 struct nfs_client *clp;
6065 struct nfs41_reclaim_complete_args arg;
6066 struct nfs41_reclaim_complete_res res;
6067};
6068
6069static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6070{
6071 struct nfs4_reclaim_complete_data *calldata = data;
6072
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006073 nfs41_setup_sequence(calldata->clp->cl_session,
6074 &calldata->arg.seq_args,
6075 &calldata->res.seq_res,
6076 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006077}
6078
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006079static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6080{
6081 switch(task->tk_status) {
6082 case 0:
6083 case -NFS4ERR_COMPLETE_ALREADY:
6084 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6085 break;
6086 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006087 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006088 /* fall through */
6089 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006090 return -EAGAIN;
6091 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006092 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006093 }
6094 return 0;
6095}
6096
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006097static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6098{
6099 struct nfs4_reclaim_complete_data *calldata = data;
6100 struct nfs_client *clp = calldata->clp;
6101 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6102
6103 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006104 if (!nfs41_sequence_done(task, res))
6105 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006106
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006107 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6108 rpc_restart_call_prepare(task);
6109 return;
6110 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006111 dprintk("<-- %s\n", __func__);
6112}
6113
6114static void nfs4_free_reclaim_complete_data(void *data)
6115{
6116 struct nfs4_reclaim_complete_data *calldata = data;
6117
6118 kfree(calldata);
6119}
6120
6121static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6122 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6123 .rpc_call_done = nfs4_reclaim_complete_done,
6124 .rpc_release = nfs4_free_reclaim_complete_data,
6125};
6126
6127/*
6128 * Issue a global reclaim complete.
6129 */
6130static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6131{
6132 struct nfs4_reclaim_complete_data *calldata;
6133 struct rpc_task *task;
6134 struct rpc_message msg = {
6135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6136 };
6137 struct rpc_task_setup task_setup_data = {
6138 .rpc_client = clp->cl_rpcclient,
6139 .rpc_message = &msg,
6140 .callback_ops = &nfs4_reclaim_complete_call_ops,
6141 .flags = RPC_TASK_ASYNC,
6142 };
6143 int status = -ENOMEM;
6144
6145 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006146 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006147 if (calldata == NULL)
6148 goto out;
6149 calldata->clp = clp;
6150 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006151
Trond Myklebust9d12b212012-01-17 22:04:25 -05006152 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006153 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006154 msg.rpc_argp = &calldata->arg;
6155 msg.rpc_resp = &calldata->res;
6156 task_setup_data.callback_data = calldata;
6157 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006158 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006159 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006160 goto out;
6161 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006162 status = nfs4_wait_for_completion_rpc_task(task);
6163 if (status == 0)
6164 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006166 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006167out:
6168 dprintk("<-- %s status=%d\n", __func__, status);
6169 return status;
6170}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006171
6172static void
6173nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6174{
6175 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006176 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006177 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006178
6179 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006180 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6181 * right now covering the LAYOUTGET we are about to send.
6182 * However, that is not so catastrophic, and there seems
6183 * to be no way to prevent it completely.
6184 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006185 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006186 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006187 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006188 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6189 NFS_I(lgp->args.inode)->layout,
6190 lgp->args.ctx->state)) {
6191 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006192 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006193}
6194
6195static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6196{
6197 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006198 struct inode *inode = lgp->args.inode;
6199 struct nfs_server *server = NFS_SERVER(inode);
6200 struct pnfs_layout_hdr *lo;
6201 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006202 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006203
6204 dprintk("--> %s\n", __func__);
6205
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006206 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006207 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006208
6209 switch (task->tk_status) {
6210 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006211 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006212 case -NFS4ERR_LAYOUTTRYLATER:
6213 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006214 timeo = rpc_get_timeout(task->tk_client);
6215 giveup = lgp->args.timestamp + timeo;
6216 if (time_after(giveup, jiffies))
6217 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006218 break;
6219 case -NFS4ERR_EXPIRED:
6220 case -NFS4ERR_BAD_STATEID:
6221 spin_lock(&inode->i_lock);
6222 lo = NFS_I(inode)->layout;
6223 if (!lo || list_empty(&lo->plh_segs)) {
6224 spin_unlock(&inode->i_lock);
6225 /* If the open stateid was bad, then recover it. */
6226 state = lgp->args.ctx->state;
6227 } else {
6228 LIST_HEAD(head);
6229
6230 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6231 spin_unlock(&inode->i_lock);
6232 /* Mark the bad layout state as invalid, then
6233 * retry using the open stateid. */
6234 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006235 }
6236 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006237 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6238 rpc_restart_call_prepare(task);
6239out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006240 dprintk("<-- %s\n", __func__);
6241}
6242
Idan Kedar85541162012-08-02 11:47:10 +03006243static size_t max_response_pages(struct nfs_server *server)
6244{
6245 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6246 return nfs_page_array_len(0, max_resp_sz);
6247}
6248
6249static void nfs4_free_pages(struct page **pages, size_t size)
6250{
6251 int i;
6252
6253 if (!pages)
6254 return;
6255
6256 for (i = 0; i < size; i++) {
6257 if (!pages[i])
6258 break;
6259 __free_page(pages[i]);
6260 }
6261 kfree(pages);
6262}
6263
6264static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6265{
6266 struct page **pages;
6267 int i;
6268
6269 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6270 if (!pages) {
6271 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6272 return NULL;
6273 }
6274
6275 for (i = 0; i < size; i++) {
6276 pages[i] = alloc_page(gfp_flags);
6277 if (!pages[i]) {
6278 dprintk("%s: failed to allocate page\n", __func__);
6279 nfs4_free_pages(pages, size);
6280 return NULL;
6281 }
6282 }
6283
6284 return pages;
6285}
6286
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006287static void nfs4_layoutget_release(void *calldata)
6288{
6289 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006290 struct inode *inode = lgp->args.inode;
6291 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006292 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006293
6294 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006295 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006296 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006297 put_nfs_open_context(lgp->args.ctx);
6298 kfree(calldata);
6299 dprintk("<-- %s\n", __func__);
6300}
6301
6302static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6303 .rpc_call_prepare = nfs4_layoutget_prepare,
6304 .rpc_call_done = nfs4_layoutget_done,
6305 .rpc_release = nfs4_layoutget_release,
6306};
6307
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006308struct pnfs_layout_segment *
6309nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006310{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006311 struct inode *inode = lgp->args.inode;
6312 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006313 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006314 struct rpc_task *task;
6315 struct rpc_message msg = {
6316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6317 .rpc_argp = &lgp->args,
6318 .rpc_resp = &lgp->res,
6319 };
6320 struct rpc_task_setup task_setup_data = {
6321 .rpc_client = server->client,
6322 .rpc_message = &msg,
6323 .callback_ops = &nfs4_layoutget_call_ops,
6324 .callback_data = lgp,
6325 .flags = RPC_TASK_ASYNC,
6326 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006327 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006328 int status = 0;
6329
6330 dprintk("--> %s\n", __func__);
6331
Idan Kedar85541162012-08-02 11:47:10 +03006332 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6333 if (!lgp->args.layout.pages) {
6334 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006335 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006336 }
6337 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006338 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006339
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006340 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006341 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006342 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006343
6344 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6345 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6346
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006347 task = rpc_run_task(&task_setup_data);
6348 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006349 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006350 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006351 if (status == 0)
6352 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006353 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6354 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006355 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006356 rpc_put_task(task);
6357 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006358 if (status)
6359 return ERR_PTR(status);
6360 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006361}
6362
Benny Halevycbe82602011-05-22 19:52:37 +03006363static void
6364nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6365{
6366 struct nfs4_layoutreturn *lrp = calldata;
6367
6368 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006369 nfs41_setup_sequence(lrp->clp->cl_session,
6370 &lrp->args.seq_args,
6371 &lrp->res.seq_res,
6372 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006373}
6374
6375static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6376{
6377 struct nfs4_layoutreturn *lrp = calldata;
6378 struct nfs_server *server;
6379
6380 dprintk("--> %s\n", __func__);
6381
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006382 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006383 return;
6384
6385 server = NFS_SERVER(lrp->args.inode);
6386 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006387 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006388 return;
6389 }
Benny Halevycbe82602011-05-22 19:52:37 +03006390 dprintk("<-- %s\n", __func__);
6391}
6392
6393static void nfs4_layoutreturn_release(void *calldata)
6394{
6395 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006396 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006397
6398 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006399 spin_lock(&lo->plh_inode->i_lock);
6400 if (lrp->res.lrs_present)
6401 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6402 lo->plh_block_lgets--;
6403 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006404 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006405 kfree(calldata);
6406 dprintk("<-- %s\n", __func__);
6407}
6408
6409static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6410 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6411 .rpc_call_done = nfs4_layoutreturn_done,
6412 .rpc_release = nfs4_layoutreturn_release,
6413};
6414
6415int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6416{
6417 struct rpc_task *task;
6418 struct rpc_message msg = {
6419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6420 .rpc_argp = &lrp->args,
6421 .rpc_resp = &lrp->res,
6422 };
6423 struct rpc_task_setup task_setup_data = {
6424 .rpc_client = lrp->clp->cl_rpcclient,
6425 .rpc_message = &msg,
6426 .callback_ops = &nfs4_layoutreturn_call_ops,
6427 .callback_data = lrp,
6428 };
6429 int status;
6430
6431 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006432 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006433 task = rpc_run_task(&task_setup_data);
6434 if (IS_ERR(task))
6435 return PTR_ERR(task);
6436 status = task->tk_status;
6437 dprintk("<-- %s status=%d\n", __func__, status);
6438 rpc_put_task(task);
6439 return status;
6440}
6441
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006442/*
6443 * Retrieve the list of Data Server devices from the MDS.
6444 */
6445static int _nfs4_getdevicelist(struct nfs_server *server,
6446 const struct nfs_fh *fh,
6447 struct pnfs_devicelist *devlist)
6448{
6449 struct nfs4_getdevicelist_args args = {
6450 .fh = fh,
6451 .layoutclass = server->pnfs_curr_ld->id,
6452 };
6453 struct nfs4_getdevicelist_res res = {
6454 .devlist = devlist,
6455 };
6456 struct rpc_message msg = {
6457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6458 .rpc_argp = &args,
6459 .rpc_resp = &res,
6460 };
6461 int status;
6462
6463 dprintk("--> %s\n", __func__);
6464 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6465 &res.seq_res, 0);
6466 dprintk("<-- %s status=%d\n", __func__, status);
6467 return status;
6468}
6469
6470int nfs4_proc_getdevicelist(struct nfs_server *server,
6471 const struct nfs_fh *fh,
6472 struct pnfs_devicelist *devlist)
6473{
6474 struct nfs4_exception exception = { };
6475 int err;
6476
6477 do {
6478 err = nfs4_handle_exception(server,
6479 _nfs4_getdevicelist(server, fh, devlist),
6480 &exception);
6481 } while (exception.retry);
6482
6483 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6484 err, devlist->num_devs);
6485
6486 return err;
6487}
6488EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6489
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006490static int
6491_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6492{
6493 struct nfs4_getdeviceinfo_args args = {
6494 .pdev = pdev,
6495 };
6496 struct nfs4_getdeviceinfo_res res = {
6497 .pdev = pdev,
6498 };
6499 struct rpc_message msg = {
6500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6501 .rpc_argp = &args,
6502 .rpc_resp = &res,
6503 };
6504 int status;
6505
6506 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006507 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006508 dprintk("<-- %s status=%d\n", __func__, status);
6509
6510 return status;
6511}
6512
6513int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6514{
6515 struct nfs4_exception exception = { };
6516 int err;
6517
6518 do {
6519 err = nfs4_handle_exception(server,
6520 _nfs4_proc_getdeviceinfo(server, pdev),
6521 &exception);
6522 } while (exception.retry);
6523 return err;
6524}
6525EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6526
Andy Adamson863a3c62011-03-23 13:27:54 +00006527static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6528{
6529 struct nfs4_layoutcommit_data *data = calldata;
6530 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006531 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006532
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006533 nfs41_setup_sequence(session,
6534 &data->args.seq_args,
6535 &data->res.seq_res,
6536 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006537}
6538
6539static void
6540nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6541{
6542 struct nfs4_layoutcommit_data *data = calldata;
6543 struct nfs_server *server = NFS_SERVER(data->args.inode);
6544
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006545 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006546 return;
6547
6548 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006549 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6550 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6551 case -NFS4ERR_BADLAYOUT: /* no layout */
6552 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006553 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006554 break;
6555 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006556 nfs_post_op_update_inode_force_wcc(data->args.inode,
6557 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006558 break;
6559 default:
6560 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6561 rpc_restart_call_prepare(task);
6562 return;
6563 }
6564 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006565}
6566
6567static void nfs4_layoutcommit_release(void *calldata)
6568{
6569 struct nfs4_layoutcommit_data *data = calldata;
6570
Andy Adamsondb29c082011-07-30 20:52:38 -04006571 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006572 put_rpccred(data->cred);
6573 kfree(data);
6574}
6575
6576static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6577 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6578 .rpc_call_done = nfs4_layoutcommit_done,
6579 .rpc_release = nfs4_layoutcommit_release,
6580};
6581
6582int
Andy Adamsonef311532011-03-12 02:58:10 -05006583nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006584{
6585 struct rpc_message msg = {
6586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6587 .rpc_argp = &data->args,
6588 .rpc_resp = &data->res,
6589 .rpc_cred = data->cred,
6590 };
6591 struct rpc_task_setup task_setup_data = {
6592 .task = &data->task,
6593 .rpc_client = NFS_CLIENT(data->args.inode),
6594 .rpc_message = &msg,
6595 .callback_ops = &nfs4_layoutcommit_ops,
6596 .callback_data = data,
6597 .flags = RPC_TASK_ASYNC,
6598 };
6599 struct rpc_task *task;
6600 int status = 0;
6601
6602 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6603 "lbw: %llu inode %lu\n",
6604 data->task.tk_pid, sync,
6605 data->args.lastbytewritten,
6606 data->args.inode->i_ino);
6607
Trond Myklebust9d12b212012-01-17 22:04:25 -05006608 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006609 task = rpc_run_task(&task_setup_data);
6610 if (IS_ERR(task))
6611 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006612 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006613 goto out;
6614 status = nfs4_wait_for_completion_rpc_task(task);
6615 if (status != 0)
6616 goto out;
6617 status = task->tk_status;
6618out:
6619 dprintk("%s: status %d\n", __func__, status);
6620 rpc_put_task(task);
6621 return status;
6622}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006623
6624static int
6625_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6626 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6627{
6628 struct nfs41_secinfo_no_name_args args = {
6629 .style = SECINFO_STYLE_CURRENT_FH,
6630 };
6631 struct nfs4_secinfo_res res = {
6632 .flavors = flavors,
6633 };
6634 struct rpc_message msg = {
6635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6636 .rpc_argp = &args,
6637 .rpc_resp = &res,
6638 };
6639 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6640}
6641
6642static int
6643nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6644 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6645{
6646 struct nfs4_exception exception = { };
6647 int err;
6648 do {
6649 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6650 switch (err) {
6651 case 0:
6652 case -NFS4ERR_WRONGSEC:
6653 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006654 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006655 default:
6656 err = nfs4_handle_exception(server, err, &exception);
6657 }
6658 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006659out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006660 return err;
6661}
6662
6663static int
6664nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6665 struct nfs_fsinfo *info)
6666{
6667 int err;
6668 struct page *page;
6669 rpc_authflavor_t flavor;
6670 struct nfs4_secinfo_flavors *flavors;
6671
6672 page = alloc_page(GFP_KERNEL);
6673 if (!page) {
6674 err = -ENOMEM;
6675 goto out;
6676 }
6677
6678 flavors = page_address(page);
6679 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6680
6681 /*
6682 * Fall back on "guess and check" method if
6683 * the server doesn't support SECINFO_NO_NAME
6684 */
6685 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6686 err = nfs4_find_root_sec(server, fhandle, info);
6687 goto out_freepage;
6688 }
6689 if (err)
6690 goto out_freepage;
6691
6692 flavor = nfs_find_best_sec(flavors);
6693 if (err == 0)
6694 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6695
6696out_freepage:
6697 put_page(page);
6698 if (err == -EACCES)
6699 return -EPERM;
6700out:
6701 return err;
6702}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006703
6704static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006705{
6706 int status;
6707 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006708 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006709 };
6710 struct nfs41_test_stateid_res res;
6711 struct rpc_message msg = {
6712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6713 .rpc_argp = &args,
6714 .rpc_resp = &res,
6715 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006716
Chuck Lever38527b12012-07-11 16:30:23 -04006717 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006718 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006719 nfs4_set_sequence_privileged(&args.seq_args);
6720 status = nfs4_call_sync_sequence(server->client, server, &msg,
6721 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006722 if (status != NFS_OK) {
6723 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006724 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006725 }
6726 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006727 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006728}
6729
Chuck Lever38527b12012-07-11 16:30:23 -04006730/**
6731 * nfs41_test_stateid - perform a TEST_STATEID operation
6732 *
6733 * @server: server / transport on which to perform the operation
6734 * @stateid: state ID to test
6735 *
6736 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6737 * Otherwise a negative NFS4ERR value is returned if the operation
6738 * failed or the state ID is not currently valid.
6739 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006740static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006741{
6742 struct nfs4_exception exception = { };
6743 int err;
6744 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006745 err = _nfs41_test_stateid(server, stateid);
6746 if (err != -NFS4ERR_DELAY)
6747 break;
6748 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006749 } while (exception.retry);
6750 return err;
6751}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006752
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006753static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006754{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006755 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006756 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006757 };
6758 struct nfs41_free_stateid_res res;
6759 struct rpc_message msg = {
6760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6761 .rpc_argp = &args,
6762 .rpc_resp = &res,
6763 };
Chuck Lever38527b12012-07-11 16:30:23 -04006764 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006765
Chuck Lever38527b12012-07-11 16:30:23 -04006766 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006767 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006768 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006769 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006770 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006771 dprintk("NFS reply free_stateid: %d\n", status);
6772 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006773}
6774
Chuck Lever38527b12012-07-11 16:30:23 -04006775/**
6776 * nfs41_free_stateid - perform a FREE_STATEID operation
6777 *
6778 * @server: server / transport on which to perform the operation
6779 * @stateid: state ID to release
6780 *
6781 * Returns NFS_OK if the server freed "stateid". Otherwise a
6782 * negative NFS4ERR value is returned.
6783 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006784static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006785{
6786 struct nfs4_exception exception = { };
6787 int err;
6788 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006789 err = _nfs4_free_stateid(server, stateid);
6790 if (err != -NFS4ERR_DELAY)
6791 break;
6792 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006793 } while (exception.retry);
6794 return err;
6795}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006796
6797static bool nfs41_match_stateid(const nfs4_stateid *s1,
6798 const nfs4_stateid *s2)
6799{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006800 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006801 return false;
6802
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006803 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006804 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006805 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006806 return true;
6807
6808 return false;
6809}
6810
Andy Adamson557134a2009-04-01 09:21:53 -04006811#endif /* CONFIG_NFS_V4_1 */
6812
Trond Myklebust36281ca2012-03-04 18:13:56 -05006813static bool nfs4_match_stateid(const nfs4_stateid *s1,
6814 const nfs4_stateid *s2)
6815{
Trond Myklebustf597c532012-03-04 18:13:56 -05006816 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006817}
6818
6819
Trond Myklebust17280172012-03-11 13:11:00 -04006820static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006821 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006822 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 .recover_open = nfs4_open_reclaim,
6824 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006825 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006826 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006827 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828};
6829
Andy Adamson591d71c2009-04-01 09:22:47 -04006830#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006831static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006832 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6833 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6834 .recover_open = nfs4_open_reclaim,
6835 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006836 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006837 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006838 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006839 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006840};
6841#endif /* CONFIG_NFS_V4_1 */
6842
Trond Myklebust17280172012-03-11 13:11:00 -04006843static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006844 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006845 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 .recover_open = nfs4_open_expired,
6847 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006848 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006849 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850};
6851
Andy Adamson591d71c2009-04-01 09:22:47 -04006852#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006853static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006854 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6855 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006856 .recover_open = nfs41_open_expired,
6857 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006858 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006859 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006860};
6861#endif /* CONFIG_NFS_V4_1 */
6862
Trond Myklebust17280172012-03-11 13:11:00 -04006863static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006864 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006865 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006866 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006867};
6868
6869#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006870static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006871 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006872 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006873 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006874};
6875#endif
6876
Trond Myklebust97dc1352010-06-16 09:52:26 -04006877static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6878 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006879 .init_caps = NFS_CAP_READDIRPLUS
6880 | NFS_CAP_ATOMIC_OPEN
6881 | NFS_CAP_CHANGE_ATTR
6882 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006883 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006884 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006885 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006886 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6887 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6888 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006889};
6890
6891#if defined(CONFIG_NFS_V4_1)
6892static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6893 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006894 .init_caps = NFS_CAP_READDIRPLUS
6895 | NFS_CAP_ATOMIC_OPEN
6896 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006897 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006898 | NFS_CAP_STATEID_NFSV41
6899 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006900 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006901 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006902 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006903 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6904 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6905 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006906};
6907#endif
6908
Trond Myklebust97dc1352010-06-16 09:52:26 -04006909const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6910 [0] = &nfs_v4_0_minor_ops,
6911#if defined(CONFIG_NFS_V4_1)
6912 [1] = &nfs_v4_1_minor_ops,
6913#endif
6914};
6915
Bryan Schumaker73a79702012-07-16 16:39:12 -04006916const struct inode_operations nfs4_dir_inode_operations = {
6917 .create = nfs_create,
6918 .lookup = nfs_lookup,
6919 .atomic_open = nfs_atomic_open,
6920 .link = nfs_link,
6921 .unlink = nfs_unlink,
6922 .symlink = nfs_symlink,
6923 .mkdir = nfs_mkdir,
6924 .rmdir = nfs_rmdir,
6925 .mknod = nfs_mknod,
6926 .rename = nfs_rename,
6927 .permission = nfs_permission,
6928 .getattr = nfs_getattr,
6929 .setattr = nfs_setattr,
6930 .getxattr = generic_getxattr,
6931 .setxattr = generic_setxattr,
6932 .listxattr = generic_listxattr,
6933 .removexattr = generic_removexattr,
6934};
6935
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006936static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006937 .permission = nfs_permission,
6938 .getattr = nfs_getattr,
6939 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006940 .getxattr = generic_getxattr,
6941 .setxattr = generic_setxattr,
6942 .listxattr = generic_listxattr,
6943 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006944};
6945
David Howells509de812006-08-22 20:06:11 -04006946const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 .version = 4, /* protocol version */
6948 .dentry_ops = &nfs4_dentry_operations,
6949 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006950 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006951 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006953 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006954 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 .getattr = nfs4_proc_getattr,
6956 .setattr = nfs4_proc_setattr,
6957 .lookup = nfs4_proc_lookup,
6958 .access = nfs4_proc_access,
6959 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 .create = nfs4_proc_create,
6961 .remove = nfs4_proc_remove,
6962 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006963 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 .unlink_done = nfs4_proc_unlink_done,
6965 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006966 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006967 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006968 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 .link = nfs4_proc_link,
6970 .symlink = nfs4_proc_symlink,
6971 .mkdir = nfs4_proc_mkdir,
6972 .rmdir = nfs4_proc_remove,
6973 .readdir = nfs4_proc_readdir,
6974 .mknod = nfs4_proc_mknod,
6975 .statfs = nfs4_proc_statfs,
6976 .fsinfo = nfs4_proc_fsinfo,
6977 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006978 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 .decode_dirent = nfs4_decode_dirent,
6980 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006981 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006982 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006983 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006985 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006986 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006987 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006989 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006990 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006992 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006993 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006994 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006995 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006996 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006997 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006998 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006999 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007000 .create_server = nfs4_create_server,
7001 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002};
7003
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007004static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7005 .prefix = XATTR_NAME_NFSV4_ACL,
7006 .list = nfs4_xattr_list_nfs4_acl,
7007 .get = nfs4_xattr_get_nfs4_acl,
7008 .set = nfs4_xattr_set_nfs4_acl,
7009};
7010
7011const struct xattr_handler *nfs4_xattr_handlers[] = {
7012 &nfs4_xattr_nfs4_acl_handler,
7013 NULL
7014};
7015
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016/*
7017 * Local variables:
7018 * c-basic-offset: 8
7019 * End:
7020 */