blob: 39849360f9f6069e1c6f9156f2948c3649149b12 [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 Myklebust24ac23a2006-01-03 09:55:11 +0100772 int rpc_status;
773 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774};
775
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400776static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
777 int err, struct nfs4_exception *exception)
778{
779 if (err != -EINVAL)
780 return false;
781 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
782 return false;
783 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
784 exception->retry = 1;
785 return true;
786}
787
788static enum open_claim_type4
789nfs4_map_atomic_open_claim(struct nfs_server *server,
790 enum open_claim_type4 claim)
791{
792 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
793 return claim;
794 switch (claim) {
795 default:
796 return claim;
797 case NFS4_OPEN_CLAIM_FH:
798 return NFS4_OPEN_CLAIM_NULL;
799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
800 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
801 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
802 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
803 }
804}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400805
806static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807{
808 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400812 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400820 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400821 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct inode *dir = parent->d_inode;
825 struct nfs_server *server = NFS_SERVER(dir);
826 struct nfs4_opendata *p;
827
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p == NULL)
830 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400831 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 if (p->o_arg.seqid == NULL)
833 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400834 nfs_sb_active(dentry->d_sb);
835 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 p->dir = parent;
837 p->owner = sp;
838 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700841 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
842 * will return permission denied for all bits until close */
843 if (!(flags & O_EXCL)) {
844 /* ask server to check for all possible rights as results
845 * are cached */
846 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
847 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700848 }
David Howells7539bba2006-08-22 20:06:09 -0400849 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400850 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
851 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 p->o_arg.server = server;
854 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust1549210f2012-06-05 09:16:47 -0400855 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400856 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
857 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400858 case NFS4_OPEN_CLAIM_NULL:
859 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
860 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
861 p->o_arg.fh = NFS_FH(dir);
862 break;
863 case NFS4_OPEN_CLAIM_PREVIOUS:
864 case NFS4_OPEN_CLAIM_FH:
865 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
866 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
867 p->o_arg.fh = NFS_FH(dentry->d_inode);
868 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500869 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500870 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400871
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500874
875 verf[0] = jiffies;
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100879 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400883 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100885 return p;
886err_free:
887 kfree(p);
888err:
889 dput(parent);
890 return NULL;
891}
892
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400893static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898
899 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400902 nfs4_put_state_owner(p->owner);
903 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400904 dput(p->dentry);
905 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500906 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400907 kfree(p);
908}
909
910static void nfs4_opendata_put(struct nfs4_opendata *p)
911{
912 if (p != NULL)
913 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100914}
915
Trond Myklebust06f814a2006-01-03 09:55:07 +0100916static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
917{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100918 int ret;
919
Trond Myklebust06f814a2006-01-03 09:55:07 +0100920 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925{
926 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927
Trond Myklebust536e43d2012-01-17 22:04:26 -0500928 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 goto out;
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 break;
935 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400938 break;
939 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 return ret;
945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500949 if (delegation == NULL)
950 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -0500955 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
956 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500957 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 return 1;
959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100962{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -0400981 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 case FMODE_READ:
984 set_bit(NFS_O_RDONLY_STATE, &state->flags);
985 break;
986 case FMODE_WRITE:
987 set_bit(NFS_O_WRONLY_STATE, &state->flags);
988 break;
989 case FMODE_READ|FMODE_WRITE:
990 set_bit(NFS_O_RDWR_STATE, &state->flags);
991 }
992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400995{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400996 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400998 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400999}
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001static 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 -07001002{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001003 /*
1004 * Protect the call to nfs4_state_set_mode_locked and
1005 * serialise the stateid update
1006 */
1007 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001008 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001009 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001010 set_bit(NFS_DELEGATED_STATE, &state->flags);
1011 }
1012 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001014 write_sequnlock(&state->seqlock);
1015 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001017 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static 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 -05001021{
1022 struct nfs_inode *nfsi = NFS_I(state->inode);
1023 struct nfs_delegation *deleg_cur;
1024 int ret = 0;
1025
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001027
1028 rcu_read_lock();
1029 deleg_cur = rcu_dereference(nfsi->delegation);
1030 if (deleg_cur == NULL)
1031 goto no_delegation;
1032
1033 spin_lock(&deleg_cur->lock);
1034 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001035 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001037 goto no_delegation_unlock;
1038
1039 if (delegation == NULL)
1040 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001041 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001042 goto no_delegation_unlock;
1043
Trond Myklebustb7391f42008-12-23 15:21:52 -05001044 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001046 ret = 1;
1047no_delegation_unlock:
1048 spin_unlock(&deleg_cur->lock);
1049no_delegation:
1050 rcu_read_unlock();
1051
1052 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001054 ret = 1;
1055 }
1056
1057 return ret;
1058}
1059
1060
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062{
1063 struct nfs_delegation *delegation;
1064
1065 rcu_read_lock();
1066 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 rcu_read_unlock();
1069 return;
1070 }
1071 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001072 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073}
1074
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076{
1077 struct nfs4_state *state = opendata->state;
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001080 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 nfs4_stateid stateid;
1083 int ret = -EAGAIN;
1084
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 if (can_open_cached(state, fmode, open_mode)) {
1089 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 goto out_return_state;
1092 }
1093 spin_unlock(&state->owner->so_lock);
1094 }
Trond Myklebust34310432008-12-23 15:21:38 -05001095 rcu_read_lock();
1096 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001097 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001098 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001100 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001102 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001104 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 if (ret != 0)
1106 goto out;
1107 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001108
1109 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113out:
1114 return ERR_PTR(ret);
1115out_return_state:
1116 atomic_inc(&state->count);
1117 return state;
1118}
1119
Andy Adamsone23008e2012-10-02 21:07:32 -04001120static void
1121nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1122{
1123 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1124 struct nfs_delegation *delegation;
1125 int delegation_flags = 0;
1126
1127 rcu_read_lock();
1128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1129 if (delegation)
1130 delegation_flags = delegation->flags;
1131 rcu_read_unlock();
1132 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1133 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1134 "returning a delegation for "
1135 "OPEN(CLAIM_DELEGATE_CUR)\n",
1136 clp->cl_hostname);
1137 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1138 nfs_inode_set_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 else
1142 nfs_inode_reclaim_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145}
1146
1147/*
1148 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1149 * and update the nfs4_state.
1150 */
1151static struct nfs4_state *
1152_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1153{
1154 struct inode *inode = data->state->inode;
1155 struct nfs4_state *state = data->state;
1156 int ret;
1157
1158 if (!data->rpc_done) {
1159 ret = data->rpc_status;
1160 goto err;
1161 }
1162
1163 ret = -ESTALE;
1164 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1165 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1166 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1167 goto err;
1168
1169 ret = -ENOMEM;
1170 state = nfs4_get_open_state(inode, data->owner);
1171 if (state == NULL)
1172 goto err;
1173
1174 ret = nfs_refresh_inode(inode, &data->f_attr);
1175 if (ret)
1176 goto err;
1177
1178 if (data->o_res.delegation_type != 0)
1179 nfs4_opendata_check_deleg(data, state);
1180 update_open_stateid(state, &data->o_res.stateid, NULL,
1181 data->o_arg.fmode);
1182
1183 return state;
1184err:
1185 return ERR_PTR(ret);
1186
1187}
1188
1189static struct nfs4_state *
1190_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001191{
1192 struct inode *inode;
1193 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001194 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001195
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001197 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001198 goto out;
1199 }
1200
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001201 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001202 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001203 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001205 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001206 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001207 goto err;
1208 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209 state = nfs4_get_open_state(inode, data->owner);
1210 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001211 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001212 if (data->o_res.delegation_type != 0)
1213 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001214 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001217out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001218 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001219 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001220err_put_inode:
1221 iput(inode);
1222err:
1223 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001224}
1225
Andy Adamsone23008e2012-10-02 21:07:32 -04001226static struct nfs4_state *
1227nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1228{
1229 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1230 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1231 return _nfs4_opendata_to_nfs4_state(data);
1232}
1233
Trond Myklebust864472e2006-01-03 09:55:15 +01001234static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1235{
1236 struct nfs_inode *nfsi = NFS_I(state->inode);
1237 struct nfs_open_context *ctx;
1238
1239 spin_lock(&state->inode->i_lock);
1240 list_for_each_entry(ctx, &nfsi->open_files, list) {
1241 if (ctx->state != state)
1242 continue;
1243 get_nfs_open_context(ctx);
1244 spin_unlock(&state->inode->i_lock);
1245 return ctx;
1246 }
1247 spin_unlock(&state->inode->i_lock);
1248 return ERR_PTR(-ENOENT);
1249}
1250
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1252 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001253{
1254 struct nfs4_opendata *opendata;
1255
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001256 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1257 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001258 if (opendata == NULL)
1259 return ERR_PTR(-ENOMEM);
1260 opendata->state = state;
1261 atomic_inc(&state->count);
1262 return opendata;
1263}
1264
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001266{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 int ret;
1269
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 opendata->o_arg.open_flags = 0;
1271 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001272 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1273 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1274 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001275 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 if (ret != 0)
1277 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001278 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001279 if (IS_ERR(newstate))
1280 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001281 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001282 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 return 0;
1284}
1285
1286static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1287{
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 int ret;
1290
1291 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001293 smp_rmb();
1294 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001295 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001296 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 if (ret != 0)
1298 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001299 if (newstate != state)
1300 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 }
1302 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001303 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001304 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 if (ret != 0)
1306 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001307 if (newstate != state)
1308 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 }
1310 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001311 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001312 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 if (ret != 0)
1314 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001315 if (newstate != state)
1316 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001318 /*
1319 * We may have performed cached opens for all three recoveries.
1320 * Check if we need to update the current stateid.
1321 */
1322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001323 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001324 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001325 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001326 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001327 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001328 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 return 0;
1330}
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332/*
1333 * OPEN_RECLAIM:
1334 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001336static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001338 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int status;
1342
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001343 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1344 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001345 if (IS_ERR(opendata))
1346 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001347 rcu_read_lock();
1348 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001349 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001350 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001351 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001352 opendata->o_arg.u.delegation_type = delegation_type;
1353 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001354 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return status;
1356}
1357
Trond Myklebust539cd032007-06-05 11:46:42 -04001358static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct nfs_server *server = NFS_SERVER(state->inode);
1361 struct nfs4_exception exception = { };
1362 int err;
1363 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001364 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001365 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1366 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001367 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001368 break;
1369 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 } while (exception.retry);
1371 return err;
1372}
1373
Trond Myklebust864472e2006-01-03 09:55:15 +01001374static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1375{
1376 struct nfs_open_context *ctx;
1377 int ret;
1378
1379 ctx = nfs4_state_find_open_context(state);
1380 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001381 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001382 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 put_nfs_open_context(ctx);
1384 return ret;
1385}
1386
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001387static 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 -07001388{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001389 switch (err) {
1390 default:
1391 printk(KERN_ERR "NFS: %s: unhandled error "
1392 "%d.\n", __func__, err);
1393 case 0:
1394 case -ENOENT:
1395 case -ESTALE:
1396 break;
1397 case -NFS4ERR_BADSESSION:
1398 case -NFS4ERR_BADSLOT:
1399 case -NFS4ERR_BAD_HIGH_SLOT:
1400 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1401 case -NFS4ERR_DEADSESSION:
1402 set_bit(NFS_DELEGATED_STATE, &state->flags);
1403 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1404 return -EAGAIN;
1405 case -NFS4ERR_STALE_CLIENTID:
1406 case -NFS4ERR_STALE_STATEID:
1407 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 case -NFS4ERR_EXPIRED:
1409 /* Don't recall a delegation if it was lost */
1410 nfs4_schedule_lease_recovery(server->nfs_client);
1411 return -EAGAIN;
1412 case -NFS4ERR_DELEG_REVOKED:
1413 case -NFS4ERR_ADMIN_REVOKED:
1414 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001415 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001416 nfs_inode_find_state_and_recover(state->inode,
1417 stateid);
1418 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001419 return 0;
1420 case -NFS4ERR_DELAY:
1421 case -NFS4ERR_GRACE:
1422 set_bit(NFS_DELEGATED_STATE, &state->flags);
1423 ssleep(1);
1424 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001425 case -ENOMEM:
1426 case -NFS4ERR_DENIED:
1427 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1428 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return err;
1431}
1432
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001433int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1434{
1435 struct nfs_server *server = NFS_SERVER(state->inode);
1436 struct nfs4_opendata *opendata;
1437 int err;
1438
1439 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1440 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1441 if (IS_ERR(opendata))
1442 return PTR_ERR(opendata);
1443 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1444 err = nfs4_open_recover(opendata, state);
1445 nfs4_opendata_put(opendata);
1446 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1447}
1448
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1450{
1451 struct nfs4_opendata *data = calldata;
1452
1453 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001455 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001456 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001458 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001460}
1461
1462static void nfs4_open_confirm_release(void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
1465 struct nfs4_state *state = NULL;
1466
1467 /* If this request hasn't been cancelled, do nothing */
1468 if (data->cancelled == 0)
1469 goto out_free;
1470 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001471 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001472 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001473 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001474 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001475 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001476out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001477 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001478}
1479
1480static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001481 .rpc_call_done = nfs4_open_confirm_done,
1482 .rpc_release = nfs4_open_confirm_release,
1483};
1484
1485/*
1486 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1487 */
1488static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1489{
1490 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1491 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001492 struct rpc_message msg = {
1493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1494 .rpc_argp = &data->c_arg,
1495 .rpc_resp = &data->c_res,
1496 .rpc_cred = data->owner->so_cred,
1497 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001498 struct rpc_task_setup task_setup_data = {
1499 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001500 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001501 .callback_ops = &nfs4_open_confirm_ops,
1502 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001503 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001504 .flags = RPC_TASK_ASYNC,
1505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int status;
1507
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001508 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001509 data->rpc_done = 0;
1510 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001511 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001512 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001513 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001514 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001515 status = nfs4_wait_for_completion_rpc_task(task);
1516 if (status != 0) {
1517 data->cancelled = 1;
1518 smp_wmb();
1519 } else
1520 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001521 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return status;
1523}
1524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001529 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001530
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001532 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001533 /*
1534 * Check if we still need to send an OPEN call, or if we can use
1535 * a delegation instead.
1536 */
1537 if (data->state != NULL) {
1538 struct nfs_delegation *delegation;
1539
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001541 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001542 rcu_read_lock();
1543 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001544 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1545 can_open_delegated(delegation, data->o_arg.fmode))
1546 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001547 rcu_read_unlock();
1548 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001549 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001550 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001551 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001552 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Andy Adamsone23008e2012-10-02 21:07:32 -04001553 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001554 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1555 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001556 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001557 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001558 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001559 &data->o_res.seq_res,
1560 task) != 0)
1561 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001562
1563 /* Set the create mode (note dependency on the session type) */
1564 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1565 if (data->o_arg.open_flags & O_EXCL) {
1566 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1567 if (nfs4_has_persistent_session(clp))
1568 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1569 else if (clp->cl_mvops->minor_version > 0)
1570 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1571 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001572 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001573unlock_no_action:
1574 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001575out_no_action:
1576 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001577out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001578 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001579}
1580
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001581static void nfs4_open_done(struct rpc_task *task, void *calldata)
1582{
1583 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001586
Trond Myklebust14516c32010-07-31 14:29:06 -04001587 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1588 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001589
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001591 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1592 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001593 case S_IFREG:
1594 break;
1595 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001597 break;
1598 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001600 break;
1601 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001603 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001604 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001605 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001606 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1607 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001608 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001609 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001611
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612static void nfs4_open_release(void *calldata)
1613{
1614 struct nfs4_opendata *data = calldata;
1615 struct nfs4_state *state = NULL;
1616
1617 /* If this request hasn't been cancelled, do nothing */
1618 if (data->cancelled == 0)
1619 goto out_free;
1620 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001621 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622 goto out_free;
1623 /* In case we need an open_confirm, no cleanup! */
1624 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1625 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001627 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001628 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001630 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001631}
1632
1633static const struct rpc_call_ops nfs4_open_ops = {
1634 .rpc_call_prepare = nfs4_open_prepare,
1635 .rpc_call_done = nfs4_open_done,
1636 .rpc_release = nfs4_open_release,
1637};
1638
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001639static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640{
1641 struct inode *dir = data->dir->d_inode;
1642 struct nfs_server *server = NFS_SERVER(dir);
1643 struct nfs_openargs *o_arg = &data->o_arg;
1644 struct nfs_openres *o_res = &data->o_res;
1645 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001646 struct rpc_message msg = {
1647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1648 .rpc_argp = o_arg,
1649 .rpc_resp = o_res,
1650 .rpc_cred = data->owner->so_cred,
1651 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001652 struct rpc_task_setup task_setup_data = {
1653 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001654 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001655 .callback_ops = &nfs4_open_ops,
1656 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001657 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001658 .flags = RPC_TASK_ASYNC,
1659 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660 int status;
1661
Trond Myklebust9d12b212012-01-17 22:04:25 -05001662 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001663 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001664 data->rpc_done = 0;
1665 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001666 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667 if (isrecover)
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001668 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001669 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001670 if (IS_ERR(task))
1671 return PTR_ERR(task);
1672 status = nfs4_wait_for_completion_rpc_task(task);
1673 if (status != 0) {
1674 data->cancelled = 1;
1675 smp_wmb();
1676 } else
1677 status = data->rpc_status;
1678 rpc_put_task(task);
1679
1680 return status;
1681}
1682
1683static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1684{
1685 struct inode *dir = data->dir->d_inode;
1686 struct nfs_openres *o_res = &data->o_res;
1687 int status;
1688
1689 status = nfs4_run_open_task(data, 1);
1690 if (status != 0 || !data->rpc_done)
1691 return status;
1692
Trond Myklebust6926afd2012-01-07 13:22:46 -05001693 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1694
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001695 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1696 status = _nfs4_proc_open_confirm(data);
1697 if (status != 0)
1698 return status;
1699 }
1700
1701 return status;
1702}
1703
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001704static int nfs4_opendata_access(struct rpc_cred *cred,
1705 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001706 struct nfs4_state *state, fmode_t fmode,
1707 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001708{
1709 struct nfs_access_entry cache;
1710 u32 mask;
1711
1712 /* access call failed or for some reason the server doesn't
1713 * support any access modes -- defer access call until later */
1714 if (opendata->o_res.access_supported == 0)
1715 return 0;
1716
1717 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001718 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001719 * write mode bits, but POSIX allows the creating process to write.
1720 * use openflags to check for exec, because fmode won't
1721 * always have FMODE_EXEC set when file open for exec. */
1722 if (openflags & __FMODE_EXEC) {
1723 /* ONLY check for exec rights */
1724 mask = MAY_EXEC;
1725 } else if (fmode & FMODE_READ)
1726 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001727
1728 cache.cred = cred;
1729 cache.jiffies = jiffies;
1730 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1731 nfs_access_add_cache(state->inode, &cache);
1732
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001733 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001734 return 0;
1735
1736 /* even though OPEN succeeded, access is denied. Close the file */
1737 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001738 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001739}
1740
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001741/*
1742 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1743 */
1744static int _nfs4_proc_open(struct nfs4_opendata *data)
1745{
1746 struct inode *dir = data->dir->d_inode;
1747 struct nfs_server *server = NFS_SERVER(dir);
1748 struct nfs_openargs *o_arg = &data->o_arg;
1749 struct nfs_openres *o_res = &data->o_res;
1750 int status;
1751
1752 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001753 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001755 if (status != 0) {
1756 if (status == -NFS4ERR_BADNAME &&
1757 !(o_arg->open_flags & O_CREAT))
1758 return -ENOENT;
1759 return status;
1760 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761
Trond Myklebust6926afd2012-01-07 13:22:46 -05001762 nfs_fattr_map_and_free_names(server, &data->f_attr);
1763
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001764 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001765 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001766 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1767 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001769 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001774 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Andy Adamsond83217c2011-03-01 01:34:17 +00001778static int nfs4_recover_expired_lease(struct nfs_server *server)
1779{
1780 return nfs4_client_recover_expired_lease(server->nfs_client);
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/*
1784 * OPEN_EXPIRED:
1785 * reclaim state on the server after a network partition.
1786 * Assumes caller holds the appropriate lock
1787 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001788static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001790 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001791 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001793 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001794 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001795 if (IS_ERR(opendata))
1796 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001797 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001798 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001799 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001800 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001804static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001805{
Trond Myklebust539cd032007-06-05 11:46:42 -04001806 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001807 struct nfs4_exception exception = { };
1808 int err;
1809
1810 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001811 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001812 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1813 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001814 switch (err) {
1815 default:
1816 goto out;
1817 case -NFS4ERR_GRACE:
1818 case -NFS4ERR_DELAY:
1819 nfs4_handle_exception(server, err, &exception);
1820 err = 0;
1821 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001822 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001823out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001824 return err;
1825}
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001830 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 ctx = nfs4_state_find_open_context(state);
1833 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001834 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001836 put_nfs_open_context(ctx);
1837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001840#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001841static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001842{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001843 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001844 nfs4_stateid *stateid = &state->stateid;
1845 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001846
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001847 /* If a state reset has been done, test_stateid is unneeded */
1848 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1849 return;
1850
1851 status = nfs41_test_stateid(server, stateid);
1852 if (status != NFS_OK) {
1853 /* Free the stateid unless the server explicitly
1854 * informs us the stateid is unrecognized. */
1855 if (status != -NFS4ERR_BAD_STATEID)
1856 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001857 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001858
Bryan Schumaker69388672012-09-26 15:25:52 -04001859 write_seqlock(&state->seqlock);
1860 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1861 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001862 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1863 }
1864}
1865
1866/**
1867 * nfs41_check_open_stateid - possibly free an open stateid
1868 *
1869 * @state: NFSv4 state for an inode
1870 *
1871 * Returns NFS_OK if recovery for this stateid is now finished.
1872 * Otherwise a negative NFS4ERR value is returned.
1873 */
1874static int nfs41_check_open_stateid(struct nfs4_state *state)
1875{
1876 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001877 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001878 int status;
1879
1880 /* If a state reset has been done, test_stateid is unneeded */
1881 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1882 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1883 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1884 return -NFS4ERR_BAD_STATEID;
1885
1886 status = nfs41_test_stateid(server, stateid);
1887 if (status != NFS_OK) {
1888 /* Free the stateid unless the server explicitly
1889 * informs us the stateid is unrecognized. */
1890 if (status != -NFS4ERR_BAD_STATEID)
1891 nfs41_free_stateid(server, stateid);
1892
1893 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1894 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1895 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001896 }
1897 return status;
1898}
1899
1900static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1901{
Chuck Levereb64cf92012-07-11 16:30:05 -04001902 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001903
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001904 nfs41_clear_delegation_stateid(state);
1905 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001906 if (status != NFS_OK)
1907 status = nfs4_open_expired(sp, state);
1908 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001909}
1910#endif
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001913 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1914 * fields corresponding to attributes that were used to store the verifier.
1915 * Make sure we clobber those fields in the later setattr call
1916 */
1917static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1918{
1919 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1920 !(sattr->ia_valid & ATTR_ATIME_SET))
1921 sattr->ia_valid |= ATTR_ATIME;
1922
1923 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1924 !(sattr->ia_valid & ATTR_MTIME_SET))
1925 sattr->ia_valid |= ATTR_MTIME;
1926}
1927
Trond Myklebustc21443c2013-02-07 14:26:21 -05001928static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1929 fmode_t fmode,
1930 int flags,
1931 struct nfs4_state **res)
1932{
1933 struct nfs4_state_owner *sp = opendata->owner;
1934 struct nfs_server *server = sp->so_server;
1935 struct nfs4_state *state;
1936 unsigned int seq;
1937 int ret;
1938
1939 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1940
1941 ret = _nfs4_proc_open(opendata);
1942 if (ret != 0)
1943 goto out;
1944
1945 state = nfs4_opendata_to_nfs4_state(opendata);
1946 ret = PTR_ERR(state);
1947 if (IS_ERR(state))
1948 goto out;
1949 if (server->caps & NFS_CAP_POSIX_LOCK)
1950 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1951
1952 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1953 if (ret != 0)
1954 goto out;
1955
Trond Myklebust91876b12013-03-28 14:01:33 -04001956 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05001957 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05001958 *res = state;
1959out:
1960 return ret;
1961}
1962
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001963/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001964 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Andy Adamson82be4172012-05-23 05:02:35 -04001966static int _nfs4_do_open(struct inode *dir,
1967 struct dentry *dentry,
1968 fmode_t fmode,
1969 int flags,
1970 struct iattr *sattr,
1971 struct rpc_cred *cred,
1972 struct nfs4_state **res,
1973 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 struct nfs4_state_owner *sp;
1976 struct nfs4_state *state = NULL;
1977 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001978 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001979 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001980 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001984 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1985 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1987 goto out_err;
1988 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001989 status = nfs4_recover_expired_lease(server);
1990 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001991 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001992 if (dentry->d_inode != NULL)
1993 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001994 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001995 if (dentry->d_inode)
1996 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001997 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001998 claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001999 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002000 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Andy Adamson82be4172012-05-23 05:02:35 -04002002 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2003 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2004 if (!opendata->f_attr.mdsthreshold)
2005 goto err_opendata_put;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002006 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002007 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002008 if (dentry->d_inode != NULL)
2009 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002010
Trond Myklebustc21443c2013-02-07 14:26:21 -05002011 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002012 if (status != 0)
2013 goto err_opendata_put;
2014
Trond Myklebust549b19c2013-04-16 18:42:34 -04002015 if ((opendata->o_arg.open_flags & O_EXCL) &&
2016 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002017 nfs4_exclusive_attrset(opendata, sattr);
2018
2019 nfs_fattr_init(opendata->o_res.f_attr);
2020 status = nfs4_do_setattr(state->inode, cred,
2021 opendata->o_res.f_attr, sattr,
2022 state);
2023 if (status == 0)
2024 nfs_setattr_update_inode(state->inode, sattr);
2025 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2026 }
Andy Adamson82be4172012-05-23 05:02:35 -04002027
2028 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2029 *ctx_th = opendata->f_attr.mdsthreshold;
2030 else
2031 kfree(opendata->f_attr.mdsthreshold);
2032 opendata->f_attr.mdsthreshold = NULL;
2033
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002034 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *res = state;
2037 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002038err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002039 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002040 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002041err_put_state_owner:
2042 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *res = NULL;
2045 return status;
2046}
2047
2048
Andy Adamson82be4172012-05-23 05:02:35 -04002049static struct nfs4_state *nfs4_do_open(struct inode *dir,
2050 struct dentry *dentry,
2051 fmode_t fmode,
2052 int flags,
2053 struct iattr *sattr,
2054 struct rpc_cred *cred,
2055 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002057 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct nfs4_exception exception = { };
2059 struct nfs4_state *res;
2060 int status;
2061
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002062 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 do {
Andy Adamson82be4172012-05-23 05:02:35 -04002064 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2065 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (status == 0)
2067 break;
2068 /* NOTE: BAD_SEQID means the server and client disagree about the
2069 * book-keeping w.r.t. state-changing operations
2070 * (OPEN/CLOSE/LOCK/LOCKU...)
2071 * It is actually a sign of a bug on the client or on the server.
2072 *
2073 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002074 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * have unhashed the old state_owner for us, and that we can
2076 * therefore safely retry using a new one. We should still warn
2077 * the user though...
2078 */
2079 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002080 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002081 " returned a bad sequence-id error!\n",
2082 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 exception.retry = 1;
2084 continue;
2085 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002086 /*
2087 * BAD_STATEID on OPEN means that the server cancelled our
2088 * state before it received the OPEN_CONFIRM.
2089 * Recover by retrying the request as per the discussion
2090 * on Page 181 of RFC3530.
2091 */
2092 if (status == -NFS4ERR_BAD_STATEID) {
2093 exception.retry = 1;
2094 continue;
2095 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002096 if (status == -EAGAIN) {
2097 /* We must have found a delegation */
2098 exception.retry = 1;
2099 continue;
2100 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002101 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2102 continue;
2103 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 status, &exception));
2105 } while (exception.retry);
2106 return res;
2107}
2108
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002109static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2110 struct nfs_fattr *fattr, struct iattr *sattr,
2111 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002113 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002115 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .iap = sattr,
2117 .server = server,
2118 .bitmask = server->attr_bitmask,
2119 };
2120 struct nfs_setattrres res = {
2121 .fattr = fattr,
2122 .server = server,
2123 };
2124 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2126 .rpc_argp = &arg,
2127 .rpc_resp = &res,
2128 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002130 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04002131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Trond Myklebust0e574af2005-10-27 22:12:38 -04002133 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Trond Myklebust5d422302013-03-14 16:57:48 -04002135 if (state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002136 struct nfs_lockowner lockowner = {
2137 .l_owner = current->files,
2138 .l_pid = current->tgid,
2139 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002140 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002141 &lockowner);
Trond Myklebust4fc87962012-03-08 17:42:01 -05002142 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2143 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002144 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002145 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002146 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Bryan Schumaker7c513052011-03-24 17:12:24 +00002148 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002149 if (status == 0 && state != NULL)
2150 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002151 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002154static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2155 struct nfs_fattr *fattr, struct iattr *sattr,
2156 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002158 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002159 struct nfs4_exception exception = {
2160 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002161 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int err;
2164 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04002165 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2166 switch (err) {
2167 case -NFS4ERR_OPENMODE:
2168 if (state && !(state->state & FMODE_WRITE)) {
2169 err = -EBADF;
2170 if (sattr->ia_valid & ATTR_OPEN)
2171 err = -EACCES;
2172 goto out;
2173 }
2174 }
2175 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002177out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return err;
2179}
2180
2181struct nfs4_closedata {
2182 struct inode *inode;
2183 struct nfs4_state *state;
2184 struct nfs_closeargs arg;
2185 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002186 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002187 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002188 bool roc;
2189 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190};
2191
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002192static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002193{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002194 struct nfs4_closedata *calldata = data;
2195 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002196 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002197
Fred Isamanf7e89172011-01-06 11:36:32 +00002198 if (calldata->roc)
2199 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002200 nfs4_put_open_state(calldata->state);
2201 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002202 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002203 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002204 kfree(calldata);
2205}
2206
Trond Myklebust88069f72009-12-08 08:33:16 -05002207static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2208 fmode_t fmode)
2209{
2210 spin_lock(&state->owner->so_lock);
2211 if (!(fmode & FMODE_READ))
2212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2213 if (!(fmode & FMODE_WRITE))
2214 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2215 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2216 spin_unlock(&state->owner->so_lock);
2217}
2218
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002219static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002221 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 struct nfs_server *server = NFS_SERVER(calldata->inode);
2224
Chuck Levera3ca5652012-03-01 17:00:40 -05002225 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002226 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* hmm. we are done with the inode, and in the process of freeing
2229 * the state_owner. we keep this around to process errors
2230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 switch (task->tk_status) {
2232 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002233 if (calldata->roc)
2234 pnfs_roc_set_barrier(state->inode,
2235 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002236 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002237 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002238 nfs4_close_clear_stateid_flags(state,
2239 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 break;
2241 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002242 case -NFS4ERR_OLD_STATEID:
2243 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002245 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002248 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2249 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002251 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002252 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002253 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002256static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002258 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002259 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002260 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002261 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002262
Chuck Levera3ca5652012-03-01 17:00:40 -05002263 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002264 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002265 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002266
Trond Myklebust88069f72009-12-08 08:33:16 -05002267 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2268 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002269 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002270 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002271 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002272 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002273 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002276 }
2277 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002278 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2279 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2280 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002281 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002282 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002283 if (!nfs4_valid_open_stateid(state))
2284 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002285 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002286
2287 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002288 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002289 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002290 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002291
Fred Isamanf7e89172011-01-06 11:36:32 +00002292 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002293 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002294 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002295 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2296 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002297 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002298 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002299 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002300
Trond Myklebust516a6af2005-10-27 22:12:41 -04002301 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002302 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002303 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002304 &calldata->arg.seq_args,
2305 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002306 task) != 0)
2307 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002308 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002309 return;
2310out_no_action:
2311 task->tk_action = NULL;
2312out_wait:
2313 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002316static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002317 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002318 .rpc_call_done = nfs4_close_done,
2319 .rpc_release = nfs4_free_closedata,
2320};
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/*
2323 * It is possible for data to be read/written from a mem-mapped file
2324 * after the sys_close call (which hits the vfs layer as a flush).
2325 * This means that we can't safely call nfsv4 close on a file until
2326 * the inode is cleared. This in turn means that we are not good
2327 * NFSv4 citizens - we do not indicate to the server to update the file's
2328 * share state even when we are done with one of the three share
2329 * stateid's in the inode.
2330 *
2331 * NOTE: Caller must be holding the sp->so_owner semaphore!
2332 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002333int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002335 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002337 struct nfs4_state_owner *sp = state->owner;
2338 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002339 struct rpc_message msg = {
2340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2341 .rpc_cred = state->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002347 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002348 .flags = RPC_TASK_ASYNC,
2349 };
Trond Myklebust95121352005-10-18 14:20:12 -07002350 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002352 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002354 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002355 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002356 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002358 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002359 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002361 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002362 if (calldata->arg.seqid == NULL)
2363 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002364 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002365 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002366 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002367 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002368 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002369 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002370 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002371
Trond Myklebust1174dd12010-12-21 10:52:24 -05002372 msg.rpc_argp = &calldata->arg;
2373 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002374 task_setup_data.callback_data = calldata;
2375 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002376 if (IS_ERR(task))
2377 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002378 status = 0;
2379 if (wait)
2380 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002381 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002382 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002383out_free_calldata:
2384 kfree(calldata);
2385out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002386 nfs4_put_open_state(state);
2387 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002388 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Trond Myklebust2b484292010-09-17 10:56:51 -04002391static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002392nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_state *state;
2395
Trond Myklebust565277f2007-10-15 18:17:53 -04002396 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002397 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2398 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002399 if (IS_ERR(state))
2400 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002401 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002402 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Trond Myklebust1185a552009-12-03 15:54:02 -05002405static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002406{
2407 if (ctx->state == NULL)
2408 return;
2409 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002410 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002411 else
Al Viro643168c2011-06-22 18:20:23 -04002412 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2416{
Benny Halevy43652ad2009-04-01 09:21:54 -04002417 struct nfs4_server_caps_arg args = {
2418 .fhandle = fhandle,
2419 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct nfs4_server_caps_res res = {};
2421 struct rpc_message msg = {
2422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002423 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 .rpc_resp = &res,
2425 };
2426 int status;
2427
Bryan Schumaker7c513052011-03-24 17:12:24 +00002428 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (status == 0) {
2430 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002431 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2432 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2433 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2434 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2435 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2437 server->caps |= NFS_CAP_ACLS;
2438 if (res.has_links != 0)
2439 server->caps |= NFS_CAP_HARDLINKS;
2440 if (res.has_symlinks != 0)
2441 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002442 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2443 server->caps |= NFS_CAP_FILEID;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2445 server->caps |= NFS_CAP_MODE;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2447 server->caps |= NFS_CAP_NLINK;
2448 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2449 server->caps |= NFS_CAP_OWNER;
2450 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2451 server->caps |= NFS_CAP_OWNER_GROUP;
2452 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2453 server->caps |= NFS_CAP_ATIME;
2454 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2455 server->caps |= NFS_CAP_CTIME;
2456 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2457 server->caps |= NFS_CAP_MTIME;
2458
Trond Myklebusta65318b2009-03-11 14:10:28 -04002459 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2460 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2461 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002463 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return status;
2467}
2468
Trond Myklebust55a97592006-06-09 09:34:19 -04002469int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct nfs4_exception exception = { };
2472 int err;
2473 do {
2474 err = nfs4_handle_exception(server,
2475 _nfs4_server_capabilities(server, fhandle),
2476 &exception);
2477 } while (exception.retry);
2478 return err;
2479}
2480
2481static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2482 struct nfs_fsinfo *info)
2483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct nfs4_lookup_root_arg args = {
2485 .bitmask = nfs4_fattr_bitmap,
2486 };
2487 struct nfs4_lookup_res res = {
2488 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002489 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .fh = fhandle,
2491 };
2492 struct rpc_message msg = {
2493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2494 .rpc_argp = &args,
2495 .rpc_resp = &res,
2496 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002497
Trond Myklebust0e574af2005-10-27 22:12:38 -04002498 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002499 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2503 struct nfs_fsinfo *info)
2504{
2505 struct nfs4_exception exception = { };
2506 int err;
2507 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002508 err = _nfs4_lookup_root(server, fhandle, info);
2509 switch (err) {
2510 case 0:
2511 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002512 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002513 default:
2514 err = nfs4_handle_exception(server, err, &exception);
2515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002517out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return err;
2519}
2520
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002521static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2522 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2523{
2524 struct rpc_auth *auth;
2525 int ret;
2526
2527 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002528 if (IS_ERR(auth)) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002529 ret = -EIO;
2530 goto out;
2531 }
2532 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002533out:
2534 return ret;
2535}
2536
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002537static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002538 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002540 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002541 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002543 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04002544 if (len < 0)
2545 return len;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002546
2547 for (i = 0; i < len; i++) {
Chuck Lever6a1a1e32012-07-11 16:31:08 -04002548 /* AUTH_UNIX is the default flavor if none was specified,
2549 * thus has already been tried. */
2550 if (flav_array[i] == RPC_AUTH_UNIX)
2551 continue;
2552
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002553 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002554 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002555 continue;
2556 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002557 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002558 /*
2559 * -EACCESS could mean that the user doesn't have correct permissions
2560 * to access the mount. It could also mean that we tried to mount
2561 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2562 * existing mount programs don't handle -EACCES very well so it should
2563 * be mapped to -EPERM instead.
2564 */
2565 if (status == -EACCES)
2566 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002567 return status;
2568}
2569
2570/*
2571 * get the file handle for the "/" directory on the server
2572 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002573int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2574 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002575{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002576 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002577 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002578 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2579 /*
2580 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2581 * by nfs4_map_errors() as this function exits.
2582 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002583 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (status == 0)
2585 status = nfs4_server_capabilities(server, fhandle);
2586 if (status == 0)
2587 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002588 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
Bryan Schumakerbae36242012-05-10 15:07:31 -04002591static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2592 struct nfs_fsinfo *info)
2593{
2594 int error;
2595 struct nfs_fattr *fattr = info->fattr;
2596
2597 error = nfs4_server_capabilities(server, mntfh);
2598 if (error < 0) {
2599 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2600 return error;
2601 }
2602
2603 error = nfs4_proc_getattr(server, mntfh, fattr);
2604 if (error < 0) {
2605 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2606 return error;
2607 }
2608
2609 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2610 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2611 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2612
2613 return error;
2614}
2615
Manoj Naik6b97fd32006-06-09 09:34:29 -04002616/*
2617 * Get locations and (maybe) other attributes of a referral.
2618 * Note that we'll actually follow the referral later when
2619 * we detect fsid mismatch in inode revalidation
2620 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002621static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2622 const struct qstr *name, struct nfs_fattr *fattr,
2623 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002624{
2625 int status = -ENOMEM;
2626 struct page *page = NULL;
2627 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002628
2629 page = alloc_page(GFP_KERNEL);
2630 if (page == NULL)
2631 goto out;
2632 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2633 if (locations == NULL)
2634 goto out;
2635
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002636 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002637 if (status != 0)
2638 goto out;
2639 /* Make sure server returned a different fsid for the referral */
2640 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002641 dprintk("%s: server did not return a different fsid for"
2642 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002643 status = -EIO;
2644 goto out;
2645 }
Andy Adamson533eb462011-06-13 18:25:56 -04002646 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2647 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002648
Andy Adamson533eb462011-06-13 18:25:56 -04002649 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002650 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002651 memset(fhandle, 0, sizeof(struct nfs_fh));
2652out:
2653 if (page)
2654 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002655 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002656 return status;
2657}
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2660{
2661 struct nfs4_getattr_arg args = {
2662 .fh = fhandle,
2663 .bitmask = server->attr_bitmask,
2664 };
2665 struct nfs4_getattr_res res = {
2666 .fattr = fattr,
2667 .server = server,
2668 };
2669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2671 .rpc_argp = &args,
2672 .rpc_resp = &res,
2673 };
2674
Trond Myklebust0e574af2005-10-27 22:12:38 -04002675 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002676 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2680{
2681 struct nfs4_exception exception = { };
2682 int err;
2683 do {
2684 err = nfs4_handle_exception(server,
2685 _nfs4_proc_getattr(server, fhandle, fattr),
2686 &exception);
2687 } while (exception.retry);
2688 return err;
2689}
2690
2691/*
2692 * The file is not closed if it is opened due to the a request to change
2693 * the size of the file. The open call will not be needed once the
2694 * VFS layer lookup-intents are implemented.
2695 *
2696 * Close is called when the inode is destroyed.
2697 * If we haven't opened the file for O_WRONLY, we
2698 * need to in the size_change case to obtain a stateid.
2699 *
2700 * Got race?
2701 * Because OPEN is always done by name in nfsv4, it is
2702 * possible that we opened a different file by the same
2703 * name. We can recognize this race condition, but we
2704 * can't do anything about it besides returning an error.
2705 *
2706 * This will be fixed with VFS changes (lookup-intent).
2707 */
2708static int
2709nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2710 struct iattr *sattr)
2711{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002712 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002713 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002714 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 int status;
2716
Benny Halevy8a1636c2010-07-14 15:43:57 -04002717 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002718 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002719
Trond Myklebust0e574af2005-10-27 22:12:38 -04002720 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Andy Adamson26699402012-05-30 16:12:24 -04002722 /* Deal with open(O_TRUNC) */
2723 if (sattr->ia_valid & ATTR_OPEN)
2724 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2725
2726 /* Optimization: if the end result is no change, don't RPC */
2727 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2728 return 0;
2729
Trond Myklebustd5308382005-11-04 15:33:38 -05002730 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002731 if (sattr->ia_valid & ATTR_FILE) {
2732 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002733
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002734 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002735 if (ctx) {
2736 cred = ctx->cred;
2737 state = ctx->state;
2738 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002739 }
2740
2741 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002742 if (status == 0)
2743 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return status;
2745}
2746
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002747static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2748 const struct qstr *name, struct nfs_fh *fhandle,
2749 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002750{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002751 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002752 int status;
2753 struct nfs4_lookup_arg args = {
2754 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002755 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002756 .name = name,
2757 };
2758 struct nfs4_lookup_res res = {
2759 .server = server,
2760 .fattr = fattr,
2761 .fh = fhandle,
2762 };
2763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2765 .rpc_argp = &args,
2766 .rpc_resp = &res,
2767 };
2768
2769 nfs_fattr_init(fattr);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002772 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 dprintk("NFS reply lookup: %d\n", status);
2774 return status;
2775}
2776
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002777static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002778{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002779 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002780 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002781 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2782 fattr->nlink = 2;
2783}
2784
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002785static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2786 struct qstr *name, struct nfs_fh *fhandle,
2787 struct nfs_fattr *fattr)
2788{
2789 struct nfs4_exception exception = { };
2790 struct rpc_clnt *client = *clnt;
2791 int err;
2792 do {
2793 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2794 switch (err) {
2795 case -NFS4ERR_BADNAME:
2796 err = -ENOENT;
2797 goto out;
2798 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002799 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002800 goto out;
2801 case -NFS4ERR_WRONGSEC:
2802 err = -EPERM;
2803 if (client != *clnt)
2804 goto out;
2805
2806 client = nfs4_create_sec_client(client, dir, name);
2807 if (IS_ERR(client))
2808 return PTR_ERR(client);
2809
2810 exception.retry = 1;
2811 break;
2812 default:
2813 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2814 }
2815 } while (exception.retry);
2816
2817out:
2818 if (err == 0)
2819 *clnt = client;
2820 else if (client != *clnt)
2821 rpc_shutdown_client(client);
2822
2823 return err;
2824}
2825
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002826static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002829 int status;
2830 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002831
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2833 if (client != NFS_CLIENT(dir)) {
2834 rpc_shutdown_client(client);
2835 nfs_fixup_secinfo_attributes(fattr);
2836 }
2837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002840struct rpc_clnt *
2841nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2842 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2843{
2844 int status;
2845 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2846
2847 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2848 if (status < 0) {
2849 rpc_shutdown_client(client);
2850 return ERR_PTR(status);
2851 }
2852 return client;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2856{
Trond Myklebust76b32992007-08-10 17:45:11 -04002857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct nfs4_accessargs args = {
2859 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002860 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002862 struct nfs4_accessres res = {
2863 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002864 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2867 .rpc_argp = &args,
2868 .rpc_resp = &res,
2869 .rpc_cred = entry->cred,
2870 };
2871 int mode = entry->mask;
2872 int status;
2873
2874 /*
2875 * Determine which access bits we want to ask for...
2876 */
2877 if (mode & MAY_READ)
2878 args.access |= NFS4_ACCESS_READ;
2879 if (S_ISDIR(inode->i_mode)) {
2880 if (mode & MAY_WRITE)
2881 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2882 if (mode & MAY_EXEC)
2883 args.access |= NFS4_ACCESS_LOOKUP;
2884 } else {
2885 if (mode & MAY_WRITE)
2886 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2887 if (mode & MAY_EXEC)
2888 args.access |= NFS4_ACCESS_EXECUTE;
2889 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002890
2891 res.fattr = nfs_alloc_fattr();
2892 if (res.fattr == NULL)
2893 return -ENOMEM;
2894
Bryan Schumaker7c513052011-03-24 17:12:24 +00002895 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002897 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04002898 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002900 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(inode),
2910 _nfs4_proc_access(inode, entry),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916/*
2917 * TODO: For the time being, we don't try to get any attributes
2918 * along with any of the zero-copy operations READ, READDIR,
2919 * READLINK, WRITE.
2920 *
2921 * In the case of the first three, we want to put the GETATTR
2922 * after the read-type operation -- this is because it is hard
2923 * to predict the length of a GETATTR response in v4, and thus
2924 * align the READ data correctly. This means that the GETATTR
2925 * may end up partially falling into the page cache, and we should
2926 * shift it into the 'tail' of the xdr_buf before processing.
2927 * To do this efficiently, we need to know the total length
2928 * of data received, which doesn't seem to be available outside
2929 * of the RPC layer.
2930 *
2931 * In the case of WRITE, we also want to put the GETATTR after
2932 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04002933 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 *
2935 * Both of these changes to the XDR layer would in fact be quite
2936 * minor, but I decided to leave them for a subsequent patch.
2937 */
2938static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2939 unsigned int pgbase, unsigned int pglen)
2940{
2941 struct nfs4_readlink args = {
2942 .fh = NFS_FH(inode),
2943 .pgbase = pgbase,
2944 .pglen = pglen,
2945 .pages = &page,
2946 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002947 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2950 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002951 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 };
2953
Bryan Schumaker7c513052011-03-24 17:12:24 +00002954 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 -07002955}
2956
2957static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2958 unsigned int pgbase, unsigned int pglen)
2959{
2960 struct nfs4_exception exception = { };
2961 int err;
2962 do {
2963 err = nfs4_handle_exception(NFS_SERVER(inode),
2964 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2965 &exception);
2966 } while (exception.retry);
2967 return err;
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002971 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static int
2974nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002975 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002977 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 int status = 0;
2980
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002981 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2982 if (IS_ERR(ctx))
2983 return PTR_ERR(ctx);
2984
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002985 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04002986 state = nfs4_do_open(dir, dentry, ctx->mode,
2987 flags, sattr, ctx->cred,
2988 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002989 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (IS_ERR(state)) {
2991 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002994 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002995 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002996 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997out:
Miklos Szeredi8867fe52012-06-05 15:10:19 +02002998 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return status;
3000}
3001
3002static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3003{
Trond Myklebust16e42952005-10-27 22:12:44 -04003004 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003005 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003007 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003009 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003010 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3014 .rpc_argp = &args,
3015 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003017 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003018
Bryan Schumaker7c513052011-03-24 17:12:24 +00003019 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003020 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003021 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return status;
3023}
3024
3025static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dir),
3031 _nfs4_proc_remove(dir, name),
3032 &exception);
3033 } while (exception.retry);
3034 return err;
3035}
3036
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003037static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003039 struct nfs_server *server = NFS_SERVER(dir);
3040 struct nfs_removeargs *args = msg->rpc_argp;
3041 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003043 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003045 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003048static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3049{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003050 nfs4_setup_sequence(NFS_SERVER(data->dir),
3051 &data->args.seq_args,
3052 &data->res.seq_res,
3053 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003056static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003058 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3059
Trond Myklebust14516c32010-07-31 14:29:06 -04003060 if (!nfs4_sequence_done(task, &res->seq_res))
3061 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003062 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003063 return 0;
3064 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Jeff Laytond3d41522010-09-17 17:31:57 -04003068static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3069{
3070 struct nfs_server *server = NFS_SERVER(dir);
3071 struct nfs_renameargs *arg = msg->rpc_argp;
3072 struct nfs_renameres *res = msg->rpc_resp;
3073
3074 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003075 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003076 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003077}
3078
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003079static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3080{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003081 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3082 &data->args.seq_args,
3083 &data->res.seq_res,
3084 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003085}
3086
3087static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3088 struct inode *new_dir)
3089{
3090 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3091
3092 if (!nfs4_sequence_done(task, &res->seq_res))
3093 return 0;
3094 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3095 return 0;
3096
3097 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003098 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003099 return 1;
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3103 struct inode *new_dir, struct qstr *new_name)
3104{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003105 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003106 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 .old_dir = NFS_FH(old_dir),
3108 .new_dir = NFS_FH(new_dir),
3109 .old_name = old_name,
3110 .new_name = new_name,
3111 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003112 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003113 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003114 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct rpc_message msg = {
3116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3117 .rpc_argp = &arg,
3118 .rpc_resp = &res,
3119 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003120 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Bryan Schumaker7c513052011-03-24 17:12:24 +00003122 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (!status) {
3124 update_changeattr(old_dir, &res.old_cinfo);
3125 update_changeattr(new_dir, &res.new_cinfo);
3126 }
3127 return status;
3128}
3129
3130static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3131 struct inode *new_dir, struct qstr *new_name)
3132{
3133 struct nfs4_exception exception = { };
3134 int err;
3135 do {
3136 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3137 _nfs4_proc_rename(old_dir, old_name,
3138 new_dir, new_name),
3139 &exception);
3140 } while (exception.retry);
3141 return err;
3142}
3143
3144static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3145{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003146 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct nfs4_link_arg arg = {
3148 .fh = NFS_FH(inode),
3149 .dir_fh = NFS_FH(dir),
3150 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003151 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003153 struct nfs4_link_res res = {
3154 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003155 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3158 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003159 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003161 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Trond Myklebust136f2622010-04-16 16:22:49 -04003163 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003164 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003165 goto out;
3166
Bryan Schumaker7c513052011-03-24 17:12:24 +00003167 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003168 if (!status) {
3169 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04003170 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003171 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003172out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003173 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return status;
3175}
3176
3177static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3178{
3179 struct nfs4_exception exception = { };
3180 int err;
3181 do {
3182 err = nfs4_handle_exception(NFS_SERVER(inode),
3183 _nfs4_proc_link(inode, dir, name),
3184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003189struct nfs4_createdata {
3190 struct rpc_message msg;
3191 struct nfs4_create_arg arg;
3192 struct nfs4_create_res res;
3193 struct nfs_fh fh;
3194 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195};
3196
3197static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3198 struct qstr *name, struct iattr *sattr, u32 ftype)
3199{
3200 struct nfs4_createdata *data;
3201
3202 data = kzalloc(sizeof(*data), GFP_KERNEL);
3203 if (data != NULL) {
3204 struct nfs_server *server = NFS_SERVER(dir);
3205
3206 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3207 data->msg.rpc_argp = &data->arg;
3208 data->msg.rpc_resp = &data->res;
3209 data->arg.dir_fh = NFS_FH(dir);
3210 data->arg.server = server;
3211 data->arg.name = name;
3212 data->arg.attrs = sattr;
3213 data->arg.ftype = ftype;
3214 data->arg.bitmask = server->attr_bitmask;
3215 data->res.server = server;
3216 data->res.fh = &data->fh;
3217 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003218 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 }
3220 return data;
3221}
3222
3223static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3224{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003225 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003226 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003227 if (status == 0) {
3228 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003229 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3230 }
3231 return status;
3232}
3233
3234static void nfs4_free_createdata(struct nfs4_createdata *data)
3235{
3236 kfree(data);
3237}
3238
Chuck Lever4f390c12006-08-22 20:06:22 -04003239static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003240 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003242 struct nfs4_createdata *data;
3243 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Chuck Lever94a6d752006-08-22 20:06:23 -04003245 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003246 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003247
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003248 status = -ENOMEM;
3249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3250 if (data == NULL)
3251 goto out;
3252
3253 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3254 data->arg.u.symlink.pages = &page;
3255 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003257 status = nfs4_do_create(dir, dentry, data);
3258
3259 nfs4_free_createdata(data);
3260out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return status;
3262}
3263
Chuck Lever4f390c12006-08-22 20:06:22 -04003264static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003265 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 struct nfs4_exception exception = { };
3268 int err;
3269 do {
3270 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003271 _nfs4_proc_symlink(dir, dentry, page,
3272 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
3278static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr)
3280{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003281 struct nfs4_createdata *data;
3282 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3285 if (data == NULL)
3286 goto out;
3287
3288 status = nfs4_do_create(dir, dentry, data);
3289
3290 nfs4_free_createdata(data);
3291out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return status;
3293}
3294
3295static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3296 struct iattr *sattr)
3297{
3298 struct nfs4_exception exception = { };
3299 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003300
3301 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 do {
3303 err = nfs4_handle_exception(NFS_SERVER(dir),
3304 _nfs4_proc_mkdir(dir, dentry, sattr),
3305 &exception);
3306 } while (exception.retry);
3307 return err;
3308}
3309
3310static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003311 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct inode *dir = dentry->d_inode;
3314 struct nfs4_readdir_arg args = {
3315 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003316 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 .pgbase = 0,
3318 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003319 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003320 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 };
3322 struct nfs4_readdir_res res;
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3325 .rpc_argp = &args,
3326 .rpc_resp = &res,
3327 .rpc_cred = cred,
3328 };
3329 int status;
3330
Harvey Harrison3110ff82008-05-02 13:42:44 -07003331 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003332 dentry->d_parent->d_name.name,
3333 dentry->d_name.name,
3334 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003335 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003337 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 -05003338 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003339 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003340 status += args.pgbase;
3341 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003342
3343 nfs_invalidate_atime(dir);
3344
Harvey Harrison3110ff82008-05-02 13:42:44 -07003345 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return status;
3347}
3348
3349static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003350 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 struct nfs4_exception exception = { };
3353 int err;
3354 do {
3355 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3356 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003357 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 &exception);
3359 } while (exception.retry);
3360 return err;
3361}
3362
3363static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3364 struct iattr *sattr, dev_t rdev)
3365{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003366 struct nfs4_createdata *data;
3367 int mode = sattr->ia_mode;
3368 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003370 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3371 if (data == NULL)
3372 goto out;
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003375 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003377 data->arg.ftype = NF4BLK;
3378 data->arg.u.device.specdata1 = MAJOR(rdev);
3379 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
3381 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003382 data->arg.ftype = NF4CHR;
3383 data->arg.u.device.specdata1 = MAJOR(rdev);
3384 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003385 } else if (!S_ISSOCK(mode)) {
3386 status = -EINVAL;
3387 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003390 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003391out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003392 nfs4_free_createdata(data);
3393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return status;
3395}
3396
3397static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3398 struct iattr *sattr, dev_t rdev)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003402
3403 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 do {
3405 err = nfs4_handle_exception(NFS_SERVER(dir),
3406 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
3412static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fsstat *fsstat)
3414{
3415 struct nfs4_statfs_arg args = {
3416 .fh = fhandle,
3417 .bitmask = server->attr_bitmask,
3418 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003419 struct nfs4_statfs_res res = {
3420 .fsstat = fsstat,
3421 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 struct rpc_message msg = {
3423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3424 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003425 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 };
3427
Trond Myklebust0e574af2005-10-27 22:12:38 -04003428 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003429 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3433{
3434 struct nfs4_exception exception = { };
3435 int err;
3436 do {
3437 err = nfs4_handle_exception(server,
3438 _nfs4_proc_statfs(server, fhandle, fsstat),
3439 &exception);
3440 } while (exception.retry);
3441 return err;
3442}
3443
3444static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3445 struct nfs_fsinfo *fsinfo)
3446{
3447 struct nfs4_fsinfo_arg args = {
3448 .fh = fhandle,
3449 .bitmask = server->attr_bitmask,
3450 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003451 struct nfs4_fsinfo_res res = {
3452 .fsinfo = fsinfo,
3453 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3456 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003457 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 };
3459
Bryan Schumaker7c513052011-03-24 17:12:24 +00003460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3464{
3465 struct nfs4_exception exception = { };
3466 int err;
3467
3468 do {
3469 err = nfs4_handle_exception(server,
3470 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3471 &exception);
3472 } while (exception.retry);
3473 return err;
3474}
3475
3476static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3477{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003478 int error;
3479
Trond Myklebust0e574af2005-10-27 22:12:38 -04003480 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003481 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003482 if (error == 0) {
3483 /* block layout checks this! */
3484 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003485 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003486 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003487
3488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
3491static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_pathconf *pathconf)
3493{
3494 struct nfs4_pathconf_arg args = {
3495 .fh = fhandle,
3496 .bitmask = server->attr_bitmask,
3497 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003498 struct nfs4_pathconf_res res = {
3499 .pathconf = pathconf,
3500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3503 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003504 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 };
3506
3507 /* None of the pathconf attributes are mandatory to implement */
3508 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3509 memset(pathconf, 0, sizeof(*pathconf));
3510 return 0;
3511 }
3512
Trond Myklebust0e574af2005-10-27 22:12:38 -04003513 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
3517static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3518 struct nfs_pathconf *pathconf)
3519{
3520 struct nfs4_exception exception = { };
3521 int err;
3522
3523 do {
3524 err = nfs4_handle_exception(server,
3525 _nfs4_proc_pathconf(server, fhandle, pathconf),
3526 &exception);
3527 } while (exception.retry);
3528 return err;
3529}
3530
Trond Myklebust5521abf2013-03-16 20:54:34 -04003531int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003532 const struct nfs_open_context *ctx,
3533 const struct nfs_lock_context *l_ctx,
3534 fmode_t fmode)
3535{
3536 const struct nfs_lockowner *lockowner = NULL;
3537
3538 if (l_ctx != NULL)
3539 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003540 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003541}
3542EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3543
Trond Myklebust5521abf2013-03-16 20:54:34 -04003544static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3545 const struct nfs_open_context *ctx,
3546 const struct nfs_lock_context *l_ctx,
3547 fmode_t fmode)
3548{
3549 nfs4_stateid current_stateid;
3550
3551 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3552 return false;
3553 return nfs4_stateid_match(stateid, &current_stateid);
3554}
3555
3556static bool nfs4_error_stateid_expired(int err)
3557{
3558 switch (err) {
3559 case -NFS4ERR_DELEG_REVOKED:
3560 case -NFS4ERR_ADMIN_REVOKED:
3561 case -NFS4ERR_BAD_STATEID:
3562 case -NFS4ERR_STALE_STATEID:
3563 case -NFS4ERR_OLD_STATEID:
3564 case -NFS4ERR_OPENMODE:
3565 case -NFS4ERR_EXPIRED:
3566 return true;
3567 }
3568 return false;
3569}
3570
Benny Halevyd20581a2011-05-22 19:52:03 +03003571void __nfs4_read_done_cb(struct nfs_read_data *data)
3572{
Fred Isamancd841602012-04-20 14:47:44 -04003573 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003574}
3575
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003576static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Fred Isamancd841602012-04-20 14:47:44 -04003578 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003580 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003581 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003582 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003584
Benny Halevyd20581a2011-05-22 19:52:03 +03003585 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003587 renew_lease(server, data->timestamp);
3588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Trond Myklebust5521abf2013-03-16 20:54:34 -04003591static bool nfs4_read_stateid_changed(struct rpc_task *task,
3592 struct nfs_readargs *args)
3593{
3594
3595 if (!nfs4_error_stateid_expired(task->tk_status) ||
3596 nfs4_stateid_is_current(&args->stateid,
3597 args->context,
3598 args->lock_context,
3599 FMODE_READ))
3600 return false;
3601 rpc_restart_call_prepare(task);
3602 return true;
3603}
3604
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003605static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3606{
3607
3608 dprintk("--> %s\n", __func__);
3609
3610 if (!nfs4_sequence_done(task, &data->res.seq_res))
3611 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003612 if (nfs4_read_stateid_changed(task, &data->args))
3613 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003614 return data->read_done_cb ? data->read_done_cb(task, data) :
3615 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003616}
3617
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003618static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003621 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003622 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003623 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003626static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3627{
Trond Myklebust9b206142013-03-17 15:52:00 -04003628 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003629 &data->args.seq_args,
3630 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003631 task))
3632 return;
3633 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3634 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
Fred Isamanb029bc92011-03-03 15:13:42 +00003637static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
Fred Isamancd841602012-04-20 14:47:44 -04003639 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003641 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003642 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003643 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003645 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003647 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003648 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Trond Myklebust5521abf2013-03-16 20:54:34 -04003652static bool nfs4_write_stateid_changed(struct rpc_task *task,
3653 struct nfs_writeargs *args)
3654{
3655
3656 if (!nfs4_error_stateid_expired(task->tk_status) ||
3657 nfs4_stateid_is_current(&args->stateid,
3658 args->context,
3659 args->lock_context,
3660 FMODE_WRITE))
3661 return false;
3662 rpc_restart_call_prepare(task);
3663 return true;
3664}
3665
Fred Isamanb029bc92011-03-03 15:13:42 +00003666static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3667{
3668 if (!nfs4_sequence_done(task, &data->res.seq_res))
3669 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003670 if (nfs4_write_stateid_changed(task, &data->args))
3671 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003672 return data->write_done_cb ? data->write_done_cb(task, data) :
3673 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003674}
3675
Trond Myklebust5a37f852012-04-28 14:55:16 -04003676static
3677bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003678{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003679 const struct nfs_pgio_header *hdr = data->header;
3680
3681 /* Don't request attributes for pNFS or O_DIRECT writes */
3682 if (data->ds_clp != NULL || hdr->dreq != NULL)
3683 return false;
3684 /* Otherwise, request attributes if and only if we don't hold
3685 * a delegation
3686 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003687 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003688}
Fred Isamana69aef12011-03-03 15:13:47 +00003689
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003690static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Fred Isamancd841602012-04-20 14:47:44 -04003692 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003693
Trond Myklebust5a37f852012-04-28 14:55:16 -04003694 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003695 data->args.bitmask = NULL;
3696 data->res.fattr = NULL;
3697 } else
3698 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003699
Fred Isamanb029bc92011-03-03 15:13:42 +00003700 if (!data->write_done_cb)
3701 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003702 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 data->timestamp = jiffies;
3704
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003705 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003706 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003709static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3710{
Trond Myklebust9b206142013-03-17 15:52:00 -04003711 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003712 &data->args.seq_args,
3713 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003714 task))
3715 return;
3716 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3717 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
Fred Isaman0b7c0152012-04-20 14:47:39 -04003720static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3721{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003722 nfs4_setup_sequence(NFS_SERVER(data->inode),
3723 &data->args.seq_args,
3724 &data->res.seq_res,
3725 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Fred Isaman0b7c0152012-04-20 14:47:39 -04003728static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003731
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003732 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003733 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003734 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737}
3738
Fred Isaman0b7c0152012-04-20 14:47:39 -04003739static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003740{
3741 if (!nfs4_sequence_done(task, &data->res.seq_res))
3742 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003743 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003744}
3745
Fred Isaman0b7c0152012-04-20 14:47:39 -04003746static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003748 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003749
Fred Isaman0b7c0152012-04-20 14:47:39 -04003750 if (data->commit_done_cb == NULL)
3751 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003752 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003753 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003754 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003757struct nfs4_renewdata {
3758 struct nfs_client *client;
3759 unsigned long timestamp;
3760};
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762/*
3763 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3764 * standalone procedure for queueing an asynchronous RENEW.
3765 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003766static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003767{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003768 struct nfs4_renewdata *data = calldata;
3769 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003770
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003771 if (atomic_read(&clp->cl_count) > 1)
3772 nfs4_schedule_state_renewal(clp);
3773 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003774 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003775}
3776
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003777static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003779 struct nfs4_renewdata *data = calldata;
3780 struct nfs_client *clp = data->client;
3781 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003784 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003785 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3786 return;
3787 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003788 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003789 return;
3790 }
3791 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003793 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003796static const struct rpc_call_ops nfs4_renew_ops = {
3797 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003798 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003799};
3800
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003801static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 struct rpc_message msg = {
3804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3805 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003806 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003808 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003810 if (renew_flags == 0)
3811 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003812 if (!atomic_inc_not_zero(&clp->cl_count))
3813 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003814 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003815 if (data == NULL)
3816 return -ENOMEM;
3817 data->client = clp;
3818 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003819 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003820 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003823static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 struct rpc_message msg = {
3826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3827 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003828 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 };
3830 unsigned long now = jiffies;
3831 int status;
3832
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04003833 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 if (status < 0)
3835 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003836 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return 0;
3838}
3839
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003840static inline int nfs4_server_supports_acls(struct nfs_server *server)
3841{
3842 return (server->caps & NFS_CAP_ACLS)
3843 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3844 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3845}
3846
Trond Myklebust21f498c2012-08-24 10:59:25 -04003847/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3848 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003849 * the stack.
3850 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04003851#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003852
Neil Hormane9e3d722011-03-04 19:26:03 -05003853static int buf_to_pages_noslab(const void *buf, size_t buflen,
3854 struct page **pages, unsigned int *pgbase)
3855{
3856 struct page *newpage, **spages;
3857 int rc = 0;
3858 size_t len;
3859 spages = pages;
3860
3861 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04003862 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003863 newpage = alloc_page(GFP_KERNEL);
3864
3865 if (newpage == NULL)
3866 goto unwind;
3867 memcpy(page_address(newpage), buf, len);
3868 buf += len;
3869 buflen -= len;
3870 *pages++ = newpage;
3871 rc++;
3872 } while (buflen != 0);
3873
3874 return rc;
3875
3876unwind:
3877 for(; rc > 0; rc--)
3878 __free_page(spages[rc-1]);
3879 return -ENOMEM;
3880}
3881
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003882struct nfs4_cached_acl {
3883 int cached;
3884 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003885 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003886};
3887
3888static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003889{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003890 struct nfs_inode *nfsi = NFS_I(inode);
3891
3892 spin_lock(&inode->i_lock);
3893 kfree(nfsi->nfs4_acl);
3894 nfsi->nfs4_acl = acl;
3895 spin_unlock(&inode->i_lock);
3896}
3897
3898static void nfs4_zap_acl_attr(struct inode *inode)
3899{
3900 nfs4_set_cached_acl(inode, NULL);
3901}
3902
3903static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3904{
3905 struct nfs_inode *nfsi = NFS_I(inode);
3906 struct nfs4_cached_acl *acl;
3907 int ret = -ENOENT;
3908
3909 spin_lock(&inode->i_lock);
3910 acl = nfsi->nfs4_acl;
3911 if (acl == NULL)
3912 goto out;
3913 if (buf == NULL) /* user is just asking for length */
3914 goto out_len;
3915 if (acl->cached == 0)
3916 goto out;
3917 ret = -ERANGE; /* see getxattr(2) man page */
3918 if (acl->len > buflen)
3919 goto out;
3920 memcpy(buf, acl->data, acl->len);
3921out_len:
3922 ret = acl->len;
3923out:
3924 spin_unlock(&inode->i_lock);
3925 return ret;
3926}
3927
Sachin Prabhu5794d212012-04-17 14:36:40 +01003928static 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 +00003929{
3930 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003931 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003932
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07003933 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04003934 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003935 if (acl == NULL)
3936 goto out;
3937 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003938 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003939 } else {
3940 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3941 if (acl == NULL)
3942 goto out;
3943 acl->cached = 0;
3944 }
3945 acl->len = acl_len;
3946out:
3947 nfs4_set_cached_acl(inode, acl);
3948}
3949
Andy Adamsonbf118a32011-12-07 11:55:27 -05003950/*
3951 * The getxattr API returns the required buffer length when called with a
3952 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3953 * the required buf. On a NULL buf, we send a page of data to the server
3954 * guessing that the ACL request can be serviced by a page. If so, we cache
3955 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3956 * the cache. If not so, we throw away the page, and cache the required
3957 * length. The next getxattr call will then produce another round trip to
3958 * the server, this time with the input buf of the required size.
3959 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003960static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003961{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003962 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003963 struct nfs_getaclargs args = {
3964 .fh = NFS_FH(inode),
3965 .acl_pages = pages,
3966 .acl_len = buflen,
3967 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003968 struct nfs_getaclres res = {
3969 .acl_len = buflen,
3970 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003971 struct rpc_message msg = {
3972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3973 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003974 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003975 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04003976 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3977 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003978
Andy Adamsonbf118a32011-12-07 11:55:27 -05003979 /* As long as we're doing a round trip to the server anyway,
3980 * let's be prepared for a page of acl data. */
3981 if (npages == 0)
3982 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04003983 if (npages > ARRAY_SIZE(pages))
3984 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003985
Andy Adamsonbf118a32011-12-07 11:55:27 -05003986 for (i = 0; i < npages; i++) {
3987 pages[i] = alloc_page(GFP_KERNEL);
3988 if (!pages[i])
3989 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003990 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003991
3992 /* for decoding across pages */
3993 res.acl_scratch = alloc_page(GFP_KERNEL);
3994 if (!res.acl_scratch)
3995 goto out_free;
3996
Andy Adamsonbf118a32011-12-07 11:55:27 -05003997 args.acl_len = npages * PAGE_SIZE;
3998 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003999
Peng Taode040be2012-01-10 22:42:47 +08004000 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004001 __func__, buf, buflen, npages, args.acl_len);
4002 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4003 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004004 if (ret)
4005 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004006
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004007 /* Handle the case where the passed-in buffer is too short */
4008 if (res.acl_flags & NFS4_ACL_TRUNC) {
4009 /* Did the user only issue a request for the acl length? */
4010 if (buf == NULL)
4011 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004012 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004013 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004014 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004015 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004016 if (buf) {
4017 if (res.acl_len > buflen) {
4018 ret = -ERANGE;
4019 goto out_free;
4020 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004021 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004022 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004023out_ok:
4024 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004025out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004026 for (i = 0; i < npages; i++)
4027 if (pages[i])
4028 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004029 if (res.acl_scratch)
4030 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004031 return ret;
4032}
4033
Trond Myklebust16b42892006-08-24 12:27:15 -04004034static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4035{
4036 struct nfs4_exception exception = { };
4037 ssize_t ret;
4038 do {
4039 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4040 if (ret >= 0)
4041 break;
4042 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4043 } while (exception.retry);
4044 return ret;
4045}
4046
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004047static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4048{
4049 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004050 int ret;
4051
4052 if (!nfs4_server_supports_acls(server))
4053 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004054 ret = nfs_revalidate_inode(server, inode);
4055 if (ret < 0)
4056 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004057 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4058 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004059 ret = nfs4_read_cached_acl(inode, buf, buflen);
4060 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004061 /* -ENOENT is returned if there is no ACL or if there is an ACL
4062 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004063 return ret;
4064 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004065}
4066
Trond Myklebust16b42892006-08-24 12:27:15 -04004067static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004068{
4069 struct nfs_server *server = NFS_SERVER(inode);
4070 struct page *pages[NFS4ACL_MAXPAGES];
4071 struct nfs_setaclargs arg = {
4072 .fh = NFS_FH(inode),
4073 .acl_pages = pages,
4074 .acl_len = buflen,
4075 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004076 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004077 struct rpc_message msg = {
4078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4079 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004080 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004081 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004082 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004083 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004084
4085 if (!nfs4_server_supports_acls(server))
4086 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004087 if (npages > ARRAY_SIZE(pages))
4088 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004089 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4090 if (i < 0)
4091 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004092 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004093 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004094
4095 /*
4096 * Free each page after tx, so the only ref left is
4097 * held by the network stack
4098 */
4099 for (; i > 0; i--)
4100 put_page(pages[i-1]);
4101
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004102 /*
4103 * Acl update can result in inode attribute update.
4104 * so mark the attribute cache invalid.
4105 */
4106 spin_lock(&inode->i_lock);
4107 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4108 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004109 nfs_access_zap_cache(inode);
4110 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004111 return ret;
4112}
4113
Trond Myklebust16b42892006-08-24 12:27:15 -04004114static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4115{
4116 struct nfs4_exception exception = { };
4117 int err;
4118 do {
4119 err = nfs4_handle_exception(NFS_SERVER(inode),
4120 __nfs4_proc_set_acl(inode, buf, buflen),
4121 &exception);
4122 } while (exception.retry);
4123 return err;
4124}
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004127nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004129 struct nfs_client *clp = server->nfs_client;
4130
4131 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return 0;
4133 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004134 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004135 case -NFS4ERR_ADMIN_REVOKED:
4136 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004137 if (state == NULL)
4138 break;
4139 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004140 case -NFS4ERR_OPENMODE:
4141 if (state == NULL)
4142 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004143 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4144 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004145 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004146 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004147 if (state != NULL) {
4148 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4149 goto stateid_invalid;
4150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004152 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004153 nfs4_schedule_lease_recovery(clp);
4154 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004155#if defined(CONFIG_NFS_V4_1)
4156 case -NFS4ERR_BADSESSION:
4157 case -NFS4ERR_BADSLOT:
4158 case -NFS4ERR_BAD_HIGH_SLOT:
4159 case -NFS4ERR_DEADSESSION:
4160 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4161 case -NFS4ERR_SEQ_FALSE_RETRY:
4162 case -NFS4ERR_SEQ_MISORDERED:
4163 dprintk("%s ERROR %d, Reset session\n", __func__,
4164 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004165 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004166 task->tk_status = 0;
4167 return -EAGAIN;
4168#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004170 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004171 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4173 task->tk_status = 0;
4174 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004175 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 case -NFS4ERR_OLD_STATEID:
4177 task->tk_status = 0;
4178 return -EAGAIN;
4179 }
4180 task->tk_status = nfs4_map_errors(task->tk_status);
4181 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004182stateid_invalid:
4183 task->tk_status = -EIO;
4184 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004185wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004186 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004187 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4188 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4189 task->tk_status = 0;
4190 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Chuck Leverf0920752012-05-21 22:45:41 -04004193static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4194 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004195{
4196 __be32 verf[2];
4197
Chuck Lever2c820d92012-05-21 22:45:33 -04004198 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4199 /* An impossible timestamp guarantees this value
4200 * will never match a generated boot time. */
4201 verf[0] = 0;
4202 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4203 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004204 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4205 verf[0] = (__be32)nn->boot_time.tv_sec;
4206 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004207 }
Chuck Levercd937102012-03-02 17:14:31 -05004208 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4209}
4210
Chuck Levere984a552012-09-14 17:24:21 -04004211static unsigned int
4212nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4213 char *buf, size_t len)
4214{
4215 unsigned int result;
4216
4217 rcu_read_lock();
4218 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4219 clp->cl_ipaddr,
4220 rpc_peeraddr2str(clp->cl_rpcclient,
4221 RPC_DISPLAY_ADDR),
4222 rpc_peeraddr2str(clp->cl_rpcclient,
4223 RPC_DISPLAY_PROTO));
4224 rcu_read_unlock();
4225 return result;
4226}
4227
4228static unsigned int
4229nfs4_init_uniform_client_string(const struct nfs_client *clp,
4230 char *buf, size_t len)
4231{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004232 char *nodename = clp->cl_rpcclient->cl_nodename;
4233
4234 if (nfs4_client_id_uniquifier[0] != '\0')
4235 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004236 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4237 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004238 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004239}
4240
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004241/**
4242 * nfs4_proc_setclientid - Negotiate client ID
4243 * @clp: state data structure
4244 * @program: RPC program for NFSv4 callback service
4245 * @port: IP port number for NFS4 callback service
4246 * @cred: RPC credential to use for this call
4247 * @res: where to place the result
4248 *
4249 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4250 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004251int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4252 unsigned short port, struct rpc_cred *cred,
4253 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 nfs4_verifier sc_verifier;
4256 struct nfs4_setclientid setclientid = {
4257 .sc_verifier = &sc_verifier,
4258 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004259 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 };
4261 struct rpc_message msg = {
4262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4263 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004264 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004265 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004267 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Chuck Leverde734832012-07-11 16:30:50 -04004269 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004270 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004271 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4272 setclientid.sc_name_len =
4273 nfs4_init_uniform_client_string(clp,
4274 setclientid.sc_name,
4275 sizeof(setclientid.sc_name));
4276 else
4277 setclientid.sc_name_len =
4278 nfs4_init_nonuniform_client_string(clp,
4279 setclientid.sc_name,
4280 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004281 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004282 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004283 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004284 sizeof(setclientid.sc_netid),
4285 rpc_peeraddr2str(clp->cl_rpcclient,
4286 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004287 rcu_read_unlock();
4288 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004289 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 clp->cl_ipaddr, port >> 8, port & 255);
4291
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004292 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4293 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4294 setclientid.sc_name_len, setclientid.sc_name);
4295 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4296 dprintk("NFS reply setclientid: %d\n", status);
4297 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004300/**
4301 * nfs4_proc_setclientid_confirm - Confirm client ID
4302 * @clp: state data structure
4303 * @res: result of a previous SETCLIENTID
4304 * @cred: RPC credential to use for this call
4305 *
4306 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4307 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004308int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004309 struct nfs4_setclientid_res *arg,
4310 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
4312 struct nfs_fsinfo fsinfo;
4313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004315 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004317 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 };
4319 unsigned long now;
4320 int status;
4321
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004322 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4323 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4324 clp->cl_clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004326 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (status == 0) {
4328 spin_lock(&clp->cl_lock);
4329 clp->cl_lease_time = fsinfo.lease_time * HZ;
4330 clp->cl_last_renewal = now;
4331 spin_unlock(&clp->cl_lock);
4332 }
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004333 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return status;
4335}
4336
Trond Myklebustfe650402006-01-03 09:55:18 +01004337struct nfs4_delegreturndata {
4338 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004339 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004340 struct nfs_fh fh;
4341 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004342 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004343 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004344 int rpc_status;
4345};
4346
Trond Myklebustfe650402006-01-03 09:55:18 +01004347static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4348{
4349 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004350
Trond Myklebust14516c32010-07-31 14:29:06 -04004351 if (!nfs4_sequence_done(task, &data->res.seq_res))
4352 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004353
Ricardo Labiaga79708862009-12-07 09:23:21 -05004354 switch (task->tk_status) {
4355 case -NFS4ERR_STALE_STATEID:
4356 case -NFS4ERR_EXPIRED:
4357 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004358 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004359 break;
4360 default:
4361 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4362 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004363 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004364 return;
4365 }
4366 }
4367 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004368}
4369
4370static void nfs4_delegreturn_release(void *calldata)
4371{
Trond Myklebustfe650402006-01-03 09:55:18 +01004372 kfree(calldata);
4373}
4374
Andy Adamson938e1012009-04-01 09:22:28 -04004375#if defined(CONFIG_NFS_V4_1)
4376static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4377{
4378 struct nfs4_delegreturndata *d_data;
4379
4380 d_data = (struct nfs4_delegreturndata *)data;
4381
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004382 nfs4_setup_sequence(d_data->res.server,
4383 &d_data->args.seq_args,
4384 &d_data->res.seq_res,
4385 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004386}
4387#endif /* CONFIG_NFS_V4_1 */
4388
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004389static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004390#if defined(CONFIG_NFS_V4_1)
4391 .rpc_call_prepare = nfs4_delegreturn_prepare,
4392#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004393 .rpc_call_done = nfs4_delegreturn_done,
4394 .rpc_release = nfs4_delegreturn_release,
4395};
4396
Trond Myklebuste6f81072008-01-24 18:14:34 -05004397static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004398{
4399 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004400 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004401 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004402 struct rpc_message msg = {
4403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4404 .rpc_cred = cred,
4405 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004406 struct rpc_task_setup task_setup_data = {
4407 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004408 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004409 .callback_ops = &nfs4_delegreturn_ops,
4410 .flags = RPC_TASK_ASYNC,
4411 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004412 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004413
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004414 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004415 if (data == NULL)
4416 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004417 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004418 data->args.fhandle = &data->fh;
4419 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004420 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004421 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004422 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004423 data->res.fattr = &data->fattr;
4424 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004425 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004426 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004427 data->rpc_status = 0;
4428
Trond Myklebustc970aa82007-07-14 15:39:59 -04004429 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004430 msg.rpc_argp = &data->args;
4431 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004433 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004434 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004435 if (!issync)
4436 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004437 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004438 if (status != 0)
4439 goto out;
4440 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004441 if (status == 0)
4442 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4443 else
4444 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004445out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004446 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004447 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
Trond Myklebuste6f81072008-01-24 18:14:34 -05004450int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 struct nfs_server *server = NFS_SERVER(inode);
4453 struct nfs4_exception exception = { };
4454 int err;
4455 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004456 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 switch (err) {
4458 case -NFS4ERR_STALE_STATEID:
4459 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 case 0:
4461 return 0;
4462 }
4463 err = nfs4_handle_exception(server, err, &exception);
4464 } while (exception.retry);
4465 return err;
4466}
4467
4468#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4469#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4470
4471/*
4472 * sleep, with exponential backoff, and retry the LOCK operation.
4473 */
4474static unsigned long
4475nfs4_set_lock_task_retry(unsigned long timeout)
4476{
Jeff Laytond3103102011-12-01 22:44:39 +01004477 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 timeout <<= 1;
4479 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4480 return NFS4_LOCK_MAXTIMEOUT;
4481 return timeout;
4482}
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4485{
4486 struct inode *inode = state->inode;
4487 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004488 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004489 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004491 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004493 struct nfs_lockt_res res = {
4494 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 };
4496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4498 .rpc_argp = &arg,
4499 .rpc_resp = &res,
4500 .rpc_cred = state->owner->so_cred,
4501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 struct nfs4_lock_state *lsp;
4503 int status;
4504
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004505 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004506 status = nfs4_set_lock_state(state, request);
4507 if (status != 0)
4508 goto out;
4509 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004510 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004511 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004513 switch (status) {
4514 case 0:
4515 request->fl_type = F_UNLCK;
4516 break;
4517 case -NFS4ERR_DENIED:
4518 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004520 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004521out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return status;
4523}
4524
4525static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4526{
4527 struct nfs4_exception exception = { };
4528 int err;
4529
4530 do {
4531 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4532 _nfs4_proc_getlk(state, cmd, request),
4533 &exception);
4534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int do_vfs_lock(struct file *file, struct file_lock *fl)
4539{
4540 int res = 0;
4541 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4542 case FL_POSIX:
4543 res = posix_lock_file_wait(file, fl);
4544 break;
4545 case FL_FLOCK:
4546 res = flock_lock_file_wait(file, fl);
4547 break;
4548 default:
4549 BUG();
4550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return res;
4552}
4553
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004554struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004555 struct nfs_locku_args arg;
4556 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004557 struct nfs4_lock_state *lsp;
4558 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004559 struct file_lock fl;
4560 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004561 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004562};
4563
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004564static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4565 struct nfs_open_context *ctx,
4566 struct nfs4_lock_state *lsp,
4567 struct nfs_seqid *seqid)
4568{
4569 struct nfs4_unlockdata *p;
4570 struct inode *inode = lsp->ls_state->inode;
4571
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004572 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004573 if (p == NULL)
4574 return NULL;
4575 p->arg.fh = NFS_FH(inode);
4576 p->arg.fl = &p->fl;
4577 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004578 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004579 p->arg.stateid = &lsp->ls_stateid;
4580 p->lsp = lsp;
4581 atomic_inc(&lsp->ls_count);
4582 /* Ensure we don't close file until we're done freeing locks! */
4583 p->ctx = get_nfs_open_context(ctx);
4584 memcpy(&p->fl, fl, sizeof(p->fl));
4585 p->server = NFS_SERVER(inode);
4586 return p;
4587}
4588
Trond Myklebust06f814a2006-01-03 09:55:07 +01004589static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004590{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004591 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004592 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004593 nfs4_put_lock_state(calldata->lsp);
4594 put_nfs_open_context(calldata->ctx);
4595 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004596}
4597
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004598static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004599{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004600 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004601
Trond Myklebust14516c32010-07-31 14:29:06 -04004602 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4603 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004604 switch (task->tk_status) {
4605 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004606 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4607 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004608 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004609 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004610 case -NFS4ERR_BAD_STATEID:
4611 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004612 case -NFS4ERR_STALE_STATEID:
4613 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004614 break;
4615 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004616 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004617 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004618 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04004619 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004620}
4621
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004622static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004623{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004624 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004626 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004627 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04004628 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004629 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004630 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004631 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004632 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004633 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004634 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004635 &calldata->res.seq_res,
4636 task) != 0)
4637 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004638 return;
4639out_no_action:
4640 task->tk_action = NULL;
4641out_wait:
4642 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004645static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004646 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004647 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004648 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004649};
4650
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004651static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4652 struct nfs_open_context *ctx,
4653 struct nfs4_lock_state *lsp,
4654 struct nfs_seqid *seqid)
4655{
4656 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004657 struct rpc_message msg = {
4658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4659 .rpc_cred = ctx->cred,
4660 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004661 struct rpc_task_setup task_setup_data = {
4662 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004663 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004664 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004665 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004666 .flags = RPC_TASK_ASYNC,
4667 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004668
Frank Filz137d6ac2007-07-09 15:32:29 -07004669 /* Ensure this is an unlock - when canceling a lock, the
4670 * canceled lock is passed in, and it won't be an unlock.
4671 */
4672 fl->fl_type = F_UNLCK;
4673
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004674 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4675 if (data == NULL) {
4676 nfs_free_seqid(seqid);
4677 return ERR_PTR(-ENOMEM);
4678 }
4679
Trond Myklebust9d12b212012-01-17 22:04:25 -05004680 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004681 msg.rpc_argp = &data->arg;
4682 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004683 task_setup_data.callback_data = data;
4684 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004685}
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4688{
Trond Myklebust65b62a22013-02-07 10:54:07 -05004689 struct inode *inode = state->inode;
4690 struct nfs4_state_owner *sp = state->owner;
4691 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004692 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004693 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004694 struct rpc_task *task;
4695 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004696 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Trond Myklebust9b073572006-06-29 16:38:34 -04004698 status = nfs4_set_lock_state(state, request);
4699 /* Unlock _before_ we do the RPC call */
4700 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05004701 /* Exclude nfs_delegation_claim_locks() */
4702 mutex_lock(&sp->so_delegreturn_mutex);
4703 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004704 down_read(&nfsi->rwsem);
4705 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4706 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004707 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004708 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004709 }
4710 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05004711 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04004712 if (status != 0)
4713 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004714 /* Is this a delegated lock? */
4715 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004716 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004717 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004718 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004719 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004720 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004721 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004722 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004723 status = PTR_ERR(task);
4724 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004725 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004726 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004727 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004728out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004729 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004730 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004733struct nfs4_lockdata {
4734 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004735 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004736 struct nfs4_lock_state *lsp;
4737 struct nfs_open_context *ctx;
4738 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004739 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004740 int rpc_status;
4741 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004742 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004743};
4744
4745static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004746 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4747 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004748{
4749 struct nfs4_lockdata *p;
4750 struct inode *inode = lsp->ls_state->inode;
4751 struct nfs_server *server = NFS_SERVER(inode);
4752
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004754 if (p == NULL)
4755 return NULL;
4756
4757 p->arg.fh = NFS_FH(inode);
4758 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004759 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004760 if (p->arg.open_seqid == NULL)
4761 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004762 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004763 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004764 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004765 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004766 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004767 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004768 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004769 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004770 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004771 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004772 atomic_inc(&lsp->ls_count);
4773 p->ctx = get_nfs_open_context(ctx);
4774 memcpy(&p->fl, fl, sizeof(p->fl));
4775 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004776out_free_seqid:
4777 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004778out_free:
4779 kfree(p);
4780 return NULL;
4781}
4782
4783static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4784{
4785 struct nfs4_lockdata *data = calldata;
4786 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Harvey Harrison3110ff82008-05-02 13:42:44 -07004788 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004789 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004790 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004791 /* Do we need to do an open_to_lock_owner? */
4792 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004793 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004794 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004795 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004796 data->arg.open_stateid = &state->stateid;
4797 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004798 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004799 } else
4800 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004801 if (!nfs4_valid_open_stateid(state)) {
4802 data->rpc_status = -EBADF;
4803 task->tk_action = NULL;
4804 goto out_release_open_seqid;
4805 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004806 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004807 if (nfs4_setup_sequence(data->server,
4808 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004809 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004810 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04004811 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04004812out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04004813 nfs_release_seqid(data->arg.open_seqid);
4814out_release_lock_seqid:
4815 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05004816out_wait:
4817 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004818 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004819}
4820
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004821static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4822{
4823 struct nfs4_lockdata *data = calldata;
4824
Harvey Harrison3110ff82008-05-02 13:42:44 -07004825 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004826
Trond Myklebust14516c32010-07-31 14:29:06 -04004827 if (!nfs4_sequence_done(task, &data->res.seq_res))
4828 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004829
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004830 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004831 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004832 if (data->rpc_status == 0)
4833 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4834 else
4835 goto out;
4836 }
4837 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004838 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04004839 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04004840 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004841 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004842out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004843 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004844}
4845
4846static void nfs4_lock_release(void *calldata)
4847{
4848 struct nfs4_lockdata *data = calldata;
4849
Harvey Harrison3110ff82008-05-02 13:42:44 -07004850 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004851 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004852 if (data->cancelled != 0) {
4853 struct rpc_task *task;
4854 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4855 data->arg.lock_seqid);
4856 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004857 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004858 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004859 } else
4860 nfs_free_seqid(data->arg.lock_seqid);
4861 nfs4_put_lock_state(data->lsp);
4862 put_nfs_open_context(data->ctx);
4863 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004864 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004865}
4866
4867static const struct rpc_call_ops nfs4_lock_ops = {
4868 .rpc_call_prepare = nfs4_lock_prepare,
4869 .rpc_call_done = nfs4_lock_done,
4870 .rpc_release = nfs4_lock_release,
4871};
4872
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004873static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4874{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004875 switch (error) {
4876 case -NFS4ERR_ADMIN_REVOKED:
4877 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004878 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004879 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04004880 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004881 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004882 break;
4883 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004884 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004885 case -NFS4ERR_EXPIRED:
4886 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004887 };
4888}
4889
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004890static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004891{
4892 struct nfs4_lockdata *data;
4893 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004894 struct rpc_message msg = {
4895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4896 .rpc_cred = state->owner->so_cred,
4897 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004898 struct rpc_task_setup task_setup_data = {
4899 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004900 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004901 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004902 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004903 .flags = RPC_TASK_ASYNC,
4904 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004905 int ret;
4906
Harvey Harrison3110ff82008-05-02 13:42:44 -07004907 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004908 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004909 fl->fl_u.nfs4_fl.owner,
4910 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004911 if (data == NULL)
4912 return -ENOMEM;
4913 if (IS_SETLKW(cmd))
4914 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004915 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004916 msg.rpc_argp = &data->arg;
4917 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004918 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04004919 if (recovery_type > NFS_LOCK_NEW) {
4920 if (recovery_type == NFS_LOCK_RECLAIM)
4921 data->arg.reclaim = NFS_LOCK_RECLAIM;
4922 nfs4_set_sequence_privileged(&data->arg.seq_args);
4923 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04004924 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004925 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004926 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004927 ret = nfs4_wait_for_completion_rpc_task(task);
4928 if (ret == 0) {
4929 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004930 if (ret)
4931 nfs4_handle_setlk_error(data->server, data->lsp,
4932 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004933 } else
4934 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004935 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004936 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4941{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004942 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004943 struct nfs4_exception exception = {
4944 .inode = state->inode,
4945 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004946 int err;
4947
4948 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004949 /* Cache the lock if possible... */
4950 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4951 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004952 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004953 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004954 break;
4955 nfs4_handle_exception(server, err, &exception);
4956 } while (exception.retry);
4957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958}
4959
4960static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4961{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004962 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004963 struct nfs4_exception exception = {
4964 .inode = state->inode,
4965 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004966 int err;
4967
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004968 err = nfs4_set_lock_state(state, request);
4969 if (err != 0)
4970 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004971 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004972 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4973 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004974 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004975 switch (err) {
4976 default:
4977 goto out;
4978 case -NFS4ERR_GRACE:
4979 case -NFS4ERR_DELAY:
4980 nfs4_handle_exception(server, err, &exception);
4981 err = 0;
4982 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004983 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004984out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004985 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
4987
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004988#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04004989/**
4990 * nfs41_check_expired_locks - possibly free a lock stateid
4991 *
4992 * @state: NFSv4 state for an inode
4993 *
4994 * Returns NFS_OK if recovery for this stateid is now finished.
4995 * Otherwise a negative NFS4ERR value is returned.
4996 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004997static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004998{
Chuck Levereb64cf92012-07-11 16:30:05 -04004999 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005000 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005001 struct nfs_server *server = NFS_SERVER(state->inode);
5002
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005003 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005004 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005005 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5006 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005007 /* Free the stateid unless the server
5008 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005009 if (status != -NFS4ERR_BAD_STATEID)
5010 nfs41_free_stateid(server,
5011 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005012 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005013 ret = status;
5014 }
5015 }
5016 };
5017
5018 return ret;
5019}
5020
5021static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5022{
5023 int status = NFS_OK;
5024
5025 if (test_bit(LK_STATE_IN_USE, &state->flags))
5026 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005027 if (status != NFS_OK)
5028 status = nfs4_lock_expired(state, request);
5029 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005030}
5031#endif
5032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5034{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005035 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005036 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005037 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005038 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005039 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005041 if ((fl_flags & FL_POSIX) &&
5042 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5043 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005044 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005045 status = nfs4_set_lock_state(state, request);
5046 if (status != 0)
5047 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005048 request->fl_flags |= FL_ACCESS;
5049 status = do_vfs_lock(request->fl_file, request);
5050 if (status < 0)
5051 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005052 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005053 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005054 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005055 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005056 request->fl_flags = fl_flags & ~FL_SLEEP;
5057 status = do_vfs_lock(request->fl_file, request);
5058 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005059 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005060 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5061 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005062 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005063 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005064 goto out;
5065 down_read(&nfsi->rwsem);
5066 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5067 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005068 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005069 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005070 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005071 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005072 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005073 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5074 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005075out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005076 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005077out:
5078 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 return status;
5080}
5081
5082static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5083{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005084 struct nfs4_exception exception = {
5085 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005086 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 int err;
5089
5090 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005091 err = _nfs4_proc_setlk(state, cmd, request);
5092 if (err == -NFS4ERR_DENIED)
5093 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005095 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 } while (exception.retry);
5097 return err;
5098}
5099
5100static int
5101nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5102{
5103 struct nfs_open_context *ctx;
5104 struct nfs4_state *state;
5105 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5106 int status;
5107
5108 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005109 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 state = ctx->state;
5111
5112 if (request->fl_start < 0 || request->fl_end < 0)
5113 return -EINVAL;
5114
Trond Myklebustd9531262009-07-21 19:22:38 -04005115 if (IS_GETLK(cmd)) {
5116 if (state != NULL)
5117 return nfs4_proc_getlk(state, F_GETLK, request);
5118 return 0;
5119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
5121 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5122 return -EINVAL;
5123
Trond Myklebustd9531262009-07-21 19:22:38 -04005124 if (request->fl_type == F_UNLCK) {
5125 if (state != NULL)
5126 return nfs4_proc_unlck(state, cmd, request);
5127 return 0;
5128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Trond Myklebustd9531262009-07-21 19:22:38 -04005130 if (state == NULL)
5131 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005132 /*
5133 * Don't rely on the VFS having checked the file open mode,
5134 * since it won't do this for flock() locks.
5135 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005136 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005137 case F_RDLCK:
5138 if (!(filp->f_mode & FMODE_READ))
5139 return -EBADF;
5140 break;
5141 case F_WRLCK:
5142 if (!(filp->f_mode & FMODE_WRITE))
5143 return -EBADF;
5144 }
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 do {
5147 status = nfs4_proc_setlk(state, cmd, request);
5148 if ((status != -EAGAIN) || IS_SETLK(cmd))
5149 break;
5150 timeout = nfs4_set_lock_task_retry(timeout);
5151 status = -ERESTARTSYS;
5152 if (signalled())
5153 break;
5154 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return status;
5156}
5157
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005158int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005159{
5160 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005161 int err;
5162
5163 err = nfs4_set_lock_state(state, fl);
5164 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005165 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005166 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005167 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005168}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005169
Trond Myklebustcf470c32012-03-07 13:49:12 -05005170struct nfs_release_lockowner_data {
5171 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005172 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005173 struct nfs_release_lockowner_args args;
5174};
5175
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005176static void nfs4_release_lockowner_release(void *calldata)
5177{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005178 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005179 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005180 kfree(calldata);
5181}
5182
Trond Myklebust17280172012-03-11 13:11:00 -04005183static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005184 .rpc_release = nfs4_release_lockowner_release,
5185};
5186
Trond Myklebustcf470c32012-03-07 13:49:12 -05005187int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005188{
5189 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005190 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005191 struct rpc_message msg = {
5192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5193 };
5194
5195 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005196 return -EINVAL;
5197 data = kmalloc(sizeof(*data), GFP_NOFS);
5198 if (!data)
5199 return -ENOMEM;
5200 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005201 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005202 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5203 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5204 data->args.lock_owner.s_dev = server->s_dev;
5205 msg.rpc_argp = &data->args;
5206 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5207 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005208}
5209
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005210#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5211
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005212static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5213 const void *buf, size_t buflen,
5214 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005215{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005216 if (strcmp(key, "") != 0)
5217 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005218
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005219 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005220}
5221
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005222static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5223 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005224{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005225 if (strcmp(key, "") != 0)
5226 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005227
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005228 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005229}
5230
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005231static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5232 size_t list_len, const char *name,
5233 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005234{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005235 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005236
J. Bruce Fields096455a2006-03-20 23:23:42 -05005237 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5238 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005239
5240 if (list && len <= list_len)
5241 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005242 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005243}
5244
Andy Adamson533eb462011-06-13 18:25:56 -04005245/*
5246 * nfs_fhget will use either the mounted_on_fileid or the fileid
5247 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005248static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5249{
Andy Adamson533eb462011-06-13 18:25:56 -04005250 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5251 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5252 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005253 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005254 return;
5255
5256 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005257 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005258 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5259 fattr->nlink = 2;
5260}
5261
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005262static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5263 const struct qstr *name,
5264 struct nfs4_fs_locations *fs_locations,
5265 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005266{
5267 struct nfs_server *server = NFS_SERVER(dir);
5268 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005269 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005270 };
5271 struct nfs4_fs_locations_arg args = {
5272 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005273 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005274 .page = page,
5275 .bitmask = bitmask,
5276 };
Benny Halevy22958462009-04-01 09:22:02 -04005277 struct nfs4_fs_locations_res res = {
5278 .fs_locations = fs_locations,
5279 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005280 struct rpc_message msg = {
5281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5282 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005283 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005284 };
5285 int status;
5286
Harvey Harrison3110ff82008-05-02 13:42:44 -07005287 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005288
5289 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5290 * is not supported */
5291 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5292 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5293 else
5294 bitmask[0] |= FATTR4_WORD0_FILEID;
5295
Trond Myklebustc228fd32007-01-13 02:28:11 -05005296 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005297 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005298 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005299 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005300 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005301 return status;
5302}
5303
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005304int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5305 const struct qstr *name,
5306 struct nfs4_fs_locations *fs_locations,
5307 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005308{
5309 struct nfs4_exception exception = { };
5310 int err;
5311 do {
5312 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005313 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005314 &exception);
5315 } while (exception.retry);
5316 return err;
5317}
5318
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005319static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5320{
5321 int status;
5322 struct nfs4_secinfo_arg args = {
5323 .dir_fh = NFS_FH(dir),
5324 .name = name,
5325 };
5326 struct nfs4_secinfo_res res = {
5327 .flavors = flavors,
5328 };
5329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5331 .rpc_argp = &args,
5332 .rpc_resp = &res,
5333 };
5334
5335 dprintk("NFS call secinfo %s\n", name->name);
5336 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5337 dprintk("NFS reply secinfo: %d\n", status);
5338 return status;
5339}
5340
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005341int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5342 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005343{
5344 struct nfs4_exception exception = { };
5345 int err;
5346 do {
5347 err = nfs4_handle_exception(NFS_SERVER(dir),
5348 _nfs4_proc_secinfo(dir, name, flavors),
5349 &exception);
5350 } while (exception.retry);
5351 return err;
5352}
5353
Andy Adamson557134a2009-04-01 09:21:53 -04005354#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005355/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005356 * Check the exchange flags returned by the server for invalid flags, having
5357 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5358 * DS flags set.
5359 */
5360static int nfs4_check_cl_exchange_flags(u32 flags)
5361{
5362 if (flags & ~EXCHGID4_FLAG_MASK_R)
5363 goto out_inval;
5364 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5365 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5366 goto out_inval;
5367 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5368 goto out_inval;
5369 return NFS_OK;
5370out_inval:
5371 return -NFS4ERR_INVAL;
5372}
5373
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005374static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005375nfs41_same_server_scope(struct nfs41_server_scope *a,
5376 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005377{
5378 if (a->server_scope_sz == b->server_scope_sz &&
5379 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5380 return true;
5381
5382 return false;
5383}
5384
Andy Adamson357f54d2010-12-14 10:11:57 -05005385/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005386 * nfs4_proc_bind_conn_to_session()
5387 *
5388 * The 4.1 client currently uses the same TCP connection for the
5389 * fore and backchannel.
5390 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005391int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005392{
5393 int status;
5394 struct nfs41_bind_conn_to_session_res res;
5395 struct rpc_message msg = {
5396 .rpc_proc =
5397 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5398 .rpc_argp = clp,
5399 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005400 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005401 };
5402
5403 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005404
5405 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5406 if (unlikely(res.session == NULL)) {
5407 status = -ENOMEM;
5408 goto out;
5409 }
5410
5411 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5412 if (status == 0) {
5413 if (memcmp(res.session->sess_id.data,
5414 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5415 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5416 status = -EIO;
5417 goto out_session;
5418 }
5419 if (res.dir != NFS4_CDFS4_BOTH) {
5420 dprintk("NFS: %s: Unexpected direction from server\n",
5421 __func__);
5422 status = -EIO;
5423 goto out_session;
5424 }
5425 if (res.use_conn_in_rdma_mode) {
5426 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5427 __func__);
5428 status = -EIO;
5429 goto out_session;
5430 }
5431 }
5432out_session:
5433 kfree(res.session);
5434out:
5435 dprintk("<-- %s status= %d\n", __func__, status);
5436 return status;
5437}
5438
5439/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005440 * nfs4_proc_exchange_id()
5441 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005442 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5443 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005444 * Since the clientid has expired, all compounds using sessions
5445 * associated with the stale clientid will be returning
5446 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5447 * be in some phase of session reset.
5448 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005449int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005450{
5451 nfs4_verifier verifier;
5452 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005453 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005454 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005455 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005456 };
5457 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005458 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005459 };
5460 int status;
5461 struct rpc_message msg = {
5462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5463 .rpc_argp = &args,
5464 .rpc_resp = &res,
5465 .rpc_cred = cred,
5466 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005467
Chuck Leverf0920752012-05-21 22:45:41 -04005468 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005469 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5470 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005471 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5472 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5473 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005474
Chuck Leveracdeb692012-05-21 22:46:16 -04005475 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005476 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005477 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005478 status = -ENOMEM;
5479 goto out;
5480 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005481
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005482 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005483 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005484 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005485 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005486 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005487 }
5488
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005489 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005490 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005491 status = -ENOMEM;
5492 goto out_server_scope;
5493 }
5494
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005495 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005496 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005497 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005498
Chuck Lever177313f2012-05-21 22:44:58 -04005499 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005500 clp->cl_clientid = res.clientid;
5501 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5502 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5503 clp->cl_seqid = res.seqid;
5504
Chuck Leveracdeb692012-05-21 22:46:16 -04005505 kfree(clp->cl_serverowner);
5506 clp->cl_serverowner = res.server_owner;
5507 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005508
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005509 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005510 kfree(clp->cl_implid);
5511 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005512
Chuck Lever177313f2012-05-21 22:44:58 -04005513 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005514 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005515 res.server_scope)) {
5516 dprintk("%s: server_scope mismatch detected\n",
5517 __func__);
5518 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005519 kfree(clp->cl_serverscope);
5520 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005521 }
5522
Chuck Lever177313f2012-05-21 22:44:58 -04005523 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005524 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005525 goto out;
5526 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005527 } else
5528 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005529
Chuck Leveracdeb692012-05-21 22:46:16 -04005530out_server_owner:
5531 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005532out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005533 kfree(res.server_scope);
5534out:
Chuck Lever177313f2012-05-21 22:44:58 -04005535 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005536 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005537 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005538 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005539 clp->cl_implid->date.seconds,
5540 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005541 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005542 return status;
5543}
5544
Trond Myklebust66245532012-05-25 17:18:09 -04005545static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5546 struct rpc_cred *cred)
5547{
5548 struct rpc_message msg = {
5549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5550 .rpc_argp = clp,
5551 .rpc_cred = cred,
5552 };
5553 int status;
5554
5555 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5556 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04005557 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04005558 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5559 return status;
5560}
5561
5562static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5563 struct rpc_cred *cred)
5564{
5565 unsigned int loop;
5566 int ret;
5567
5568 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5569 ret = _nfs4_proc_destroy_clientid(clp, cred);
5570 switch (ret) {
5571 case -NFS4ERR_DELAY:
5572 case -NFS4ERR_CLIENTID_BUSY:
5573 ssleep(1);
5574 break;
5575 default:
5576 return ret;
5577 }
5578 }
5579 return 0;
5580}
5581
5582int nfs4_destroy_clientid(struct nfs_client *clp)
5583{
5584 struct rpc_cred *cred;
5585 int ret = 0;
5586
5587 if (clp->cl_mvops->minor_version < 1)
5588 goto out;
5589 if (clp->cl_exchange_flags == 0)
5590 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04005591 if (clp->cl_preserve_clid)
5592 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04005593 cred = nfs4_get_exchange_id_cred(clp);
5594 ret = nfs4_proc_destroy_clientid(clp, cred);
5595 if (cred)
5596 put_rpccred(cred);
5597 switch (ret) {
5598 case 0:
5599 case -NFS4ERR_STALE_CLIENTID:
5600 clp->cl_exchange_flags = 0;
5601 }
5602out:
5603 return ret;
5604}
5605
Andy Adamson2050f0c2009-04-01 09:22:30 -04005606struct nfs4_get_lease_time_data {
5607 struct nfs4_get_lease_time_args *args;
5608 struct nfs4_get_lease_time_res *res;
5609 struct nfs_client *clp;
5610};
5611
5612static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5613 void *calldata)
5614{
Andy Adamson2050f0c2009-04-01 09:22:30 -04005615 struct nfs4_get_lease_time_data *data =
5616 (struct nfs4_get_lease_time_data *)calldata;
5617
5618 dprintk("--> %s\n", __func__);
5619 /* just setup sequence, do not trigger session recovery
5620 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005621 nfs41_setup_sequence(data->clp->cl_session,
5622 &data->args->la_seq_args,
5623 &data->res->lr_seq_res,
5624 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005625 dprintk("<-- %s\n", __func__);
5626}
5627
5628/*
5629 * Called from nfs4_state_manager thread for session setup, so don't recover
5630 * from sequence operation or clientid errors.
5631 */
5632static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5633{
5634 struct nfs4_get_lease_time_data *data =
5635 (struct nfs4_get_lease_time_data *)calldata;
5636
5637 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005638 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5639 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005640 switch (task->tk_status) {
5641 case -NFS4ERR_DELAY:
5642 case -NFS4ERR_GRACE:
5643 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5644 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5645 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005646 /* fall through */
5647 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005648 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005649 return;
5650 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005651 dprintk("<-- %s\n", __func__);
5652}
5653
Trond Myklebust17280172012-03-11 13:11:00 -04005654static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005655 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5656 .rpc_call_done = nfs4_get_lease_time_done,
5657};
5658
5659int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5660{
5661 struct rpc_task *task;
5662 struct nfs4_get_lease_time_args args;
5663 struct nfs4_get_lease_time_res res = {
5664 .lr_fsinfo = fsinfo,
5665 };
5666 struct nfs4_get_lease_time_data data = {
5667 .args = &args,
5668 .res = &res,
5669 .clp = clp,
5670 };
5671 struct rpc_message msg = {
5672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5673 .rpc_argp = &args,
5674 .rpc_resp = &res,
5675 };
5676 struct rpc_task_setup task_setup = {
5677 .rpc_client = clp->cl_rpcclient,
5678 .rpc_message = &msg,
5679 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005680 .callback_data = &data,
5681 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005682 };
5683 int status;
5684
Trond Myklebust9d12b212012-01-17 22:04:25 -05005685 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005686 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005687 dprintk("--> %s\n", __func__);
5688 task = rpc_run_task(&task_setup);
5689
5690 if (IS_ERR(task))
5691 status = PTR_ERR(task);
5692 else {
5693 status = task->tk_status;
5694 rpc_put_task(task);
5695 }
5696 dprintk("<-- %s return %d\n", __func__, status);
5697
5698 return status;
5699}
5700
Andy Adamsonfc931582009-04-01 09:22:31 -04005701/*
5702 * Initialize the values to be used by the client in CREATE_SESSION
5703 * If nfs4_init_session set the fore channel request and response sizes,
5704 * use them.
5705 *
5706 * Set the back channel max_resp_sz_cached to zero to force the client to
5707 * always set csa_cachethis to FALSE because the current implementation
5708 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5709 */
5710static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5711{
5712 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05005713 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5714 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005715
5716 if (mxrqst_sz == 0)
5717 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5718 if (mxresp_sz == 0)
5719 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5720 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005721 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5722 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005723 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005724 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005725
5726 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005727 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005728 __func__,
5729 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005730 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005731
5732 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005733 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5734 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5735 args->bc_attrs.max_resp_sz_cached = 0;
5736 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5737 args->bc_attrs.max_reqs = 1;
5738
5739 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5740 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5741 __func__,
5742 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5743 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5744 args->bc_attrs.max_reqs);
5745}
5746
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005747static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005748{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005749 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5750 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5751
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005752 if (rcvd->max_resp_sz > sent->max_resp_sz)
5753 return -EINVAL;
5754 /*
5755 * Our requested max_ops is the minimum we need; we're not
5756 * prepared to break up compounds into smaller pieces than that.
5757 * So, no point even trying to continue if the server won't
5758 * cooperate:
5759 */
5760 if (rcvd->max_ops < sent->max_ops)
5761 return -EINVAL;
5762 if (rcvd->max_reqs == 0)
5763 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005764 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5765 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005766 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005767}
5768
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005769static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5770{
5771 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5772 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005773
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005774 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5775 return -EINVAL;
5776 if (rcvd->max_resp_sz < sent->max_resp_sz)
5777 return -EINVAL;
5778 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5779 return -EINVAL;
5780 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005781 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005782 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005783 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005784 return -EINVAL;
5785 return 0;
5786}
Andy Adamson8d353012009-04-01 09:22:32 -04005787
Andy Adamson8d353012009-04-01 09:22:32 -04005788static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5789 struct nfs4_session *session)
5790{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005791 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005792
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005793 ret = nfs4_verify_fore_channel_attrs(args, session);
5794 if (ret)
5795 return ret;
5796 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005797}
5798
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005799static int _nfs4_proc_create_session(struct nfs_client *clp,
5800 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005801{
5802 struct nfs4_session *session = clp->cl_session;
5803 struct nfs41_create_session_args args = {
5804 .client = clp,
5805 .cb_program = NFS4_CALLBACK,
5806 };
5807 struct nfs41_create_session_res res = {
5808 .client = clp,
5809 };
5810 struct rpc_message msg = {
5811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5812 .rpc_argp = &args,
5813 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005814 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005815 };
5816 int status;
5817
5818 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005819 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005820
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005821 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005822
Trond Myklebust43095d32012-11-20 11:13:12 -05005823 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04005824 /* Verify the session's negotiated channel_attrs values */
5825 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005826 /* Increment the clientid slot sequence id */
5827 clp->cl_seqid++;
5828 }
5829
5830 return status;
5831}
5832
5833/*
5834 * Issues a CREATE_SESSION operation to the server.
5835 * It is the responsibility of the caller to verify the session is
5836 * expired before calling this routine.
5837 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005838int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005839{
5840 int status;
5841 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005842 struct nfs4_session *session = clp->cl_session;
5843
5844 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5845
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005846 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005847 if (status)
5848 goto out;
5849
Andy Adamsonaacd5532011-11-09 13:58:21 -05005850 /* Init or reset the session slot tables */
5851 status = nfs4_setup_session_slot_tables(session);
5852 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005853 if (status)
5854 goto out;
5855
5856 ptr = (unsigned *)&session->sess_id.data[0];
5857 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5858 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005859out:
5860 dprintk("<-- %s\n", __func__);
5861 return status;
5862}
5863
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005864/*
5865 * Issue the over-the-wire RPC DESTROY_SESSION.
5866 * The caller must serialize access to this routine.
5867 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005868int nfs4_proc_destroy_session(struct nfs4_session *session,
5869 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005870{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005871 struct rpc_message msg = {
5872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5873 .rpc_argp = session,
5874 .rpc_cred = cred,
5875 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005876 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005877
5878 dprintk("--> nfs4_proc_destroy_session\n");
5879
5880 /* session is still being setup */
5881 if (session->clp->cl_cons_state != NFS_CS_READY)
5882 return status;
5883
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005884 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005885
5886 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04005887 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005888 "Session has been destroyed regardless...\n", status);
5889
5890 dprintk("<-- nfs4_proc_destroy_session\n");
5891 return status;
5892}
5893
Trond Myklebust7b38c362012-05-23 13:23:31 -04005894/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005895 * Renew the cl_session lease.
5896 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005897struct nfs4_sequence_data {
5898 struct nfs_client *clp;
5899 struct nfs4_sequence_args args;
5900 struct nfs4_sequence_res res;
5901};
5902
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005903static void nfs41_sequence_release(void *data)
5904{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005905 struct nfs4_sequence_data *calldata = data;
5906 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005907
Alexandros Batsakis71358402010-02-05 03:45:05 -08005908 if (atomic_read(&clp->cl_count) > 1)
5909 nfs4_schedule_state_renewal(clp);
5910 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005911 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005912}
5913
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005914static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5915{
5916 switch(task->tk_status) {
5917 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005918 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5919 return -EAGAIN;
5920 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005921 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005922 }
5923 return 0;
5924}
5925
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005926static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005927{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005928 struct nfs4_sequence_data *calldata = data;
5929 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005930
Trond Myklebust14516c32010-07-31 14:29:06 -04005931 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5932 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005933
5934 if (task->tk_status < 0) {
5935 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005936 if (atomic_read(&clp->cl_count) == 1)
5937 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005938
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005939 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5940 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005941 return;
5942 }
5943 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005944 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005945out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005946 dprintk("<-- %s\n", __func__);
5947}
5948
5949static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5950{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005951 struct nfs4_sequence_data *calldata = data;
5952 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005953 struct nfs4_sequence_args *args;
5954 struct nfs4_sequence_res *res;
5955
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005956 args = task->tk_msg.rpc_argp;
5957 res = task->tk_msg.rpc_resp;
5958
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005959 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005960}
5961
5962static const struct rpc_call_ops nfs41_sequence_ops = {
5963 .rpc_call_done = nfs41_sequence_call_done,
5964 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005965 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005966};
5967
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005968static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5969 struct rpc_cred *cred,
5970 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005971{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005972 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005973 struct rpc_message msg = {
5974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5975 .rpc_cred = cred,
5976 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005977 struct rpc_task_setup task_setup_data = {
5978 .rpc_client = clp->cl_rpcclient,
5979 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005980 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005981 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005982 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005983
Alexandros Batsakis71358402010-02-05 03:45:05 -08005984 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005985 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005986 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005987 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005988 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005989 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005990 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005991 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005992 if (is_privileged)
5993 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005994 msg.rpc_argp = &calldata->args;
5995 msg.rpc_resp = &calldata->res;
5996 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005997 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005998
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005999 return rpc_run_task(&task_setup_data);
6000}
6001
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006002static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006003{
6004 struct rpc_task *task;
6005 int ret = 0;
6006
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006007 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6008 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006009 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006010 if (IS_ERR(task))
6011 ret = PTR_ERR(task);
6012 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006013 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006014 dprintk("<-- %s status=%d\n", __func__, ret);
6015 return ret;
6016}
6017
6018static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6019{
6020 struct rpc_task *task;
6021 int ret;
6022
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006023 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006024 if (IS_ERR(task)) {
6025 ret = PTR_ERR(task);
6026 goto out;
6027 }
6028 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006029 if (!ret) {
6030 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6031
6032 if (task->tk_status == 0)
6033 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006034 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006035 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006036 rpc_put_task(task);
6037out:
6038 dprintk("<-- %s status=%d\n", __func__, ret);
6039 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006040}
6041
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006042struct nfs4_reclaim_complete_data {
6043 struct nfs_client *clp;
6044 struct nfs41_reclaim_complete_args arg;
6045 struct nfs41_reclaim_complete_res res;
6046};
6047
6048static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6049{
6050 struct nfs4_reclaim_complete_data *calldata = data;
6051
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006052 nfs41_setup_sequence(calldata->clp->cl_session,
6053 &calldata->arg.seq_args,
6054 &calldata->res.seq_res,
6055 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006056}
6057
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006058static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6059{
6060 switch(task->tk_status) {
6061 case 0:
6062 case -NFS4ERR_COMPLETE_ALREADY:
6063 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6064 break;
6065 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006066 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006067 /* fall through */
6068 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006069 return -EAGAIN;
6070 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006071 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006072 }
6073 return 0;
6074}
6075
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006076static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6077{
6078 struct nfs4_reclaim_complete_data *calldata = data;
6079 struct nfs_client *clp = calldata->clp;
6080 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6081
6082 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006083 if (!nfs41_sequence_done(task, res))
6084 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006085
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006086 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6087 rpc_restart_call_prepare(task);
6088 return;
6089 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006090 dprintk("<-- %s\n", __func__);
6091}
6092
6093static void nfs4_free_reclaim_complete_data(void *data)
6094{
6095 struct nfs4_reclaim_complete_data *calldata = data;
6096
6097 kfree(calldata);
6098}
6099
6100static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6101 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6102 .rpc_call_done = nfs4_reclaim_complete_done,
6103 .rpc_release = nfs4_free_reclaim_complete_data,
6104};
6105
6106/*
6107 * Issue a global reclaim complete.
6108 */
6109static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6110{
6111 struct nfs4_reclaim_complete_data *calldata;
6112 struct rpc_task *task;
6113 struct rpc_message msg = {
6114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6115 };
6116 struct rpc_task_setup task_setup_data = {
6117 .rpc_client = clp->cl_rpcclient,
6118 .rpc_message = &msg,
6119 .callback_ops = &nfs4_reclaim_complete_call_ops,
6120 .flags = RPC_TASK_ASYNC,
6121 };
6122 int status = -ENOMEM;
6123
6124 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006125 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006126 if (calldata == NULL)
6127 goto out;
6128 calldata->clp = clp;
6129 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006130
Trond Myklebust9d12b212012-01-17 22:04:25 -05006131 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006132 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006133 msg.rpc_argp = &calldata->arg;
6134 msg.rpc_resp = &calldata->res;
6135 task_setup_data.callback_data = calldata;
6136 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006137 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006138 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006139 goto out;
6140 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006141 status = nfs4_wait_for_completion_rpc_task(task);
6142 if (status == 0)
6143 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006144 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006145 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006146out:
6147 dprintk("<-- %s status=%d\n", __func__, status);
6148 return status;
6149}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006150
6151static void
6152nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6153{
6154 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006155 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006156 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006157
6158 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006159 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6160 * right now covering the LAYOUTGET we are about to send.
6161 * However, that is not so catastrophic, and there seems
6162 * to be no way to prevent it completely.
6163 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006164 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006165 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006166 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006167 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6168 NFS_I(lgp->args.inode)->layout,
6169 lgp->args.ctx->state)) {
6170 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006171 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172}
6173
6174static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6175{
6176 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006177 struct inode *inode = lgp->args.inode;
6178 struct nfs_server *server = NFS_SERVER(inode);
6179 struct pnfs_layout_hdr *lo;
6180 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006181 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006182
6183 dprintk("--> %s\n", __func__);
6184
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006185 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006186 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006187
6188 switch (task->tk_status) {
6189 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006190 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006191 case -NFS4ERR_LAYOUTTRYLATER:
6192 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006193 timeo = rpc_get_timeout(task->tk_client);
6194 giveup = lgp->args.timestamp + timeo;
6195 if (time_after(giveup, jiffies))
6196 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006197 break;
6198 case -NFS4ERR_EXPIRED:
6199 case -NFS4ERR_BAD_STATEID:
6200 spin_lock(&inode->i_lock);
6201 lo = NFS_I(inode)->layout;
6202 if (!lo || list_empty(&lo->plh_segs)) {
6203 spin_unlock(&inode->i_lock);
6204 /* If the open stateid was bad, then recover it. */
6205 state = lgp->args.ctx->state;
6206 } else {
6207 LIST_HEAD(head);
6208
6209 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6210 spin_unlock(&inode->i_lock);
6211 /* Mark the bad layout state as invalid, then
6212 * retry using the open stateid. */
6213 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006214 }
6215 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006216 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6217 rpc_restart_call_prepare(task);
6218out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006219 dprintk("<-- %s\n", __func__);
6220}
6221
Idan Kedar85541162012-08-02 11:47:10 +03006222static size_t max_response_pages(struct nfs_server *server)
6223{
6224 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6225 return nfs_page_array_len(0, max_resp_sz);
6226}
6227
6228static void nfs4_free_pages(struct page **pages, size_t size)
6229{
6230 int i;
6231
6232 if (!pages)
6233 return;
6234
6235 for (i = 0; i < size; i++) {
6236 if (!pages[i])
6237 break;
6238 __free_page(pages[i]);
6239 }
6240 kfree(pages);
6241}
6242
6243static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6244{
6245 struct page **pages;
6246 int i;
6247
6248 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6249 if (!pages) {
6250 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6251 return NULL;
6252 }
6253
6254 for (i = 0; i < size; i++) {
6255 pages[i] = alloc_page(gfp_flags);
6256 if (!pages[i]) {
6257 dprintk("%s: failed to allocate page\n", __func__);
6258 nfs4_free_pages(pages, size);
6259 return NULL;
6260 }
6261 }
6262
6263 return pages;
6264}
6265
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006266static void nfs4_layoutget_release(void *calldata)
6267{
6268 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006269 struct inode *inode = lgp->args.inode;
6270 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006271 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006272
6273 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006274 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006275 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006276 put_nfs_open_context(lgp->args.ctx);
6277 kfree(calldata);
6278 dprintk("<-- %s\n", __func__);
6279}
6280
6281static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6282 .rpc_call_prepare = nfs4_layoutget_prepare,
6283 .rpc_call_done = nfs4_layoutget_done,
6284 .rpc_release = nfs4_layoutget_release,
6285};
6286
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006287struct pnfs_layout_segment *
6288nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006289{
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 struct rpc_task *task;
6294 struct rpc_message msg = {
6295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6296 .rpc_argp = &lgp->args,
6297 .rpc_resp = &lgp->res,
6298 };
6299 struct rpc_task_setup task_setup_data = {
6300 .rpc_client = server->client,
6301 .rpc_message = &msg,
6302 .callback_ops = &nfs4_layoutget_call_ops,
6303 .callback_data = lgp,
6304 .flags = RPC_TASK_ASYNC,
6305 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006306 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006307 int status = 0;
6308
6309 dprintk("--> %s\n", __func__);
6310
Idan Kedar85541162012-08-02 11:47:10 +03006311 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6312 if (!lgp->args.layout.pages) {
6313 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006314 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006315 }
6316 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006317 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006318
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006319 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006320 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006321 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006322
6323 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6324 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6325
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006326 task = rpc_run_task(&task_setup_data);
6327 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006328 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006329 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006330 if (status == 0)
6331 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006332 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6333 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006334 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006335 rpc_put_task(task);
6336 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006337 if (status)
6338 return ERR_PTR(status);
6339 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006340}
6341
Benny Halevycbe82602011-05-22 19:52:37 +03006342static void
6343nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6344{
6345 struct nfs4_layoutreturn *lrp = calldata;
6346
6347 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006348 nfs41_setup_sequence(lrp->clp->cl_session,
6349 &lrp->args.seq_args,
6350 &lrp->res.seq_res,
6351 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006352}
6353
6354static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6355{
6356 struct nfs4_layoutreturn *lrp = calldata;
6357 struct nfs_server *server;
6358
6359 dprintk("--> %s\n", __func__);
6360
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006361 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006362 return;
6363
6364 server = NFS_SERVER(lrp->args.inode);
6365 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006366 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006367 return;
6368 }
Benny Halevycbe82602011-05-22 19:52:37 +03006369 dprintk("<-- %s\n", __func__);
6370}
6371
6372static void nfs4_layoutreturn_release(void *calldata)
6373{
6374 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006375 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006376
6377 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006378 spin_lock(&lo->plh_inode->i_lock);
6379 if (lrp->res.lrs_present)
6380 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6381 lo->plh_block_lgets--;
6382 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006383 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006384 kfree(calldata);
6385 dprintk("<-- %s\n", __func__);
6386}
6387
6388static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6389 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6390 .rpc_call_done = nfs4_layoutreturn_done,
6391 .rpc_release = nfs4_layoutreturn_release,
6392};
6393
6394int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6395{
6396 struct rpc_task *task;
6397 struct rpc_message msg = {
6398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6399 .rpc_argp = &lrp->args,
6400 .rpc_resp = &lrp->res,
6401 };
6402 struct rpc_task_setup task_setup_data = {
6403 .rpc_client = lrp->clp->cl_rpcclient,
6404 .rpc_message = &msg,
6405 .callback_ops = &nfs4_layoutreturn_call_ops,
6406 .callback_data = lrp,
6407 };
6408 int status;
6409
6410 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006411 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006412 task = rpc_run_task(&task_setup_data);
6413 if (IS_ERR(task))
6414 return PTR_ERR(task);
6415 status = task->tk_status;
6416 dprintk("<-- %s status=%d\n", __func__, status);
6417 rpc_put_task(task);
6418 return status;
6419}
6420
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006421/*
6422 * Retrieve the list of Data Server devices from the MDS.
6423 */
6424static int _nfs4_getdevicelist(struct nfs_server *server,
6425 const struct nfs_fh *fh,
6426 struct pnfs_devicelist *devlist)
6427{
6428 struct nfs4_getdevicelist_args args = {
6429 .fh = fh,
6430 .layoutclass = server->pnfs_curr_ld->id,
6431 };
6432 struct nfs4_getdevicelist_res res = {
6433 .devlist = devlist,
6434 };
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6437 .rpc_argp = &args,
6438 .rpc_resp = &res,
6439 };
6440 int status;
6441
6442 dprintk("--> %s\n", __func__);
6443 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6444 &res.seq_res, 0);
6445 dprintk("<-- %s status=%d\n", __func__, status);
6446 return status;
6447}
6448
6449int nfs4_proc_getdevicelist(struct nfs_server *server,
6450 const struct nfs_fh *fh,
6451 struct pnfs_devicelist *devlist)
6452{
6453 struct nfs4_exception exception = { };
6454 int err;
6455
6456 do {
6457 err = nfs4_handle_exception(server,
6458 _nfs4_getdevicelist(server, fh, devlist),
6459 &exception);
6460 } while (exception.retry);
6461
6462 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6463 err, devlist->num_devs);
6464
6465 return err;
6466}
6467EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6468
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006469static int
6470_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6471{
6472 struct nfs4_getdeviceinfo_args args = {
6473 .pdev = pdev,
6474 };
6475 struct nfs4_getdeviceinfo_res res = {
6476 .pdev = pdev,
6477 };
6478 struct rpc_message msg = {
6479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6480 .rpc_argp = &args,
6481 .rpc_resp = &res,
6482 };
6483 int status;
6484
6485 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006486 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006487 dprintk("<-- %s status=%d\n", __func__, status);
6488
6489 return status;
6490}
6491
6492int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496
6497 do {
6498 err = nfs4_handle_exception(server,
6499 _nfs4_proc_getdeviceinfo(server, pdev),
6500 &exception);
6501 } while (exception.retry);
6502 return err;
6503}
6504EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6505
Andy Adamson863a3c62011-03-23 13:27:54 +00006506static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6507{
6508 struct nfs4_layoutcommit_data *data = calldata;
6509 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006510 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006511
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006512 nfs41_setup_sequence(session,
6513 &data->args.seq_args,
6514 &data->res.seq_res,
6515 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006516}
6517
6518static void
6519nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6520{
6521 struct nfs4_layoutcommit_data *data = calldata;
6522 struct nfs_server *server = NFS_SERVER(data->args.inode);
6523
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006524 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006525 return;
6526
6527 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006528 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6529 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6530 case -NFS4ERR_BADLAYOUT: /* no layout */
6531 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006532 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006533 break;
6534 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006535 nfs_post_op_update_inode_force_wcc(data->args.inode,
6536 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006537 break;
6538 default:
6539 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6540 rpc_restart_call_prepare(task);
6541 return;
6542 }
6543 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006544}
6545
6546static void nfs4_layoutcommit_release(void *calldata)
6547{
6548 struct nfs4_layoutcommit_data *data = calldata;
6549
Andy Adamsondb29c082011-07-30 20:52:38 -04006550 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006551 put_rpccred(data->cred);
6552 kfree(data);
6553}
6554
6555static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6556 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6557 .rpc_call_done = nfs4_layoutcommit_done,
6558 .rpc_release = nfs4_layoutcommit_release,
6559};
6560
6561int
Andy Adamsonef311532011-03-12 02:58:10 -05006562nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006563{
6564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6566 .rpc_argp = &data->args,
6567 .rpc_resp = &data->res,
6568 .rpc_cred = data->cred,
6569 };
6570 struct rpc_task_setup task_setup_data = {
6571 .task = &data->task,
6572 .rpc_client = NFS_CLIENT(data->args.inode),
6573 .rpc_message = &msg,
6574 .callback_ops = &nfs4_layoutcommit_ops,
6575 .callback_data = data,
6576 .flags = RPC_TASK_ASYNC,
6577 };
6578 struct rpc_task *task;
6579 int status = 0;
6580
6581 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6582 "lbw: %llu inode %lu\n",
6583 data->task.tk_pid, sync,
6584 data->args.lastbytewritten,
6585 data->args.inode->i_ino);
6586
Trond Myklebust9d12b212012-01-17 22:04:25 -05006587 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006588 task = rpc_run_task(&task_setup_data);
6589 if (IS_ERR(task))
6590 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006591 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006592 goto out;
6593 status = nfs4_wait_for_completion_rpc_task(task);
6594 if (status != 0)
6595 goto out;
6596 status = task->tk_status;
6597out:
6598 dprintk("%s: status %d\n", __func__, status);
6599 rpc_put_task(task);
6600 return status;
6601}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006602
6603static int
6604_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6605 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6606{
6607 struct nfs41_secinfo_no_name_args args = {
6608 .style = SECINFO_STYLE_CURRENT_FH,
6609 };
6610 struct nfs4_secinfo_res res = {
6611 .flavors = flavors,
6612 };
6613 struct rpc_message msg = {
6614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6615 .rpc_argp = &args,
6616 .rpc_resp = &res,
6617 };
6618 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6619}
6620
6621static int
6622nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6623 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6624{
6625 struct nfs4_exception exception = { };
6626 int err;
6627 do {
6628 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6629 switch (err) {
6630 case 0:
6631 case -NFS4ERR_WRONGSEC:
6632 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006633 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006634 default:
6635 err = nfs4_handle_exception(server, err, &exception);
6636 }
6637 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006638out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006639 return err;
6640}
6641
6642static int
6643nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6644 struct nfs_fsinfo *info)
6645{
6646 int err;
6647 struct page *page;
6648 rpc_authflavor_t flavor;
6649 struct nfs4_secinfo_flavors *flavors;
6650
6651 page = alloc_page(GFP_KERNEL);
6652 if (!page) {
6653 err = -ENOMEM;
6654 goto out;
6655 }
6656
6657 flavors = page_address(page);
6658 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6659
6660 /*
6661 * Fall back on "guess and check" method if
6662 * the server doesn't support SECINFO_NO_NAME
6663 */
6664 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6665 err = nfs4_find_root_sec(server, fhandle, info);
6666 goto out_freepage;
6667 }
6668 if (err)
6669 goto out_freepage;
6670
6671 flavor = nfs_find_best_sec(flavors);
6672 if (err == 0)
6673 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6674
6675out_freepage:
6676 put_page(page);
6677 if (err == -EACCES)
6678 return -EPERM;
6679out:
6680 return err;
6681}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006682
6683static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006684{
6685 int status;
6686 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006687 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006688 };
6689 struct nfs41_test_stateid_res res;
6690 struct rpc_message msg = {
6691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6692 .rpc_argp = &args,
6693 .rpc_resp = &res,
6694 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006695
Chuck Lever38527b12012-07-11 16:30:23 -04006696 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006697 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006698 nfs4_set_sequence_privileged(&args.seq_args);
6699 status = nfs4_call_sync_sequence(server->client, server, &msg,
6700 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006701 if (status != NFS_OK) {
6702 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04006703 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04006704 }
6705 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04006706 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006707}
6708
Chuck Lever38527b12012-07-11 16:30:23 -04006709/**
6710 * nfs41_test_stateid - perform a TEST_STATEID operation
6711 *
6712 * @server: server / transport on which to perform the operation
6713 * @stateid: state ID to test
6714 *
6715 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6716 * Otherwise a negative NFS4ERR value is returned if the operation
6717 * failed or the state ID is not currently valid.
6718 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006719static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006720{
6721 struct nfs4_exception exception = { };
6722 int err;
6723 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006724 err = _nfs41_test_stateid(server, stateid);
6725 if (err != -NFS4ERR_DELAY)
6726 break;
6727 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006728 } while (exception.retry);
6729 return err;
6730}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006731
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006732static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006733{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006734 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006735 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006736 };
6737 struct nfs41_free_stateid_res res;
6738 struct rpc_message msg = {
6739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6740 .rpc_argp = &args,
6741 .rpc_resp = &res,
6742 };
Chuck Lever38527b12012-07-11 16:30:23 -04006743 int status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006744
Chuck Lever38527b12012-07-11 16:30:23 -04006745 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006746 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006747 nfs4_set_sequence_privileged(&args.seq_args);
Chuck Lever38527b12012-07-11 16:30:23 -04006748 status = nfs4_call_sync_sequence(server->client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006749 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04006750 dprintk("NFS reply free_stateid: %d\n", status);
6751 return status;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006752}
6753
Chuck Lever38527b12012-07-11 16:30:23 -04006754/**
6755 * nfs41_free_stateid - perform a FREE_STATEID operation
6756 *
6757 * @server: server / transport on which to perform the operation
6758 * @stateid: state ID to release
6759 *
6760 * Returns NFS_OK if the server freed "stateid". Otherwise a
6761 * negative NFS4ERR value is returned.
6762 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006763static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006764{
6765 struct nfs4_exception exception = { };
6766 int err;
6767 do {
Chuck Lever377e5072012-07-11 16:29:45 -04006768 err = _nfs4_free_stateid(server, stateid);
6769 if (err != -NFS4ERR_DELAY)
6770 break;
6771 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006772 } while (exception.retry);
6773 return err;
6774}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006775
6776static bool nfs41_match_stateid(const nfs4_stateid *s1,
6777 const nfs4_stateid *s2)
6778{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006779 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006780 return false;
6781
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006782 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006783 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006784 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006785 return true;
6786
6787 return false;
6788}
6789
Andy Adamson557134a2009-04-01 09:21:53 -04006790#endif /* CONFIG_NFS_V4_1 */
6791
Trond Myklebust36281ca2012-03-04 18:13:56 -05006792static bool nfs4_match_stateid(const nfs4_stateid *s1,
6793 const nfs4_stateid *s2)
6794{
Trond Myklebustf597c532012-03-04 18:13:56 -05006795 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006796}
6797
6798
Trond Myklebust17280172012-03-11 13:11:00 -04006799static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006800 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006801 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 .recover_open = nfs4_open_reclaim,
6803 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006804 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006805 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04006806 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807};
6808
Andy Adamson591d71c2009-04-01 09:22:47 -04006809#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006810static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006811 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6812 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6813 .recover_open = nfs4_open_reclaim,
6814 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006815 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006816 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006817 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04006818 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04006819};
6820#endif /* CONFIG_NFS_V4_1 */
6821
Trond Myklebust17280172012-03-11 13:11:00 -04006822static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006823 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006824 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 .recover_open = nfs4_open_expired,
6826 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006827 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006828 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829};
6830
Andy Adamson591d71c2009-04-01 09:22:47 -04006831#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006832static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006833 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6834 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006835 .recover_open = nfs41_open_expired,
6836 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006837 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006838 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006839};
6840#endif /* CONFIG_NFS_V4_1 */
6841
Trond Myklebust17280172012-03-11 13:11:00 -04006842static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006843 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006844 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006845 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006846};
6847
6848#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006849static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006850 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006851 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006852 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006853};
6854#endif
6855
Trond Myklebust97dc1352010-06-16 09:52:26 -04006856static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6857 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006858 .init_caps = NFS_CAP_READDIRPLUS
6859 | NFS_CAP_ATOMIC_OPEN
6860 | NFS_CAP_CHANGE_ATTR
6861 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006862 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006863 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006864 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006865 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6866 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6867 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006868};
6869
6870#if defined(CONFIG_NFS_V4_1)
6871static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6872 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04006873 .init_caps = NFS_CAP_READDIRPLUS
6874 | NFS_CAP_ATOMIC_OPEN
6875 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04006876 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04006877 | NFS_CAP_STATEID_NFSV41
6878 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05006879 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006880 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006881 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006882 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6883 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6884 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006885};
6886#endif
6887
Trond Myklebust97dc1352010-06-16 09:52:26 -04006888const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6889 [0] = &nfs_v4_0_minor_ops,
6890#if defined(CONFIG_NFS_V4_1)
6891 [1] = &nfs_v4_1_minor_ops,
6892#endif
6893};
6894
Bryan Schumaker73a79702012-07-16 16:39:12 -04006895const struct inode_operations nfs4_dir_inode_operations = {
6896 .create = nfs_create,
6897 .lookup = nfs_lookup,
6898 .atomic_open = nfs_atomic_open,
6899 .link = nfs_link,
6900 .unlink = nfs_unlink,
6901 .symlink = nfs_symlink,
6902 .mkdir = nfs_mkdir,
6903 .rmdir = nfs_rmdir,
6904 .mknod = nfs_mknod,
6905 .rename = nfs_rename,
6906 .permission = nfs_permission,
6907 .getattr = nfs_getattr,
6908 .setattr = nfs_setattr,
6909 .getxattr = generic_getxattr,
6910 .setxattr = generic_setxattr,
6911 .listxattr = generic_listxattr,
6912 .removexattr = generic_removexattr,
6913};
6914
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006915static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006916 .permission = nfs_permission,
6917 .getattr = nfs_getattr,
6918 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006919 .getxattr = generic_getxattr,
6920 .setxattr = generic_setxattr,
6921 .listxattr = generic_listxattr,
6922 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006923};
6924
David Howells509de812006-08-22 20:06:11 -04006925const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 .version = 4, /* protocol version */
6927 .dentry_ops = &nfs4_dentry_operations,
6928 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006929 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006930 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006932 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04006933 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 .getattr = nfs4_proc_getattr,
6935 .setattr = nfs4_proc_setattr,
6936 .lookup = nfs4_proc_lookup,
6937 .access = nfs4_proc_access,
6938 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 .create = nfs4_proc_create,
6940 .remove = nfs4_proc_remove,
6941 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006942 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 .unlink_done = nfs4_proc_unlink_done,
6944 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006945 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006946 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006947 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 .link = nfs4_proc_link,
6949 .symlink = nfs4_proc_symlink,
6950 .mkdir = nfs4_proc_mkdir,
6951 .rmdir = nfs4_proc_remove,
6952 .readdir = nfs4_proc_readdir,
6953 .mknod = nfs4_proc_mknod,
6954 .statfs = nfs4_proc_statfs,
6955 .fsinfo = nfs4_proc_fsinfo,
6956 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006957 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 .decode_dirent = nfs4_decode_dirent,
6959 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04006960 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006961 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006962 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04006964 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006965 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006966 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006968 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006969 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006971 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006972 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006973 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04006974 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04006975 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04006976 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00006977 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04006978 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04006979 .create_server = nfs4_create_server,
6980 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981};
6982
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006983static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6984 .prefix = XATTR_NAME_NFSV4_ACL,
6985 .list = nfs4_xattr_list_nfs4_acl,
6986 .get = nfs4_xattr_get_nfs4_acl,
6987 .set = nfs4_xattr_set_nfs4_acl,
6988};
6989
6990const struct xattr_handler *nfs4_xattr_handlers[] = {
6991 &nfs4_xattr_nfs4_acl_handler,
6992 NULL
6993};
6994
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995/*
6996 * Local variables:
6997 * c-basic-offset: 8
6998 * End:
6999 */